mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /*
ansond 0:137634ff4186 2 * Diffie-Hellman-Merkle key exchange
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 9 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 10 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 11 * (at your option) any later version.
ansond 0:137634ff4186 12 *
ansond 0:137634ff4186 13 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 16 * GNU General Public License for more details.
ansond 0:137634ff4186 17 *
ansond 0:137634ff4186 18 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 19 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 21 */
ansond 0:137634ff4186 22 /*
ansond 0:137634ff4186 23 * Reference:
ansond 0:137634ff4186 24 *
ansond 0:137634ff4186 25 * http://www.cacr.math.uwaterloo.ca/hac/ (chapter 12)
ansond 0:137634ff4186 26 */
ansond 0:137634ff4186 27
ansond 0:137634ff4186 28 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 29 #include "polarssl/config.h"
ansond 0:137634ff4186 30 #else
ansond 0:137634ff4186 31 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 32 #endif
ansond 0:137634ff4186 33
ansond 0:137634ff4186 34 #if defined(POLARSSL_DHM_C)
ansond 0:137634ff4186 35
ansond 0:137634ff4186 36 #include "polarssl/dhm.h"
ansond 0:137634ff4186 37
ansond 0:137634ff4186 38 #include <string.h>
ansond 0:137634ff4186 39
ansond 0:137634ff4186 40 #if defined(POLARSSL_PEM_PARSE_C)
ansond 0:137634ff4186 41 #include "polarssl/pem.h"
ansond 0:137634ff4186 42 #endif
ansond 0:137634ff4186 43
ansond 0:137634ff4186 44 #if defined(POLARSSL_ASN1_PARSE_C)
ansond 0:137634ff4186 45 #include "polarssl/asn1.h"
ansond 0:137634ff4186 46 #endif
ansond 0:137634ff4186 47
ansond 0:137634ff4186 48 #if defined(POLARSSL_PLATFORM_C)
ansond 0:137634ff4186 49 #include "polarssl/platform.h"
ansond 0:137634ff4186 50 #else
ansond 0:137634ff4186 51 #include <stdlib.h>
ansond 0:137634ff4186 52 #include <stdio.h>
ansond 0:137634ff4186 53 #define polarssl_printf printf
ansond 0:137634ff4186 54 #define polarssl_malloc malloc
ansond 0:137634ff4186 55 #define polarssl_free free
ansond 0:137634ff4186 56 #endif
ansond 0:137634ff4186 57
ansond 0:137634ff4186 58 /* Implementation that should never be optimized out by the compiler */
ansond 0:137634ff4186 59 static void polarssl_zeroize( void *v, size_t n ) {
ansond 0:137634ff4186 60 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
ansond 0:137634ff4186 61 }
ansond 0:137634ff4186 62
ansond 0:137634ff4186 63 /*
ansond 0:137634ff4186 64 * helper to validate the mpi size and import it
ansond 0:137634ff4186 65 */
ansond 0:137634ff4186 66 static int dhm_read_bignum( mpi *X,
ansond 0:137634ff4186 67 unsigned char **p,
ansond 0:137634ff4186 68 const unsigned char *end )
ansond 0:137634ff4186 69 {
ansond 0:137634ff4186 70 int ret, n;
ansond 0:137634ff4186 71
ansond 0:137634ff4186 72 if( end - *p < 2 )
ansond 0:137634ff4186 73 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
ansond 0:137634ff4186 74
ansond 0:137634ff4186 75 n = ( (*p)[0] << 8 ) | (*p)[1];
ansond 0:137634ff4186 76 (*p) += 2;
ansond 0:137634ff4186 77
ansond 0:137634ff4186 78 if( (int)( end - *p ) < n )
ansond 0:137634ff4186 79 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
ansond 0:137634ff4186 80
ansond 0:137634ff4186 81 if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
ansond 0:137634ff4186 82 return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
ansond 0:137634ff4186 83
ansond 0:137634ff4186 84 (*p) += n;
ansond 0:137634ff4186 85
ansond 0:137634ff4186 86 return( 0 );
ansond 0:137634ff4186 87 }
ansond 0:137634ff4186 88
ansond 0:137634ff4186 89 /*
ansond 0:137634ff4186 90 * Verify sanity of parameter with regards to P
ansond 0:137634ff4186 91 *
ansond 0:137634ff4186 92 * Parameter should be: 2 <= public_param <= P - 2
ansond 0:137634ff4186 93 *
ansond 0:137634ff4186 94 * For more information on the attack, see:
ansond 0:137634ff4186 95 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
ansond 0:137634ff4186 96 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
ansond 0:137634ff4186 97 */
ansond 0:137634ff4186 98 static int dhm_check_range( const mpi *param, const mpi *P )
ansond 0:137634ff4186 99 {
ansond 0:137634ff4186 100 mpi L, U;
ansond 0:137634ff4186 101 int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
ansond 0:137634ff4186 102
ansond 0:137634ff4186 103 mpi_init( &L ); mpi_init( &U );
ansond 0:137634ff4186 104
ansond 0:137634ff4186 105 MPI_CHK( mpi_lset( &L, 2 ) );
ansond 0:137634ff4186 106 MPI_CHK( mpi_sub_int( &U, P, 2 ) );
ansond 0:137634ff4186 107
ansond 0:137634ff4186 108 if( mpi_cmp_mpi( param, &L ) >= 0 &&
ansond 0:137634ff4186 109 mpi_cmp_mpi( param, &U ) <= 0 )
ansond 0:137634ff4186 110 {
ansond 0:137634ff4186 111 ret = 0;
ansond 0:137634ff4186 112 }
ansond 0:137634ff4186 113
ansond 0:137634ff4186 114 cleanup:
ansond 0:137634ff4186 115 mpi_free( &L ); mpi_free( &U );
ansond 0:137634ff4186 116 return( ret );
ansond 0:137634ff4186 117 }
ansond 0:137634ff4186 118
ansond 0:137634ff4186 119 void dhm_init( dhm_context *ctx )
ansond 0:137634ff4186 120 {
ansond 0:137634ff4186 121 memset( ctx, 0, sizeof( dhm_context ) );
ansond 0:137634ff4186 122 }
ansond 0:137634ff4186 123
ansond 0:137634ff4186 124 /*
ansond 0:137634ff4186 125 * Parse the ServerKeyExchange parameters
ansond 0:137634ff4186 126 */
ansond 0:137634ff4186 127 int dhm_read_params( dhm_context *ctx,
ansond 0:137634ff4186 128 unsigned char **p,
ansond 0:137634ff4186 129 const unsigned char *end )
ansond 0:137634ff4186 130 {
ansond 0:137634ff4186 131 int ret;
ansond 0:137634ff4186 132
ansond 0:137634ff4186 133 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
ansond 0:137634ff4186 134 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
ansond 0:137634ff4186 135 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
ansond 0:137634ff4186 136 return( ret );
ansond 0:137634ff4186 137
ansond 0:137634ff4186 138 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
ansond 0:137634ff4186 139 return( ret );
ansond 0:137634ff4186 140
ansond 0:137634ff4186 141 ctx->len = mpi_size( &ctx->P );
ansond 0:137634ff4186 142
ansond 0:137634ff4186 143 return( 0 );
ansond 0:137634ff4186 144 }
ansond 0:137634ff4186 145
ansond 0:137634ff4186 146 /*
ansond 0:137634ff4186 147 * Setup and write the ServerKeyExchange parameters
ansond 0:137634ff4186 148 */
ansond 0:137634ff4186 149 int dhm_make_params( dhm_context *ctx, int x_size,
ansond 0:137634ff4186 150 unsigned char *output, size_t *olen,
ansond 0:137634ff4186 151 int (*f_rng)(void *, unsigned char *, size_t),
ansond 0:137634ff4186 152 void *p_rng )
ansond 0:137634ff4186 153 {
ansond 0:137634ff4186 154 int ret, count = 0;
ansond 0:137634ff4186 155 size_t n1, n2, n3;
ansond 0:137634ff4186 156 unsigned char *p;
ansond 0:137634ff4186 157
ansond 0:137634ff4186 158 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
ansond 0:137634ff4186 159 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
ansond 0:137634ff4186 160
ansond 0:137634ff4186 161 /*
ansond 0:137634ff4186 162 * Generate X as large as possible ( < P )
ansond 0:137634ff4186 163 */
ansond 0:137634ff4186 164 do
ansond 0:137634ff4186 165 {
ansond 0:137634ff4186 166 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
ansond 0:137634ff4186 167
ansond 0:137634ff4186 168 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
ansond 0:137634ff4186 169 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
ansond 0:137634ff4186 170
ansond 0:137634ff4186 171 if( count++ > 10 )
ansond 0:137634ff4186 172 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
ansond 0:137634ff4186 173 }
ansond 0:137634ff4186 174 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
ansond 0:137634ff4186 175
ansond 0:137634ff4186 176 /*
ansond 0:137634ff4186 177 * Calculate GX = G^X mod P
ansond 0:137634ff4186 178 */
ansond 0:137634ff4186 179 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
ansond 0:137634ff4186 180 &ctx->P , &ctx->RP ) );
ansond 0:137634ff4186 181
ansond 0:137634ff4186 182 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
ansond 0:137634ff4186 183 return( ret );
ansond 0:137634ff4186 184
ansond 0:137634ff4186 185 /*
ansond 0:137634ff4186 186 * export P, G, GX
ansond 0:137634ff4186 187 */
ansond 0:137634ff4186 188 #define DHM_MPI_EXPORT(X,n) \
ansond 0:137634ff4186 189 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
ansond 0:137634ff4186 190 *p++ = (unsigned char)( n >> 8 ); \
ansond 0:137634ff4186 191 *p++ = (unsigned char)( n ); p += n;
ansond 0:137634ff4186 192
ansond 0:137634ff4186 193 n1 = mpi_size( &ctx->P );
ansond 0:137634ff4186 194 n2 = mpi_size( &ctx->G );
ansond 0:137634ff4186 195 n3 = mpi_size( &ctx->GX );
ansond 0:137634ff4186 196
ansond 0:137634ff4186 197 p = output;
ansond 0:137634ff4186 198 DHM_MPI_EXPORT( &ctx->P , n1 );
ansond 0:137634ff4186 199 DHM_MPI_EXPORT( &ctx->G , n2 );
ansond 0:137634ff4186 200 DHM_MPI_EXPORT( &ctx->GX, n3 );
ansond 0:137634ff4186 201
ansond 0:137634ff4186 202 *olen = p - output;
ansond 0:137634ff4186 203
ansond 0:137634ff4186 204 ctx->len = n1;
ansond 0:137634ff4186 205
ansond 0:137634ff4186 206 cleanup:
ansond 0:137634ff4186 207
ansond 0:137634ff4186 208 if( ret != 0 )
ansond 0:137634ff4186 209 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
ansond 0:137634ff4186 210
ansond 0:137634ff4186 211 return( 0 );
ansond 0:137634ff4186 212 }
ansond 0:137634ff4186 213
ansond 0:137634ff4186 214 /*
ansond 0:137634ff4186 215 * Import the peer's public value G^Y
ansond 0:137634ff4186 216 */
ansond 0:137634ff4186 217 int dhm_read_public( dhm_context *ctx,
ansond 0:137634ff4186 218 const unsigned char *input, size_t ilen )
ansond 0:137634ff4186 219 {
ansond 0:137634ff4186 220 int ret;
ansond 0:137634ff4186 221
ansond 0:137634ff4186 222 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
ansond 0:137634ff4186 223 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
ansond 0:137634ff4186 224
ansond 0:137634ff4186 225 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
ansond 0:137634ff4186 226 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
ansond 0:137634ff4186 227
ansond 0:137634ff4186 228 return( 0 );
ansond 0:137634ff4186 229 }
ansond 0:137634ff4186 230
ansond 0:137634ff4186 231 /*
ansond 0:137634ff4186 232 * Create own private value X and export G^X
ansond 0:137634ff4186 233 */
ansond 0:137634ff4186 234 int dhm_make_public( dhm_context *ctx, int x_size,
ansond 0:137634ff4186 235 unsigned char *output, size_t olen,
ansond 0:137634ff4186 236 int (*f_rng)(void *, unsigned char *, size_t),
ansond 0:137634ff4186 237 void *p_rng )
ansond 0:137634ff4186 238 {
ansond 0:137634ff4186 239 int ret, count = 0;
ansond 0:137634ff4186 240
ansond 0:137634ff4186 241 if( ctx == NULL || olen < 1 || olen > ctx->len )
ansond 0:137634ff4186 242 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
ansond 0:137634ff4186 243
ansond 0:137634ff4186 244 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
ansond 0:137634ff4186 245 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
ansond 0:137634ff4186 246
ansond 0:137634ff4186 247 /*
ansond 0:137634ff4186 248 * generate X and calculate GX = G^X mod P
ansond 0:137634ff4186 249 */
ansond 0:137634ff4186 250 do
ansond 0:137634ff4186 251 {
ansond 0:137634ff4186 252 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
ansond 0:137634ff4186 253
ansond 0:137634ff4186 254 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
ansond 0:137634ff4186 255 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
ansond 0:137634ff4186 256
ansond 0:137634ff4186 257 if( count++ > 10 )
ansond 0:137634ff4186 258 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
ansond 0:137634ff4186 259 }
ansond 0:137634ff4186 260 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
ansond 0:137634ff4186 261
ansond 0:137634ff4186 262 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
ansond 0:137634ff4186 263 &ctx->P , &ctx->RP ) );
ansond 0:137634ff4186 264
ansond 0:137634ff4186 265 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
ansond 0:137634ff4186 266 return( ret );
ansond 0:137634ff4186 267
ansond 0:137634ff4186 268 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
ansond 0:137634ff4186 269
ansond 0:137634ff4186 270 cleanup:
ansond 0:137634ff4186 271
ansond 0:137634ff4186 272 if( ret != 0 )
ansond 0:137634ff4186 273 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
ansond 0:137634ff4186 274
ansond 0:137634ff4186 275 return( 0 );
ansond 0:137634ff4186 276 }
ansond 0:137634ff4186 277
ansond 0:137634ff4186 278 /*
ansond 0:137634ff4186 279 * Use the blinding method and optimisation suggested in section 10 of:
ansond 0:137634ff4186 280 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
ansond 0:137634ff4186 281 * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
ansond 0:137634ff4186 282 * Berlin Heidelberg, 1996. p. 104-113.
ansond 0:137634ff4186 283 */
ansond 0:137634ff4186 284 static int dhm_update_blinding( dhm_context *ctx,
ansond 0:137634ff4186 285 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
ansond 0:137634ff4186 286 {
ansond 0:137634ff4186 287 int ret, count;
ansond 0:137634ff4186 288
ansond 0:137634ff4186 289 /*
ansond 0:137634ff4186 290 * Don't use any blinding the first time a particular X is used,
ansond 0:137634ff4186 291 * but remember it to use blinding next time.
ansond 0:137634ff4186 292 */
ansond 0:137634ff4186 293 if( mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
ansond 0:137634ff4186 294 {
ansond 0:137634ff4186 295 MPI_CHK( mpi_copy( &ctx->pX, &ctx->X ) );
ansond 0:137634ff4186 296 MPI_CHK( mpi_lset( &ctx->Vi, 1 ) );
ansond 0:137634ff4186 297 MPI_CHK( mpi_lset( &ctx->Vf, 1 ) );
ansond 0:137634ff4186 298
ansond 0:137634ff4186 299 return( 0 );
ansond 0:137634ff4186 300 }
ansond 0:137634ff4186 301
ansond 0:137634ff4186 302 /*
ansond 0:137634ff4186 303 * Ok, we need blinding. Can we re-use existing values?
ansond 0:137634ff4186 304 * If yes, just update them by squaring them.
ansond 0:137634ff4186 305 */
ansond 0:137634ff4186 306 if( mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
ansond 0:137634ff4186 307 {
ansond 0:137634ff4186 308 MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
ansond 0:137634ff4186 309 MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
ansond 0:137634ff4186 310
ansond 0:137634ff4186 311 MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
ansond 0:137634ff4186 312 MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
ansond 0:137634ff4186 313
ansond 0:137634ff4186 314 return( 0 );
ansond 0:137634ff4186 315 }
ansond 0:137634ff4186 316
ansond 0:137634ff4186 317 /*
ansond 0:137634ff4186 318 * We need to generate blinding values from scratch
ansond 0:137634ff4186 319 */
ansond 0:137634ff4186 320
ansond 0:137634ff4186 321 /* Vi = random( 2, P-1 ) */
ansond 0:137634ff4186 322 count = 0;
ansond 0:137634ff4186 323 do
ansond 0:137634ff4186 324 {
ansond 0:137634ff4186 325 mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng );
ansond 0:137634ff4186 326
ansond 0:137634ff4186 327 while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
ansond 0:137634ff4186 328 MPI_CHK( mpi_shift_r( &ctx->Vi, 1 ) );
ansond 0:137634ff4186 329
ansond 0:137634ff4186 330 if( count++ > 10 )
ansond 0:137634ff4186 331 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
ansond 0:137634ff4186 332 }
ansond 0:137634ff4186 333 while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
ansond 0:137634ff4186 334
ansond 0:137634ff4186 335 /* Vf = Vi^-X mod P */
ansond 0:137634ff4186 336 MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
ansond 0:137634ff4186 337 MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
ansond 0:137634ff4186 338
ansond 0:137634ff4186 339 cleanup:
ansond 0:137634ff4186 340 return( ret );
ansond 0:137634ff4186 341 }
ansond 0:137634ff4186 342
ansond 0:137634ff4186 343 /*
ansond 0:137634ff4186 344 * Derive and export the shared secret (G^Y)^X mod P
ansond 0:137634ff4186 345 */
ansond 0:137634ff4186 346 int dhm_calc_secret( dhm_context *ctx,
ansond 0:137634ff4186 347 unsigned char *output, size_t *olen,
ansond 0:137634ff4186 348 int (*f_rng)(void *, unsigned char *, size_t),
ansond 0:137634ff4186 349 void *p_rng )
ansond 0:137634ff4186 350 {
ansond 0:137634ff4186 351 int ret;
ansond 0:137634ff4186 352 mpi GYb;
ansond 0:137634ff4186 353
ansond 0:137634ff4186 354 if( ctx == NULL || *olen < ctx->len )
ansond 0:137634ff4186 355 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
ansond 0:137634ff4186 356
ansond 0:137634ff4186 357 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
ansond 0:137634ff4186 358 return( ret );
ansond 0:137634ff4186 359
ansond 0:137634ff4186 360 mpi_init( &GYb );
ansond 0:137634ff4186 361
ansond 0:137634ff4186 362 /* Blind peer's value */
ansond 0:137634ff4186 363 if( f_rng != NULL )
ansond 0:137634ff4186 364 {
ansond 0:137634ff4186 365 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
ansond 0:137634ff4186 366 MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
ansond 0:137634ff4186 367 MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
ansond 0:137634ff4186 368 }
ansond 0:137634ff4186 369 else
ansond 0:137634ff4186 370 MPI_CHK( mpi_copy( &GYb, &ctx->GY ) );
ansond 0:137634ff4186 371
ansond 0:137634ff4186 372 /* Do modular exponentiation */
ansond 0:137634ff4186 373 MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
ansond 0:137634ff4186 374 &ctx->P, &ctx->RP ) );
ansond 0:137634ff4186 375
ansond 0:137634ff4186 376 /* Unblind secret value */
ansond 0:137634ff4186 377 if( f_rng != NULL )
ansond 0:137634ff4186 378 {
ansond 0:137634ff4186 379 MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
ansond 0:137634ff4186 380 MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
ansond 0:137634ff4186 381 }
ansond 0:137634ff4186 382
ansond 0:137634ff4186 383 *olen = mpi_size( &ctx->K );
ansond 0:137634ff4186 384
ansond 0:137634ff4186 385 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
ansond 0:137634ff4186 386
ansond 0:137634ff4186 387 cleanup:
ansond 0:137634ff4186 388 mpi_free( &GYb );
ansond 0:137634ff4186 389
ansond 0:137634ff4186 390 if( ret != 0 )
ansond 0:137634ff4186 391 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
ansond 0:137634ff4186 392
ansond 0:137634ff4186 393 return( 0 );
ansond 0:137634ff4186 394 }
ansond 0:137634ff4186 395
ansond 0:137634ff4186 396 /*
ansond 0:137634ff4186 397 * Free the components of a DHM key
ansond 0:137634ff4186 398 */
ansond 0:137634ff4186 399 void dhm_free( dhm_context *ctx )
ansond 0:137634ff4186 400 {
ansond 0:137634ff4186 401 mpi_free( &ctx->pX); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi );
ansond 0:137634ff4186 402 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
ansond 0:137634ff4186 403 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
ansond 0:137634ff4186 404 mpi_free( &ctx->P );
ansond 0:137634ff4186 405
ansond 0:137634ff4186 406 polarssl_zeroize( ctx, sizeof( dhm_context ) );
ansond 0:137634ff4186 407 }
ansond 0:137634ff4186 408
ansond 0:137634ff4186 409 #if defined(POLARSSL_ASN1_PARSE_C)
ansond 0:137634ff4186 410 /*
ansond 0:137634ff4186 411 * Parse DHM parameters
ansond 0:137634ff4186 412 */
ansond 0:137634ff4186 413 int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin,
ansond 0:137634ff4186 414 size_t dhminlen )
ansond 0:137634ff4186 415 {
ansond 0:137634ff4186 416 int ret;
ansond 0:137634ff4186 417 size_t len;
ansond 0:137634ff4186 418 unsigned char *p, *end;
ansond 0:137634ff4186 419 #if defined(POLARSSL_PEM_PARSE_C)
ansond 0:137634ff4186 420 pem_context pem;
ansond 0:137634ff4186 421
ansond 0:137634ff4186 422 pem_init( &pem );
ansond 0:137634ff4186 423
ansond 0:137634ff4186 424 ret = pem_read_buffer( &pem,
ansond 0:137634ff4186 425 "-----BEGIN DH PARAMETERS-----",
ansond 0:137634ff4186 426 "-----END DH PARAMETERS-----",
ansond 0:137634ff4186 427 dhmin, NULL, 0, &dhminlen );
ansond 0:137634ff4186 428
ansond 0:137634ff4186 429 if( ret == 0 )
ansond 0:137634ff4186 430 {
ansond 0:137634ff4186 431 /*
ansond 0:137634ff4186 432 * Was PEM encoded
ansond 0:137634ff4186 433 */
ansond 0:137634ff4186 434 dhminlen = pem.buflen;
ansond 0:137634ff4186 435 }
ansond 0:137634ff4186 436 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
ansond 0:137634ff4186 437 goto exit;
ansond 0:137634ff4186 438
ansond 0:137634ff4186 439 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
ansond 0:137634ff4186 440 #else
ansond 0:137634ff4186 441 p = (unsigned char *) dhmin;
ansond 0:137634ff4186 442 #endif /* POLARSSL_PEM_PARSE_C */
ansond 0:137634ff4186 443 end = p + dhminlen;
ansond 0:137634ff4186 444
ansond 0:137634ff4186 445 /*
ansond 0:137634ff4186 446 * DHParams ::= SEQUENCE {
ansond 0:137634ff4186 447 * prime INTEGER, -- P
ansond 0:137634ff4186 448 * generator INTEGER, -- g
ansond 0:137634ff4186 449 * privateValueLength INTEGER OPTIONAL
ansond 0:137634ff4186 450 * }
ansond 0:137634ff4186 451 */
ansond 0:137634ff4186 452 if( ( ret = asn1_get_tag( &p, end, &len,
ansond 0:137634ff4186 453 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
ansond 0:137634ff4186 454 {
ansond 0:137634ff4186 455 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
ansond 0:137634ff4186 456 goto exit;
ansond 0:137634ff4186 457 }
ansond 0:137634ff4186 458
ansond 0:137634ff4186 459 end = p + len;
ansond 0:137634ff4186 460
ansond 0:137634ff4186 461 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
ansond 0:137634ff4186 462 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
ansond 0:137634ff4186 463 {
ansond 0:137634ff4186 464 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
ansond 0:137634ff4186 465 goto exit;
ansond 0:137634ff4186 466 }
ansond 0:137634ff4186 467
ansond 0:137634ff4186 468 if( p != end )
ansond 0:137634ff4186 469 {
ansond 0:137634ff4186 470 /* this might be the optional privateValueLength; If so, we
ansond 0:137634ff4186 471 can cleanly discard it; */
ansond 0:137634ff4186 472 mpi rec;
ansond 0:137634ff4186 473 mpi_init( &rec );
ansond 0:137634ff4186 474 ret = asn1_get_mpi( &p, end, &rec );
ansond 0:137634ff4186 475 mpi_free( &rec );
ansond 0:137634ff4186 476 if ( ret != 0 )
ansond 0:137634ff4186 477 {
ansond 0:137634ff4186 478 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
ansond 0:137634ff4186 479 goto exit;
ansond 0:137634ff4186 480 }
ansond 0:137634ff4186 481 if ( p != end )
ansond 0:137634ff4186 482 {
ansond 0:137634ff4186 483 ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
ansond 0:137634ff4186 484 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
ansond 0:137634ff4186 485 goto exit;
ansond 0:137634ff4186 486 }
ansond 0:137634ff4186 487 }
ansond 0:137634ff4186 488
ansond 0:137634ff4186 489 ret = 0;
ansond 0:137634ff4186 490
ansond 0:137634ff4186 491 dhm->len = mpi_size( &dhm->P );
ansond 0:137634ff4186 492
ansond 0:137634ff4186 493 exit:
ansond 0:137634ff4186 494 #if defined(POLARSSL_PEM_PARSE_C)
ansond 0:137634ff4186 495 pem_free( &pem );
ansond 0:137634ff4186 496 #endif
ansond 0:137634ff4186 497 if( ret != 0 )
ansond 0:137634ff4186 498 dhm_free( dhm );
ansond 0:137634ff4186 499
ansond 0:137634ff4186 500 return( ret );
ansond 0:137634ff4186 501 }
ansond 0:137634ff4186 502
ansond 0:137634ff4186 503 #if defined(POLARSSL_FS_IO)
ansond 0:137634ff4186 504 /*
ansond 0:137634ff4186 505 * Load all data from a file into a given buffer.
ansond 0:137634ff4186 506 */
ansond 0:137634ff4186 507 static int load_file( const char *path, unsigned char **buf, size_t *n )
ansond 0:137634ff4186 508 {
ansond 0:137634ff4186 509 FILE *f;
ansond 0:137634ff4186 510 long size;
ansond 0:137634ff4186 511
ansond 0:137634ff4186 512 if( ( f = fopen( path, "rb" ) ) == NULL )
ansond 0:137634ff4186 513 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
ansond 0:137634ff4186 514
ansond 0:137634ff4186 515 fseek( f, 0, SEEK_END );
ansond 0:137634ff4186 516 if( ( size = ftell( f ) ) == -1 )
ansond 0:137634ff4186 517 {
ansond 0:137634ff4186 518 fclose( f );
ansond 0:137634ff4186 519 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
ansond 0:137634ff4186 520 }
ansond 0:137634ff4186 521 fseek( f, 0, SEEK_SET );
ansond 0:137634ff4186 522
ansond 0:137634ff4186 523 *n = (size_t) size;
ansond 0:137634ff4186 524
ansond 0:137634ff4186 525 if( *n + 1 == 0 ||
ansond 0:137634ff4186 526 ( *buf = polarssl_malloc( *n + 1 ) ) == NULL )
ansond 0:137634ff4186 527 {
ansond 0:137634ff4186 528 fclose( f );
ansond 0:137634ff4186 529 return( POLARSSL_ERR_DHM_MALLOC_FAILED );
ansond 0:137634ff4186 530 }
ansond 0:137634ff4186 531
ansond 0:137634ff4186 532 if( fread( *buf, 1, *n, f ) != *n )
ansond 0:137634ff4186 533 {
ansond 0:137634ff4186 534 fclose( f );
ansond 0:137634ff4186 535 polarssl_free( *buf );
ansond 0:137634ff4186 536 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
ansond 0:137634ff4186 537 }
ansond 0:137634ff4186 538
ansond 0:137634ff4186 539 fclose( f );
ansond 0:137634ff4186 540
ansond 0:137634ff4186 541 (*buf)[*n] = '\0';
ansond 0:137634ff4186 542
ansond 0:137634ff4186 543 return( 0 );
ansond 0:137634ff4186 544 }
ansond 0:137634ff4186 545
ansond 0:137634ff4186 546 /*
ansond 0:137634ff4186 547 * Load and parse DHM parameters
ansond 0:137634ff4186 548 */
ansond 0:137634ff4186 549 int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
ansond 0:137634ff4186 550 {
ansond 0:137634ff4186 551 int ret;
ansond 0:137634ff4186 552 size_t n;
ansond 0:137634ff4186 553 unsigned char *buf;
ansond 0:137634ff4186 554
ansond 0:137634ff4186 555 if( ( ret = load_file( path, &buf, &n ) ) != 0 )
ansond 0:137634ff4186 556 return( ret );
ansond 0:137634ff4186 557
ansond 0:137634ff4186 558 ret = dhm_parse_dhm( dhm, buf, n );
ansond 0:137634ff4186 559
ansond 0:137634ff4186 560 polarssl_zeroize( buf, n + 1 );
ansond 0:137634ff4186 561 polarssl_free( buf );
ansond 0:137634ff4186 562
ansond 0:137634ff4186 563 return( ret );
ansond 0:137634ff4186 564 }
ansond 0:137634ff4186 565 #endif /* POLARSSL_FS_IO */
ansond 0:137634ff4186 566 #endif /* POLARSSL_ASN1_PARSE_C */
ansond 0:137634ff4186 567
ansond 0:137634ff4186 568 #if defined(POLARSSL_SELF_TEST)
ansond 0:137634ff4186 569
ansond 0:137634ff4186 570 #include "polarssl/certs.h"
ansond 0:137634ff4186 571
ansond 0:137634ff4186 572 /*
ansond 0:137634ff4186 573 * Checkup routine
ansond 0:137634ff4186 574 */
ansond 0:137634ff4186 575 int dhm_self_test( int verbose )
ansond 0:137634ff4186 576 {
ansond 0:137634ff4186 577 #if defined(POLARSSL_CERTS_C)
ansond 0:137634ff4186 578 int ret;
ansond 0:137634ff4186 579 dhm_context dhm;
ansond 0:137634ff4186 580
ansond 0:137634ff4186 581 dhm_init( &dhm );
ansond 0:137634ff4186 582
ansond 0:137634ff4186 583 if( verbose != 0 )
ansond 0:137634ff4186 584 polarssl_printf( " DHM parameter load: " );
ansond 0:137634ff4186 585
ansond 0:137634ff4186 586 if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
ansond 0:137634ff4186 587 strlen( test_dhm_params ) ) ) != 0 )
ansond 0:137634ff4186 588 {
ansond 0:137634ff4186 589 if( verbose != 0 )
ansond 0:137634ff4186 590 polarssl_printf( "failed\n" );
ansond 0:137634ff4186 591
ansond 0:137634ff4186 592 ret = 1;
ansond 0:137634ff4186 593 goto exit;
ansond 0:137634ff4186 594 }
ansond 0:137634ff4186 595
ansond 0:137634ff4186 596 if( verbose != 0 )
ansond 0:137634ff4186 597 polarssl_printf( "passed\n\n" );
ansond 0:137634ff4186 598
ansond 0:137634ff4186 599 exit:
ansond 0:137634ff4186 600 dhm_free( &dhm );
ansond 0:137634ff4186 601
ansond 0:137634ff4186 602 return( ret );
ansond 0:137634ff4186 603 #else
ansond 0:137634ff4186 604 if( verbose != 0 )
ansond 0:137634ff4186 605 polarssl_printf( " DHM parameter load: skipped\n" );
ansond 0:137634ff4186 606
ansond 0:137634ff4186 607 return( 0 );
ansond 0:137634ff4186 608 #endif /* POLARSSL_CERTS_C */
ansond 0:137634ff4186 609 }
ansond 0:137634ff4186 610
ansond 0:137634ff4186 611 #endif /* POLARSSL_SELF_TEST */
ansond 0:137634ff4186 612
ansond 0:137634ff4186 613 #endif /* POLARSSL_DHM_C */
ansond 0:137634ff4186 614