Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

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