BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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