31 #if !defined(POLARSSL_CONFIG_FILE)
34 #include POLARSSL_CONFIG_FILE
37 #if defined(POLARSSL_DHM_C)
41 #if defined(POLARSSL_PEM_PARSE_C)
45 #if defined(POLARSSL_ASN1_PARSE_C)
49 #if defined(POLARSSL_PLATFORM_C)
53 #define polarssl_printf printf
54 #define polarssl_malloc malloc
55 #define polarssl_free free
61 static int dhm_read_bignum(
mpi *X,
63 const unsigned char *end )
70 n = ( (*p)[0] << 8 ) | (*p)[1];
73 if( (
int)( end - *p ) < n )
93 static int dhm_check_range(
const mpi *param,
const mpi *P )
119 const unsigned char *end )
125 if( ( ret = dhm_read_bignum( &ctx->
P, p, end ) ) != 0 ||
126 ( ret = dhm_read_bignum( &ctx->
G, p, end ) ) != 0 ||
127 ( ret = dhm_read_bignum( &ctx->
GY, p, end ) ) != 0 )
130 if( ( ret = dhm_check_range( &ctx->
GY, &ctx->
P ) ) != 0 )
142 unsigned char *output,
size_t *olen,
143 int (*f_rng)(
void *,
unsigned char *,
size_t),
166 while( dhm_check_range( &ctx->
X, &ctx->
P ) != 0 );
172 &ctx->
P , &ctx->
RP ) );
174 if( ( ret = dhm_check_range( &ctx->
GX, &ctx->
P ) ) != 0 )
180 #define DHM_MPI_EXPORT(X,n) \
181 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
182 *p++ = (unsigned char)( n >> 8 ); \
183 *p++ = (unsigned char)( n ); p += n;
190 DHM_MPI_EXPORT( &ctx->
P , n1 );
191 DHM_MPI_EXPORT( &ctx->
G , n2 );
192 DHM_MPI_EXPORT( &ctx->
GX, n3 );
210 const unsigned char *input,
size_t ilen )
214 if( ctx == NULL || ilen < 1 || ilen > ctx->
len )
227 unsigned char *output,
size_t olen,
228 int (*f_rng)(
void *,
unsigned char *,
size_t),
233 if( ctx == NULL || olen < 1 || olen > ctx->
len )
252 while( dhm_check_range( &ctx->
X, &ctx->
P ) != 0 );
255 &ctx->
P , &ctx->
RP ) );
257 if( ( ret = dhm_check_range( &ctx->
GX, &ctx->
P ) ) != 0 )
277 int (*f_rng)(
void *,
unsigned char *,
size_t),
void *p_rng )
339 unsigned char *output,
size_t *olen,
340 int (*f_rng)(
void *,
unsigned char *,
size_t),
346 if( ctx == NULL || *olen < ctx->len )
349 if( ( ret = dhm_check_range( &ctx->
GY, &ctx->
P ) ) != 0 )
357 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
366 &ctx->
P, &ctx->
RP ) );
401 #if defined(POLARSSL_ASN1_PARSE_C)
410 unsigned char *p, *end;
411 #if defined(POLARSSL_PEM_PARSE_C)
417 ret = pem_read_buffer( &pem,
418 "-----BEGIN DH PARAMETERS-----",
419 "-----END DH PARAMETERS-----",
420 dhmin, NULL, 0, &dhminlen );
427 dhminlen = pem.buflen;
432 p = ( ret == 0 ) ? pem.buf : (
unsigned char *) dhmin;
434 p = (
unsigned char *) dhmin;
472 #if defined(POLARSSL_PEM_PARSE_C)
481 #if defined(POLARSSL_FS_IO)
485 static int load_file(
const char *path,
unsigned char **buf,
size_t *n )
490 if( ( f = fopen( path,
"rb" ) ) == NULL )
493 fseek( f, 0, SEEK_END );
494 if( ( size = ftell( f ) ) == -1 )
499 fseek( f, 0, SEEK_SET );
510 if( fread( *buf, 1, *n, f ) != *n )
533 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
538 memset( buf, 0, n + 1 );
546 #if defined(POLARSSL_SELF_TEST)
555 #if defined(POLARSSL_CERTS_C)
562 if( ( ret =
dhm_parse_dhm( &dhm, (
const unsigned char *) test_dhm_params,
563 strlen( test_dhm_params ) ) ) != 0 )
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.
#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.
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.
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 subtraction: 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.