Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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