dhm.c File Reference

#include "polarssl/config.h"
#include "polarssl/dhm.h"
#include "polarssl/pem.h"
#include "polarssl/asn1.h"
#include <stdlib.h>
#include "polarssl/certs.h"
Include dependency graph for dhm.c:

Go to the source code of this file.

Defines

#define polarssl_malloc   malloc
#define polarssl_free   free
#define DHM_MPI_EXPORT(X, n)

Functions

static int dhm_read_bignum (mpi *X, unsigned char **p, const unsigned char *end)
static int dhm_check_range (const mpi *param, const mpi *P)
int dhm_read_params (dhm_context *ctx, unsigned char **p, const unsigned char *end)
 Parse the ServerKeyExchange parameters.
int dhm_make_params (dhm_context *ctx, int x_size, unsigned char *output, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
 Setup and write the ServerKeyExchange parameters.
int dhm_read_public (dhm_context *ctx, const unsigned char *input, size_t ilen)
 Import the peer's public value G^Y.
int dhm_make_public (dhm_context *ctx, int x_size, unsigned char *output, size_t olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
 Create own private value X and export G^X.
static int dhm_update_blinding (dhm_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
int dhm_calc_secret (dhm_context *ctx, unsigned char *output, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
 Derive and export the shared secret (G^Y)^X mod P.
void dhm_free (dhm_context *ctx)
 Free the components of a DHM key.
int dhm_parse_dhm (dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen)
 Parse DHM parameters.
static int load_file (const char *path, unsigned char **buf, size_t *n)
int dhm_parse_dhmfile (dhm_context *dhm, const char *path)
 Load and parse DHM parameters.
int dhm_self_test (int verbose)
 Checkup routine.

Define Documentation

#define DHM_MPI_EXPORT ( X,
 ) 
Value:
MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
    *p++ = (unsigned char)( n >> 8 );           \
    *p++ = (unsigned char)( n      ); p += n;

Referenced by dhm_make_params().

#define polarssl_free   free

Definition at line 50 of file dhm.c.

#define polarssl_malloc   malloc

Definition at line 49 of file dhm.c.


Function Documentation

int dhm_calc_secret ( dhm_context ctx,
unsigned char *  output,
size_t *  olen,
int(*)(void *, unsigned char *, size_t)  f_rng,
void *  p_rng 
)

Derive and export the shared secret (G^Y)^X mod P.

Parameters:
ctx DHM context
output destination buffer
olen number of chars written
f_rng RNG function, for blinding purposes
p_rng RNG parameter
Returns:
0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Note:
If non-NULL, f_rng is used to blind the input as countermeasure against timing attacks. Blinding is automatically used if and only if our secret value X is re-used and costs nothing otherwise, so it is recommended to always pass a non-NULL f_rng argument.

Definition at line 332 of file dhm.c.

References dhm_check_range(), dhm_update_blinding(), dhm_context::GY, dhm_context::K, MPI_CHK, mpi_copy(), mpi_exp_mod(), mpi_free(), mpi_init(), mpi_mod_mpi(), mpi_mul_mpi(), mpi_size(), mpi_write_binary(), dhm_context::P, POLARSSL_ERR_DHM_BAD_INPUT_DATA, POLARSSL_ERR_DHM_CALC_SECRET_FAILED, dhm_context::RP, dhm_context::Vf, dhm_context::Vi, and dhm_context::X.

Referenced by ssl_parse_client_key_exchange(), ssl_psk_derive_premaster(), ssl_write_client_key_exchange(), and test_suite_dhm_do_dhm().

static int dhm_check_range ( const mpi param,
const mpi P 
) [static]
void dhm_free ( dhm_context ctx  ) 
int dhm_make_params ( dhm_context ctx,
int  x_size,
unsigned char *  output,
size_t *  olen,
int(*)(void *, unsigned char *, size_t)  f_rng,
void *  p_rng 
)

Setup and write the ServerKeyExchange parameters.

Parameters:
ctx DHM context
x_size private value size in bytes
output destination buffer
olen number of chars written
f_rng RNG function
p_rng RNG parameter
Note:
This function assumes that ctx->P and ctx->G have already been properly set (for example using mpi_read_string or mpi_read_binary).
Returns:
0 if successful, or an POLARSSL_ERR_DHM_XXX error code

Definition at line 135 of file dhm.c.

References dhm_check_range(), DHM_MPI_EXPORT, dhm_context::G, dhm_context::GX, dhm_context::len, MPI_CHK, mpi_cmp_int(), mpi_cmp_mpi(), mpi_exp_mod(), mpi_fill_random(), mpi_shift_r(), mpi_size(), dhm_context::P, POLARSSL_ERR_DHM_BAD_INPUT_DATA, POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED, dhm_context::RP, and dhm_context::X.

Referenced by ssl_write_server_key_exchange(), and test_suite_dhm_do_dhm().

int dhm_make_public ( dhm_context ctx,
int  x_size,
unsigned char *  output,
size_t  olen,
int(*)(void *, unsigned char *, size_t)  f_rng,
void *  p_rng 
)

Create own private value X and export G^X.

Parameters:
ctx DHM context
x_size private value size in bytes
output destination buffer
olen must be equal to ctx->P.len
f_rng RNG function
p_rng RNG parameter
Returns:
0 if successful, or an POLARSSL_ERR_DHM_XXX error code

Definition at line 220 of file dhm.c.

References dhm_check_range(), dhm_context::G, dhm_context::GX, dhm_context::len, MPI_CHK, mpi_cmp_int(), mpi_cmp_mpi(), mpi_exp_mod(), mpi_fill_random(), mpi_shift_r(), mpi_write_binary(), dhm_context::P, POLARSSL_ERR_DHM_BAD_INPUT_DATA, POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED, dhm_context::RP, and dhm_context::X.

Referenced by ssl_write_client_key_exchange(), and test_suite_dhm_do_dhm().

static int dhm_read_bignum ( mpi X,
unsigned char **  p,
const unsigned char *  end 
) [static]
int dhm_read_params ( dhm_context ctx,
unsigned char **  p,
const unsigned char *  end 
)

Parse the ServerKeyExchange parameters.

Parameters:
ctx DHM context
p &(start of input buffer)
end end of buffer
Returns:
0 if successful, or an POLARSSL_ERR_DHM_XXX error code

Definition at line 111 of file dhm.c.

References dhm_check_range(), dhm_free(), dhm_read_bignum(), dhm_context::G, dhm_context::GY, dhm_context::len, mpi_size(), and dhm_context::P.

Referenced by ssl_parse_server_dh_params(), and test_suite_dhm_do_dhm().

int dhm_read_public ( dhm_context ctx,
const unsigned char *  input,
size_t  ilen 
)

Import the peer's public value G^Y.

Parameters:
ctx DHM context
input input buffer
ilen size of buffer
Returns:
0 if successful, or an POLARSSL_ERR_DHM_XXX error code

Definition at line 203 of file dhm.c.

References dhm_context::GY, dhm_context::len, mpi_read_binary(), POLARSSL_ERR_DHM_BAD_INPUT_DATA, and POLARSSL_ERR_DHM_READ_PUBLIC_FAILED.

Referenced by ssl_parse_client_dh_public(), and test_suite_dhm_do_dhm().

int dhm_self_test ( int  verbose  ) 

Checkup routine.

Returns:
0 if successful, or 1 if the test failed

Definition at line 544 of file dhm.c.

References dhm_free(), dhm_parse_dhm(), POLARSSL_ERR_X509_FEATURE_UNAVAILABLE, and test_dhm_params.

Referenced by test_suite_dhm_selftest().

static int dhm_update_blinding ( dhm_context ctx,
int(*)(void *, unsigned char *, size_t)  f_rng,
void *  p_rng 
) [static]
static int load_file ( const char *  path,
unsigned char **  buf,
size_t *  n 
) [static]

Generated on 9 Apr 2014 for PolarSSL v1.3.2 by  doxygen 1.6.1