33 #if defined(POLARSSL_DHM_C)
37 #if defined(POLARSSL_PEM_PARSE_C)
41 #if defined(POLARSSL_ASN1_PARSE_C)
45 #if defined(POLARSSL_MEMORY_C)
49 #define polarssl_malloc malloc
50 #define polarssl_free free
56 static int dhm_read_bignum(
mpi *X,
58 const unsigned char *end )
65 n = ( (*p)[0] << 8 ) | (*p)[1];
68 if( (
int)( end - *p ) < n )
88 static int dhm_check_range(
const mpi *param,
const mpi *P )
113 const unsigned char *end )
119 if( ( ret = dhm_read_bignum( &ctx->
P, p, end ) ) != 0 ||
120 ( ret = dhm_read_bignum( &ctx->
G, p, end ) ) != 0 ||
121 ( ret = dhm_read_bignum( &ctx->
GY, p, end ) ) != 0 )
124 if( ( ret = dhm_check_range( &ctx->
GY, &ctx->
P ) ) != 0 )
136 unsigned char *output,
size_t *olen,
137 int (*f_rng)(
void *,
unsigned char *,
size_t),
160 while( dhm_check_range( &ctx->
X, &ctx->
P ) != 0 );
166 &ctx->
P , &ctx->
RP ) );
168 if( ( ret = dhm_check_range( &ctx->
GX, &ctx->
P ) ) != 0 )
174 #define DHM_MPI_EXPORT(X,n) \
175 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
176 *p++ = (unsigned char)( n >> 8 ); \
177 *p++ = (unsigned char)( n ); p += n;
184 DHM_MPI_EXPORT( &ctx->
P , n1 );
185 DHM_MPI_EXPORT( &ctx->
G , n2 );
186 DHM_MPI_EXPORT( &ctx->
GX, n3 );
204 const unsigned char *input,
size_t ilen )
208 if( ctx == NULL || ilen < 1 || ilen > ctx->
len )
221 unsigned char *output,
size_t olen,
222 int (*f_rng)(
void *,
unsigned char *,
size_t),
227 if( ctx == NULL || olen < 1 || olen > ctx->
len )
246 while( dhm_check_range( &ctx->
X, &ctx->
P ) != 0 );
249 &ctx->
P , &ctx->
RP ) );
251 if( ( ret = dhm_check_range( &ctx->
GX, &ctx->
P ) ) != 0 )
271 int (*f_rng)(
void *,
unsigned char *,
size_t),
void *p_rng )
333 unsigned char *output,
size_t *olen,
334 int (*f_rng)(
void *,
unsigned char *,
size_t),
340 if( ctx == NULL || *olen < ctx->len )
343 if( ( ret = dhm_check_range( &ctx->
GY, &ctx->
P ) ) != 0 )
351 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
360 &ctx->
P, &ctx->
RP ) );
395 #if defined(POLARSSL_ASN1_PARSE_C)
403 unsigned char *p, *end;
404 #if defined(POLARSSL_PEM_PARSE_C)
410 ret = pem_read_buffer( &pem,
411 "-----BEGIN DH PARAMETERS-----",
412 "-----END DH PARAMETERS-----",
413 dhmin, NULL, 0, &dhminlen );
420 dhminlen = pem.buflen;
425 p = ( ret == 0 ) ? pem.buf : (
unsigned char *) dhmin;
427 p = (
unsigned char *) dhmin;
463 #if defined(POLARSSL_PEM_PARSE_C)
472 #if defined(POLARSSL_FS_IO)
476 static int load_file(
const char *path,
unsigned char **buf,
size_t *n )
481 if( ( f = fopen( path,
"rb" ) ) == NULL )
484 fseek( f, 0, SEEK_END );
485 if( ( size = ftell( f ) ) == -1 )
490 fseek( f, 0, SEEK_SET );
501 if( fread( *buf, 1, *n, f ) != *n )
524 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
529 memset( buf, 0, n + 1 );
537 #if defined(POLARSSL_SELF_TEST)
546 #if defined(POLARSSL_CERTS_C)
551 printf(
" DHM parameter load: " );
553 if( ( ret =
dhm_parse_dhm( &dhm, (
const unsigned char *) test_dhm_params,
554 strlen( test_dhm_params ) ) ) != 0 )
557 printf(
"failed\n" );
563 printf(
"passed\n\n" );
int mpi_cmp_int(const mpi *X, t_sint z)
Compare signed values.
#define POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED
Making of the public value failed.
#define POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED
Making of the DHM parameters failed.
#define POLARSSL_ERR_DHM_INVALID_FORMAT
The ASN.1 data is not formatted correctly.
void *(* polarssl_malloc)(size_t len)
#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH
Actual length differs from expected length.
#define POLARSSL_ERR_DHM_CALC_SECRET_FAILED
Calculation of the DHM secret failed.
int mpi_fill_random(mpi *X, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Fill an MPI X with size bytes of random.
#define POLARSSL_ERR_DHM_MALLOC_FAILED
Allocation of memory failed.
int dhm_self_test(int verbose)
Checkup routine.
Configuration options (set of defines)
int mpi_lset(mpi *X, t_sint z)
Set value from integer.
void mpi_init(mpi *X)
Initialize one MPI.
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
int mpi_shift_r(mpi *X, size_t count)
Right-shift: X >>= count.
int dhm_read_params(dhm_context *ctx, unsigned char **p, const unsigned char *end)
Parse the ServerKeyExchange parameters.
#define POLARSSL_ERR_DHM_READ_PUBLIC_FAILED
Reading of the public values failed.
Privacy Enhanced Mail (PEM) decoding.
#define POLARSSL_ERR_DHM_READ_PARAMS_FAILED
Reading of the DHM parameters failed.
void(* polarssl_free)(void *ptr)
int mpi_inv_mod(mpi *X, const mpi *A, const mpi *N)
Modular inverse: X = A^-1 mod N.
void mpi_free(mpi *X)
Unallocate one MPI.
Diffie-Hellman-Merkle key exchange.
int mpi_exp_mod(mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR)
Sliding-window exponentiation: X = A^E mod N.
int mpi_read_binary(mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, big endian.
Sample certificates and DHM parameters for testing.
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.
size_t mpi_size(const mpi *X)
Return the total size in bytes.
#define POLARSSL_ERR_DHM_FILE_IO_ERROR
Read/write of file failed.
int mpi_copy(mpi *X, const mpi *Y)
Copy the contents of Y into X.
#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE
Unavailable feature, e.g.
int mpi_mod_mpi(mpi *R, const mpi *A, const mpi *B)
Modulo: R = A mod B.
int asn1_get_tag(unsigned char **p, const unsigned char *end, size_t *len, int tag)
Get the tag and length of the tag.
int mpi_write_binary(const mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, big endian.
int dhm_parse_dhm(dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen)
Parse DHM parameters.
void dhm_free(dhm_context *ctx)
Free the components of a DHM key.
#define POLARSSL_ERR_DHM_BAD_INPUT_DATA
Bad input parameters to function.
#define POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT
No PEM header or footer found.
int mpi_mul_mpi(mpi *X, const mpi *A, const mpi *B)
Baseline multiplication: X = A * B.
int asn1_get_mpi(unsigned char **p, const unsigned char *end, mpi *X)
Retrieve a MPI value from an integer ASN.1 tag.
int mpi_sub_int(mpi *X, const mpi *A, t_sint b)
Signed substraction: X = A - b.
int dhm_parse_dhmfile(dhm_context *dhm, const char *path)
Load and parse DHM 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.
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE
The input arguments are not acceptable.
int dhm_read_public(dhm_context *ctx, const unsigned char *input, size_t ilen)
Import the peer's public value G^Y.
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.