mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Child:
2:bbdeda018a3c
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /*
markrad 0:cdf462088d13 2 * Diffie-Hellman-Merkle key exchange
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
markrad 0:cdf462088d13 5 * SPDX-License-Identifier: Apache-2.0
markrad 0:cdf462088d13 6 *
markrad 0:cdf462088d13 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
markrad 0:cdf462088d13 8 * not use this file except in compliance with the License.
markrad 0:cdf462088d13 9 * You may obtain a copy of the License at
markrad 0:cdf462088d13 10 *
markrad 0:cdf462088d13 11 * http://www.apache.org/licenses/LICENSE-2.0
markrad 0:cdf462088d13 12 *
markrad 0:cdf462088d13 13 * Unless required by applicable law or agreed to in writing, software
markrad 0:cdf462088d13 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
markrad 0:cdf462088d13 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
markrad 0:cdf462088d13 16 * See the License for the specific language governing permissions and
markrad 0:cdf462088d13 17 * limitations under the License.
markrad 0:cdf462088d13 18 *
markrad 0:cdf462088d13 19 * This file is part of mbed TLS (https://tls.mbed.org)
markrad 0:cdf462088d13 20 */
markrad 0:cdf462088d13 21 /*
markrad 0:cdf462088d13 22 * The following sources were referenced in the design of this implementation
markrad 0:cdf462088d13 23 * of the Diffie-Hellman-Merkle algorithm:
markrad 0:cdf462088d13 24 *
markrad 0:cdf462088d13 25 * [1] Handbook of Applied Cryptography - 1997, Chapter 12
markrad 0:cdf462088d13 26 * Menezes, van Oorschot and Vanstone
markrad 0:cdf462088d13 27 *
markrad 0:cdf462088d13 28 */
markrad 0:cdf462088d13 29
markrad 0:cdf462088d13 30 #if !defined(MBEDTLS_CONFIG_FILE)
markrad 0:cdf462088d13 31 #include "mbedtls/config.h"
markrad 0:cdf462088d13 32 #else
markrad 0:cdf462088d13 33 #include MBEDTLS_CONFIG_FILE
markrad 0:cdf462088d13 34 #endif
markrad 0:cdf462088d13 35
markrad 0:cdf462088d13 36 #if defined(MBEDTLS_DHM_C)
markrad 0:cdf462088d13 37
markrad 0:cdf462088d13 38 #include "mbedtls/dhm.h"
markrad 0:cdf462088d13 39
markrad 0:cdf462088d13 40 #include <string.h>
markrad 0:cdf462088d13 41
markrad 0:cdf462088d13 42 #if defined(MBEDTLS_PEM_PARSE_C)
markrad 0:cdf462088d13 43 #include "mbedtls/pem.h"
markrad 0:cdf462088d13 44 #endif
markrad 0:cdf462088d13 45
markrad 0:cdf462088d13 46 #if defined(MBEDTLS_ASN1_PARSE_C)
markrad 0:cdf462088d13 47 #include "mbedtls/asn1.h"
markrad 0:cdf462088d13 48 #endif
markrad 0:cdf462088d13 49
markrad 0:cdf462088d13 50 #if defined(MBEDTLS_PLATFORM_C)
markrad 0:cdf462088d13 51 #include "mbedtls/platform.h"
markrad 0:cdf462088d13 52 #else
markrad 0:cdf462088d13 53 #include <stdlib.h>
markrad 0:cdf462088d13 54 #include <stdio.h>
markrad 0:cdf462088d13 55 #define mbedtls_printf printf
markrad 0:cdf462088d13 56 #define mbedtls_calloc calloc
markrad 0:cdf462088d13 57 #define mbedtls_free free
markrad 0:cdf462088d13 58 #endif
markrad 0:cdf462088d13 59
markrad 0:cdf462088d13 60 /* Implementation that should never be optimized out by the compiler */
markrad 0:cdf462088d13 61 static void mbedtls_zeroize( void *v, size_t n ) {
markrad 0:cdf462088d13 62 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
markrad 0:cdf462088d13 63 }
markrad 0:cdf462088d13 64
markrad 0:cdf462088d13 65 /*
markrad 0:cdf462088d13 66 * helper to validate the mbedtls_mpi size and import it
markrad 0:cdf462088d13 67 */
markrad 0:cdf462088d13 68 static int dhm_read_bignum( mbedtls_mpi *X,
markrad 0:cdf462088d13 69 unsigned char **p,
markrad 0:cdf462088d13 70 const unsigned char *end )
markrad 0:cdf462088d13 71 {
markrad 0:cdf462088d13 72 int ret, n;
markrad 0:cdf462088d13 73
markrad 0:cdf462088d13 74 if( end - *p < 2 )
markrad 0:cdf462088d13 75 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
markrad 0:cdf462088d13 76
markrad 0:cdf462088d13 77 n = ( (*p)[0] << 8 ) | (*p)[1];
markrad 0:cdf462088d13 78 (*p) += 2;
markrad 0:cdf462088d13 79
markrad 0:cdf462088d13 80 if( (int)( end - *p ) < n )
markrad 0:cdf462088d13 81 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
markrad 0:cdf462088d13 82
markrad 0:cdf462088d13 83 if( ( ret = mbedtls_mpi_read_binary( X, *p, n ) ) != 0 )
markrad 0:cdf462088d13 84 return( MBEDTLS_ERR_DHM_READ_PARAMS_FAILED + ret );
markrad 0:cdf462088d13 85
markrad 0:cdf462088d13 86 (*p) += n;
markrad 0:cdf462088d13 87
markrad 0:cdf462088d13 88 return( 0 );
markrad 0:cdf462088d13 89 }
markrad 0:cdf462088d13 90
markrad 0:cdf462088d13 91 /*
markrad 0:cdf462088d13 92 * Verify sanity of parameter with regards to P
markrad 0:cdf462088d13 93 *
markrad 0:cdf462088d13 94 * Parameter should be: 2 <= public_param <= P - 2
markrad 0:cdf462088d13 95 *
markrad 0:cdf462088d13 96 * For more information on the attack, see:
markrad 0:cdf462088d13 97 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
markrad 0:cdf462088d13 98 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
markrad 0:cdf462088d13 99 */
markrad 0:cdf462088d13 100 static int dhm_check_range( const mbedtls_mpi *param, const mbedtls_mpi *P )
markrad 0:cdf462088d13 101 {
markrad 0:cdf462088d13 102 mbedtls_mpi L, U;
markrad 0:cdf462088d13 103 int ret = MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
markrad 0:cdf462088d13 104
markrad 0:cdf462088d13 105 mbedtls_mpi_init( &L ); mbedtls_mpi_init( &U );
markrad 0:cdf462088d13 106
markrad 0:cdf462088d13 107 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &L, 2 ) );
markrad 0:cdf462088d13 108 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &U, P, 2 ) );
markrad 0:cdf462088d13 109
markrad 0:cdf462088d13 110 if( mbedtls_mpi_cmp_mpi( param, &L ) >= 0 &&
markrad 0:cdf462088d13 111 mbedtls_mpi_cmp_mpi( param, &U ) <= 0 )
markrad 0:cdf462088d13 112 {
markrad 0:cdf462088d13 113 ret = 0;
markrad 0:cdf462088d13 114 }
markrad 0:cdf462088d13 115
markrad 0:cdf462088d13 116 cleanup:
markrad 0:cdf462088d13 117 mbedtls_mpi_free( &L ); mbedtls_mpi_free( &U );
markrad 0:cdf462088d13 118 return( ret );
markrad 0:cdf462088d13 119 }
markrad 0:cdf462088d13 120
markrad 0:cdf462088d13 121 void mbedtls_dhm_init( mbedtls_dhm_context *ctx )
markrad 0:cdf462088d13 122 {
markrad 0:cdf462088d13 123 memset( ctx, 0, sizeof( mbedtls_dhm_context ) );
markrad 0:cdf462088d13 124 }
markrad 0:cdf462088d13 125
markrad 0:cdf462088d13 126 /*
markrad 0:cdf462088d13 127 * Parse the ServerKeyExchange parameters
markrad 0:cdf462088d13 128 */
markrad 0:cdf462088d13 129 int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx,
markrad 0:cdf462088d13 130 unsigned char **p,
markrad 0:cdf462088d13 131 const unsigned char *end )
markrad 0:cdf462088d13 132 {
markrad 0:cdf462088d13 133 int ret;
markrad 0:cdf462088d13 134
markrad 0:cdf462088d13 135 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
markrad 0:cdf462088d13 136 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
markrad 0:cdf462088d13 137 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
markrad 0:cdf462088d13 138 return( ret );
markrad 0:cdf462088d13 139
markrad 0:cdf462088d13 140 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
markrad 0:cdf462088d13 141 return( ret );
markrad 0:cdf462088d13 142
markrad 0:cdf462088d13 143 ctx->len = mbedtls_mpi_size( &ctx->P );
markrad 0:cdf462088d13 144
markrad 0:cdf462088d13 145 return( 0 );
markrad 0:cdf462088d13 146 }
markrad 0:cdf462088d13 147
markrad 0:cdf462088d13 148 /*
markrad 0:cdf462088d13 149 * Setup and write the ServerKeyExchange parameters
markrad 0:cdf462088d13 150 */
markrad 0:cdf462088d13 151 int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size,
markrad 0:cdf462088d13 152 unsigned char *output, size_t *olen,
markrad 0:cdf462088d13 153 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 154 void *p_rng )
markrad 0:cdf462088d13 155 {
markrad 0:cdf462088d13 156 int ret, count = 0;
markrad 0:cdf462088d13 157 size_t n1, n2, n3;
markrad 0:cdf462088d13 158 unsigned char *p;
markrad 0:cdf462088d13 159
markrad 0:cdf462088d13 160 if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
markrad 0:cdf462088d13 161 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
markrad 0:cdf462088d13 162
markrad 0:cdf462088d13 163 /*
markrad 0:cdf462088d13 164 * Generate X as large as possible ( < P )
markrad 0:cdf462088d13 165 */
markrad 0:cdf462088d13 166 do
markrad 0:cdf462088d13 167 {
markrad 0:cdf462088d13 168 mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
markrad 0:cdf462088d13 169
markrad 0:cdf462088d13 170 while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
markrad 0:cdf462088d13 171 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
markrad 0:cdf462088d13 172
markrad 0:cdf462088d13 173 if( count++ > 10 )
markrad 0:cdf462088d13 174 return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED );
markrad 0:cdf462088d13 175 }
markrad 0:cdf462088d13 176 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
markrad 0:cdf462088d13 177
markrad 0:cdf462088d13 178 /*
markrad 0:cdf462088d13 179 * Calculate GX = G^X mod P
markrad 0:cdf462088d13 180 */
markrad 0:cdf462088d13 181 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
markrad 0:cdf462088d13 182 &ctx->P , &ctx->RP ) );
markrad 0:cdf462088d13 183
markrad 0:cdf462088d13 184 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
markrad 0:cdf462088d13 185 return( ret );
markrad 0:cdf462088d13 186
markrad 0:cdf462088d13 187 /*
markrad 0:cdf462088d13 188 * export P, G, GX
markrad 0:cdf462088d13 189 */
markrad 0:cdf462088d13 190 #define DHM_MPI_EXPORT(X,n) \
markrad 0:cdf462088d13 191 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, p + 2, n ) ); \
markrad 0:cdf462088d13 192 *p++ = (unsigned char)( n >> 8 ); \
markrad 0:cdf462088d13 193 *p++ = (unsigned char)( n ); p += n;
markrad 0:cdf462088d13 194
markrad 0:cdf462088d13 195 n1 = mbedtls_mpi_size( &ctx->P );
markrad 0:cdf462088d13 196 n2 = mbedtls_mpi_size( &ctx->G );
markrad 0:cdf462088d13 197 n3 = mbedtls_mpi_size( &ctx->GX );
markrad 0:cdf462088d13 198
markrad 0:cdf462088d13 199 p = output;
markrad 0:cdf462088d13 200 DHM_MPI_EXPORT( &ctx->P , n1 );
markrad 0:cdf462088d13 201 DHM_MPI_EXPORT( &ctx->G , n2 );
markrad 0:cdf462088d13 202 DHM_MPI_EXPORT( &ctx->GX, n3 );
markrad 0:cdf462088d13 203
markrad 0:cdf462088d13 204 *olen = p - output;
markrad 0:cdf462088d13 205
markrad 0:cdf462088d13 206 ctx->len = n1;
markrad 0:cdf462088d13 207
markrad 0:cdf462088d13 208 cleanup:
markrad 0:cdf462088d13 209
markrad 0:cdf462088d13 210 if( ret != 0 )
markrad 0:cdf462088d13 211 return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED + ret );
markrad 0:cdf462088d13 212
markrad 0:cdf462088d13 213 return( 0 );
markrad 0:cdf462088d13 214 }
markrad 0:cdf462088d13 215
markrad 0:cdf462088d13 216 /*
markrad 0:cdf462088d13 217 * Import the peer's public value G^Y
markrad 0:cdf462088d13 218 */
markrad 0:cdf462088d13 219 int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx,
markrad 0:cdf462088d13 220 const unsigned char *input, size_t ilen )
markrad 0:cdf462088d13 221 {
markrad 0:cdf462088d13 222 int ret;
markrad 0:cdf462088d13 223
markrad 0:cdf462088d13 224 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
markrad 0:cdf462088d13 225 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
markrad 0:cdf462088d13 226
markrad 0:cdf462088d13 227 if( ( ret = mbedtls_mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
markrad 0:cdf462088d13 228 return( MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED + ret );
markrad 0:cdf462088d13 229
markrad 0:cdf462088d13 230 return( 0 );
markrad 0:cdf462088d13 231 }
markrad 0:cdf462088d13 232
markrad 0:cdf462088d13 233 /*
markrad 0:cdf462088d13 234 * Create own private value X and export G^X
markrad 0:cdf462088d13 235 */
markrad 0:cdf462088d13 236 int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size,
markrad 0:cdf462088d13 237 unsigned char *output, size_t olen,
markrad 0:cdf462088d13 238 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 239 void *p_rng )
markrad 0:cdf462088d13 240 {
markrad 0:cdf462088d13 241 int ret, count = 0;
markrad 0:cdf462088d13 242
markrad 0:cdf462088d13 243 if( ctx == NULL || olen < 1 || olen > ctx->len )
markrad 0:cdf462088d13 244 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
markrad 0:cdf462088d13 245
markrad 0:cdf462088d13 246 if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
markrad 0:cdf462088d13 247 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
markrad 0:cdf462088d13 248
markrad 0:cdf462088d13 249 /*
markrad 0:cdf462088d13 250 * generate X and calculate GX = G^X mod P
markrad 0:cdf462088d13 251 */
markrad 0:cdf462088d13 252 do
markrad 0:cdf462088d13 253 {
markrad 0:cdf462088d13 254 mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
markrad 0:cdf462088d13 255
markrad 0:cdf462088d13 256 while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
markrad 0:cdf462088d13 257 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
markrad 0:cdf462088d13 258
markrad 0:cdf462088d13 259 if( count++ > 10 )
markrad 0:cdf462088d13 260 return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED );
markrad 0:cdf462088d13 261 }
markrad 0:cdf462088d13 262 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
markrad 0:cdf462088d13 263
markrad 0:cdf462088d13 264 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
markrad 0:cdf462088d13 265 &ctx->P , &ctx->RP ) );
markrad 0:cdf462088d13 266
markrad 0:cdf462088d13 267 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
markrad 0:cdf462088d13 268 return( ret );
markrad 0:cdf462088d13 269
markrad 0:cdf462088d13 270 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->GX, output, olen ) );
markrad 0:cdf462088d13 271
markrad 0:cdf462088d13 272 cleanup:
markrad 0:cdf462088d13 273
markrad 0:cdf462088d13 274 if( ret != 0 )
markrad 0:cdf462088d13 275 return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
markrad 0:cdf462088d13 276
markrad 0:cdf462088d13 277 return( 0 );
markrad 0:cdf462088d13 278 }
markrad 0:cdf462088d13 279
markrad 0:cdf462088d13 280 /*
markrad 0:cdf462088d13 281 * Use the blinding method and optimisation suggested in section 10 of:
markrad 0:cdf462088d13 282 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
markrad 0:cdf462088d13 283 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
markrad 0:cdf462088d13 284 * Berlin Heidelberg, 1996. p. 104-113.
markrad 0:cdf462088d13 285 */
markrad 0:cdf462088d13 286 static int dhm_update_blinding( mbedtls_dhm_context *ctx,
markrad 0:cdf462088d13 287 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
markrad 0:cdf462088d13 288 {
markrad 0:cdf462088d13 289 int ret, count;
markrad 0:cdf462088d13 290
markrad 0:cdf462088d13 291 /*
markrad 0:cdf462088d13 292 * Don't use any blinding the first time a particular X is used,
markrad 0:cdf462088d13 293 * but remember it to use blinding next time.
markrad 0:cdf462088d13 294 */
markrad 0:cdf462088d13 295 if( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
markrad 0:cdf462088d13 296 {
markrad 0:cdf462088d13 297 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &ctx->pX, &ctx->X ) );
markrad 0:cdf462088d13 298 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vi, 1 ) );
markrad 0:cdf462088d13 299 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vf, 1 ) );
markrad 0:cdf462088d13 300
markrad 0:cdf462088d13 301 return( 0 );
markrad 0:cdf462088d13 302 }
markrad 0:cdf462088d13 303
markrad 0:cdf462088d13 304 /*
markrad 0:cdf462088d13 305 * Ok, we need blinding. Can we re-use existing values?
markrad 0:cdf462088d13 306 * If yes, just update them by squaring them.
markrad 0:cdf462088d13 307 */
markrad 0:cdf462088d13 308 if( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
markrad 0:cdf462088d13 309 {
markrad 0:cdf462088d13 310 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
markrad 0:cdf462088d13 311 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
markrad 0:cdf462088d13 312
markrad 0:cdf462088d13 313 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
markrad 0:cdf462088d13 314 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
markrad 0:cdf462088d13 315
markrad 0:cdf462088d13 316 return( 0 );
markrad 0:cdf462088d13 317 }
markrad 0:cdf462088d13 318
markrad 0:cdf462088d13 319 /*
markrad 0:cdf462088d13 320 * We need to generate blinding values from scratch
markrad 0:cdf462088d13 321 */
markrad 0:cdf462088d13 322
markrad 0:cdf462088d13 323 /* Vi = random( 2, P-1 ) */
markrad 0:cdf462088d13 324 count = 0;
markrad 0:cdf462088d13 325 do
markrad 0:cdf462088d13 326 {
markrad 0:cdf462088d13 327 mbedtls_mpi_fill_random( &ctx->Vi, mbedtls_mpi_size( &ctx->P ), f_rng, p_rng );
markrad 0:cdf462088d13 328
markrad 0:cdf462088d13 329 while( mbedtls_mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
markrad 0:cdf462088d13 330 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->Vi, 1 ) );
markrad 0:cdf462088d13 331
markrad 0:cdf462088d13 332 if( count++ > 10 )
markrad 0:cdf462088d13 333 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
markrad 0:cdf462088d13 334 }
markrad 0:cdf462088d13 335 while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
markrad 0:cdf462088d13 336
markrad 0:cdf462088d13 337 /* Vf = Vi^-X mod P */
markrad 0:cdf462088d13 338 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
markrad 0:cdf462088d13 339 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
markrad 0:cdf462088d13 340
markrad 0:cdf462088d13 341 cleanup:
markrad 0:cdf462088d13 342 return( ret );
markrad 0:cdf462088d13 343 }
markrad 0:cdf462088d13 344
markrad 0:cdf462088d13 345 /*
markrad 0:cdf462088d13 346 * Derive and export the shared secret (G^Y)^X mod P
markrad 0:cdf462088d13 347 */
markrad 0:cdf462088d13 348 int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx,
markrad 0:cdf462088d13 349 unsigned char *output, size_t output_size, size_t *olen,
markrad 0:cdf462088d13 350 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 351 void *p_rng )
markrad 0:cdf462088d13 352 {
markrad 0:cdf462088d13 353 int ret;
markrad 0:cdf462088d13 354 mbedtls_mpi GYb;
markrad 0:cdf462088d13 355
markrad 0:cdf462088d13 356 if( ctx == NULL || output_size < ctx->len )
markrad 0:cdf462088d13 357 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
markrad 0:cdf462088d13 358
markrad 0:cdf462088d13 359 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
markrad 0:cdf462088d13 360 return( ret );
markrad 0:cdf462088d13 361
markrad 0:cdf462088d13 362 mbedtls_mpi_init( &GYb );
markrad 0:cdf462088d13 363
markrad 0:cdf462088d13 364 /* Blind peer's value */
markrad 0:cdf462088d13 365 if( f_rng != NULL )
markrad 0:cdf462088d13 366 {
markrad 0:cdf462088d13 367 MBEDTLS_MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
markrad 0:cdf462088d13 368 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
markrad 0:cdf462088d13 369 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
markrad 0:cdf462088d13 370 }
markrad 0:cdf462088d13 371 else
markrad 0:cdf462088d13 372 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &GYb, &ctx->GY ) );
markrad 0:cdf462088d13 373
markrad 0:cdf462088d13 374 /* Do modular exponentiation */
markrad 0:cdf462088d13 375 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
markrad 0:cdf462088d13 376 &ctx->P, &ctx->RP ) );
markrad 0:cdf462088d13 377
markrad 0:cdf462088d13 378 /* Unblind secret value */
markrad 0:cdf462088d13 379 if( f_rng != NULL )
markrad 0:cdf462088d13 380 {
markrad 0:cdf462088d13 381 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
markrad 0:cdf462088d13 382 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
markrad 0:cdf462088d13 383 }
markrad 0:cdf462088d13 384
markrad 0:cdf462088d13 385 *olen = mbedtls_mpi_size( &ctx->K );
markrad 0:cdf462088d13 386
markrad 0:cdf462088d13 387 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->K, output, *olen ) );
markrad 0:cdf462088d13 388
markrad 0:cdf462088d13 389 cleanup:
markrad 0:cdf462088d13 390 mbedtls_mpi_free( &GYb );
markrad 0:cdf462088d13 391
markrad 0:cdf462088d13 392 if( ret != 0 )
markrad 0:cdf462088d13 393 return( MBEDTLS_ERR_DHM_CALC_SECRET_FAILED + ret );
markrad 0:cdf462088d13 394
markrad 0:cdf462088d13 395 return( 0 );
markrad 0:cdf462088d13 396 }
markrad 0:cdf462088d13 397
markrad 0:cdf462088d13 398 /*
markrad 0:cdf462088d13 399 * Free the components of a DHM key
markrad 0:cdf462088d13 400 */
markrad 0:cdf462088d13 401 void mbedtls_dhm_free( mbedtls_dhm_context *ctx )
markrad 0:cdf462088d13 402 {
markrad 0:cdf462088d13 403 mbedtls_mpi_free( &ctx->pX); mbedtls_mpi_free( &ctx->Vf ); mbedtls_mpi_free( &ctx->Vi );
markrad 0:cdf462088d13 404 mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->K ); mbedtls_mpi_free( &ctx->GY );
markrad 0:cdf462088d13 405 mbedtls_mpi_free( &ctx->GX ); mbedtls_mpi_free( &ctx->X ); mbedtls_mpi_free( &ctx->G );
markrad 0:cdf462088d13 406 mbedtls_mpi_free( &ctx->P );
markrad 0:cdf462088d13 407
markrad 0:cdf462088d13 408 mbedtls_zeroize( ctx, sizeof( mbedtls_dhm_context ) );
markrad 0:cdf462088d13 409 }
markrad 0:cdf462088d13 410
markrad 0:cdf462088d13 411 #if defined(MBEDTLS_ASN1_PARSE_C)
markrad 0:cdf462088d13 412 /*
markrad 0:cdf462088d13 413 * Parse DHM parameters
markrad 0:cdf462088d13 414 */
markrad 0:cdf462088d13 415 int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin,
markrad 0:cdf462088d13 416 size_t dhminlen )
markrad 0:cdf462088d13 417 {
markrad 0:cdf462088d13 418 int ret;
markrad 0:cdf462088d13 419 size_t len;
markrad 0:cdf462088d13 420 unsigned char *p, *end;
markrad 0:cdf462088d13 421 #if defined(MBEDTLS_PEM_PARSE_C)
markrad 0:cdf462088d13 422 mbedtls_pem_context pem;
markrad 0:cdf462088d13 423
markrad 0:cdf462088d13 424 mbedtls_pem_init( &pem );
markrad 0:cdf462088d13 425
markrad 0:cdf462088d13 426 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
markrad 0:cdf462088d13 427 if( dhminlen == 0 || dhmin[dhminlen - 1] != '\0' )
markrad 0:cdf462088d13 428 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
markrad 0:cdf462088d13 429 else
markrad 0:cdf462088d13 430 ret = mbedtls_pem_read_buffer( &pem,
markrad 0:cdf462088d13 431 "-----BEGIN DH PARAMETERS-----",
markrad 0:cdf462088d13 432 "-----END DH PARAMETERS-----",
markrad 0:cdf462088d13 433 dhmin, NULL, 0, &dhminlen );
markrad 0:cdf462088d13 434
markrad 0:cdf462088d13 435 if( ret == 0 )
markrad 0:cdf462088d13 436 {
markrad 0:cdf462088d13 437 /*
markrad 0:cdf462088d13 438 * Was PEM encoded
markrad 0:cdf462088d13 439 */
markrad 0:cdf462088d13 440 dhminlen = pem.buflen;
markrad 0:cdf462088d13 441 }
markrad 0:cdf462088d13 442 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
markrad 0:cdf462088d13 443 goto exit;
markrad 0:cdf462088d13 444
markrad 0:cdf462088d13 445 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
markrad 0:cdf462088d13 446 #else
markrad 0:cdf462088d13 447 p = (unsigned char *) dhmin;
markrad 0:cdf462088d13 448 #endif /* MBEDTLS_PEM_PARSE_C */
markrad 0:cdf462088d13 449 end = p + dhminlen;
markrad 0:cdf462088d13 450
markrad 0:cdf462088d13 451 /*
markrad 0:cdf462088d13 452 * DHParams ::= SEQUENCE {
markrad 0:cdf462088d13 453 * prime INTEGER, -- P
markrad 0:cdf462088d13 454 * generator INTEGER, -- g
markrad 0:cdf462088d13 455 * privateValueLength INTEGER OPTIONAL
markrad 0:cdf462088d13 456 * }
markrad 0:cdf462088d13 457 */
markrad 0:cdf462088d13 458 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
markrad 0:cdf462088d13 459 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
markrad 0:cdf462088d13 460 {
markrad 0:cdf462088d13 461 ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
markrad 0:cdf462088d13 462 goto exit;
markrad 0:cdf462088d13 463 }
markrad 0:cdf462088d13 464
markrad 0:cdf462088d13 465 end = p + len;
markrad 0:cdf462088d13 466
markrad 0:cdf462088d13 467 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
markrad 0:cdf462088d13 468 ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
markrad 0:cdf462088d13 469 {
markrad 0:cdf462088d13 470 ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
markrad 0:cdf462088d13 471 goto exit;
markrad 0:cdf462088d13 472 }
markrad 0:cdf462088d13 473
markrad 0:cdf462088d13 474 if( p != end )
markrad 0:cdf462088d13 475 {
markrad 0:cdf462088d13 476 /* This might be the optional privateValueLength.
markrad 0:cdf462088d13 477 * If so, we can cleanly discard it */
markrad 0:cdf462088d13 478 mbedtls_mpi rec;
markrad 0:cdf462088d13 479 mbedtls_mpi_init( &rec );
markrad 0:cdf462088d13 480 ret = mbedtls_asn1_get_mpi( &p, end, &rec );
markrad 0:cdf462088d13 481 mbedtls_mpi_free( &rec );
markrad 0:cdf462088d13 482 if ( ret != 0 )
markrad 0:cdf462088d13 483 {
markrad 0:cdf462088d13 484 ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
markrad 0:cdf462088d13 485 goto exit;
markrad 0:cdf462088d13 486 }
markrad 0:cdf462088d13 487 if ( p != end )
markrad 0:cdf462088d13 488 {
markrad 0:cdf462088d13 489 ret = MBEDTLS_ERR_DHM_INVALID_FORMAT +
markrad 0:cdf462088d13 490 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
markrad 0:cdf462088d13 491 goto exit;
markrad 0:cdf462088d13 492 }
markrad 0:cdf462088d13 493 }
markrad 0:cdf462088d13 494
markrad 0:cdf462088d13 495 ret = 0;
markrad 0:cdf462088d13 496
markrad 0:cdf462088d13 497 dhm->len = mbedtls_mpi_size( &dhm->P );
markrad 0:cdf462088d13 498
markrad 0:cdf462088d13 499 exit:
markrad 0:cdf462088d13 500 #if defined(MBEDTLS_PEM_PARSE_C)
markrad 0:cdf462088d13 501 mbedtls_pem_free( &pem );
markrad 0:cdf462088d13 502 #endif
markrad 0:cdf462088d13 503 if( ret != 0 )
markrad 0:cdf462088d13 504 mbedtls_dhm_free( dhm );
markrad 0:cdf462088d13 505
markrad 0:cdf462088d13 506 return( ret );
markrad 0:cdf462088d13 507 }
markrad 0:cdf462088d13 508
markrad 0:cdf462088d13 509 #if defined(MBEDTLS_FS_IO)
markrad 0:cdf462088d13 510 /*
markrad 0:cdf462088d13 511 * Load all data from a file into a given buffer.
markrad 0:cdf462088d13 512 *
markrad 0:cdf462088d13 513 * The file is expected to contain either PEM or DER encoded data.
markrad 0:cdf462088d13 514 * A terminating null byte is always appended. It is included in the announced
markrad 0:cdf462088d13 515 * length only if the data looks like it is PEM encoded.
markrad 0:cdf462088d13 516 */
markrad 0:cdf462088d13 517 static int load_file( const char *path, unsigned char **buf, size_t *n )
markrad 0:cdf462088d13 518 {
markrad 0:cdf462088d13 519 FILE *f;
markrad 0:cdf462088d13 520 long size;
markrad 0:cdf462088d13 521
markrad 0:cdf462088d13 522 if( ( f = fopen( path, "rb" ) ) == NULL )
markrad 0:cdf462088d13 523 return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
markrad 0:cdf462088d13 524
markrad 0:cdf462088d13 525 fseek( f, 0, SEEK_END );
markrad 0:cdf462088d13 526 if( ( size = ftell( f ) ) == -1 )
markrad 0:cdf462088d13 527 {
markrad 0:cdf462088d13 528 fclose( f );
markrad 0:cdf462088d13 529 return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
markrad 0:cdf462088d13 530 }
markrad 0:cdf462088d13 531 fseek( f, 0, SEEK_SET );
markrad 0:cdf462088d13 532
markrad 0:cdf462088d13 533 *n = (size_t) size;
markrad 0:cdf462088d13 534
markrad 0:cdf462088d13 535 if( *n + 1 == 0 ||
markrad 0:cdf462088d13 536 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
markrad 0:cdf462088d13 537 {
markrad 0:cdf462088d13 538 fclose( f );
markrad 0:cdf462088d13 539 return( MBEDTLS_ERR_DHM_ALLOC_FAILED );
markrad 0:cdf462088d13 540 }
markrad 0:cdf462088d13 541
markrad 0:cdf462088d13 542 if( fread( *buf, 1, *n, f ) != *n )
markrad 0:cdf462088d13 543 {
markrad 0:cdf462088d13 544 fclose( f );
markrad 0:cdf462088d13 545 mbedtls_free( *buf );
markrad 0:cdf462088d13 546 return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
markrad 0:cdf462088d13 547 }
markrad 0:cdf462088d13 548
markrad 0:cdf462088d13 549 fclose( f );
markrad 0:cdf462088d13 550
markrad 0:cdf462088d13 551 (*buf)[*n] = '\0';
markrad 0:cdf462088d13 552
markrad 0:cdf462088d13 553 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
markrad 0:cdf462088d13 554 ++*n;
markrad 0:cdf462088d13 555
markrad 0:cdf462088d13 556 return( 0 );
markrad 0:cdf462088d13 557 }
markrad 0:cdf462088d13 558
markrad 0:cdf462088d13 559 /*
markrad 0:cdf462088d13 560 * Load and parse DHM parameters
markrad 0:cdf462088d13 561 */
markrad 0:cdf462088d13 562 int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path )
markrad 0:cdf462088d13 563 {
markrad 0:cdf462088d13 564 int ret;
markrad 0:cdf462088d13 565 size_t n;
markrad 0:cdf462088d13 566 unsigned char *buf;
markrad 0:cdf462088d13 567
markrad 0:cdf462088d13 568 if( ( ret = load_file( path, &buf, &n ) ) != 0 )
markrad 0:cdf462088d13 569 return( ret );
markrad 0:cdf462088d13 570
markrad 0:cdf462088d13 571 ret = mbedtls_dhm_parse_dhm( dhm, buf, n );
markrad 0:cdf462088d13 572
markrad 0:cdf462088d13 573 mbedtls_zeroize( buf, n );
markrad 0:cdf462088d13 574 mbedtls_free( buf );
markrad 0:cdf462088d13 575
markrad 0:cdf462088d13 576 return( ret );
markrad 0:cdf462088d13 577 }
markrad 0:cdf462088d13 578 #endif /* MBEDTLS_FS_IO */
markrad 0:cdf462088d13 579 #endif /* MBEDTLS_ASN1_PARSE_C */
markrad 0:cdf462088d13 580
markrad 0:cdf462088d13 581 #if defined(MBEDTLS_SELF_TEST)
markrad 0:cdf462088d13 582
markrad 0:cdf462088d13 583 static const char mbedtls_test_dhm_params[] =
markrad 0:cdf462088d13 584 "-----BEGIN DH PARAMETERS-----\r\n"
markrad 0:cdf462088d13 585 "MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh\r\n"
markrad 0:cdf462088d13 586 "1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32\r\n"
markrad 0:cdf462088d13 587 "9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC\r\n"
markrad 0:cdf462088d13 588 "-----END DH PARAMETERS-----\r\n";
markrad 0:cdf462088d13 589
markrad 0:cdf462088d13 590 static const size_t mbedtls_test_dhm_params_len = sizeof( mbedtls_test_dhm_params );
markrad 0:cdf462088d13 591
markrad 0:cdf462088d13 592 /*
markrad 0:cdf462088d13 593 * Checkup routine
markrad 0:cdf462088d13 594 */
markrad 0:cdf462088d13 595 int mbedtls_dhm_self_test( int verbose )
markrad 0:cdf462088d13 596 {
markrad 0:cdf462088d13 597 int ret;
markrad 0:cdf462088d13 598 mbedtls_dhm_context dhm;
markrad 0:cdf462088d13 599
markrad 0:cdf462088d13 600 mbedtls_dhm_init( &dhm );
markrad 0:cdf462088d13 601
markrad 0:cdf462088d13 602 if( verbose != 0 )
markrad 0:cdf462088d13 603 mbedtls_printf( " DHM parameter load: " );
markrad 0:cdf462088d13 604
markrad 0:cdf462088d13 605 if( ( ret = mbedtls_dhm_parse_dhm( &dhm,
markrad 0:cdf462088d13 606 (const unsigned char *) mbedtls_test_dhm_params,
markrad 0:cdf462088d13 607 mbedtls_test_dhm_params_len ) ) != 0 )
markrad 0:cdf462088d13 608 {
markrad 0:cdf462088d13 609 if( verbose != 0 )
markrad 0:cdf462088d13 610 mbedtls_printf( "failed\n" );
markrad 0:cdf462088d13 611
markrad 0:cdf462088d13 612 ret = 1;
markrad 0:cdf462088d13 613 goto exit;
markrad 0:cdf462088d13 614 }
markrad 0:cdf462088d13 615
markrad 0:cdf462088d13 616 if( verbose != 0 )
markrad 0:cdf462088d13 617 mbedtls_printf( "passed\n\n" );
markrad 0:cdf462088d13 618
markrad 0:cdf462088d13 619 exit:
markrad 0:cdf462088d13 620 mbedtls_dhm_free( &dhm );
markrad 0:cdf462088d13 621
markrad 0:cdf462088d13 622 return( ret );
markrad 0:cdf462088d13 623 }
markrad 0:cdf462088d13 624
markrad 0:cdf462088d13 625 #endif /* MBEDTLS_SELF_TEST */
markrad 0:cdf462088d13 626
markrad 0:cdf462088d13 627 #endif /* MBEDTLS_DHM_C */