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
59 static void polarssl_zeroize(
void *v,
size_t n ) {
60 volatile unsigned char *p = v;
while( n-- ) *p++ = 0;
66 static int dhm_read_bignum(
mpi *X,
68 const unsigned char *end )
75 n = ( (*p)[0] << 8 ) | (*p)[1];
78 if( (
int)( end - *p ) < n )
98 static int dhm_check_range(
const mpi *param,
const mpi *P )
129 const unsigned char *end )
133 if( ( ret = dhm_read_bignum( &ctx->
P, p, end ) ) != 0 ||
134 ( ret = dhm_read_bignum( &ctx->
G, p, end ) ) != 0 ||
135 ( ret = dhm_read_bignum( &ctx->
GY, p, end ) ) != 0 )
138 if( ( ret = dhm_check_range( &ctx->
GY, &ctx->
P ) ) != 0 )
150 unsigned char *output,
size_t *olen,
151 int (*f_rng)(
void *,
unsigned char *,
size_t),
174 while( dhm_check_range( &ctx->
X, &ctx->
P ) != 0 );
180 &ctx->
P , &ctx->
RP ) );
182 if( ( ret = dhm_check_range( &ctx->
GX, &ctx->
P ) ) != 0 )
188 #define DHM_MPI_EXPORT(X,n) \
189 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
190 *p++ = (unsigned char)( n >> 8 ); \
191 *p++ = (unsigned char)( n ); p += n;
198 DHM_MPI_EXPORT( &ctx->
P , n1 );
199 DHM_MPI_EXPORT( &ctx->
G , n2 );
200 DHM_MPI_EXPORT( &ctx->
GX, n3 );
218 const unsigned char *input,
size_t ilen )
222 if( ctx == NULL || ilen < 1 || ilen > ctx->
len )
235 unsigned char *output,
size_t olen,
236 int (*f_rng)(
void *,
unsigned char *,
size_t),
241 if( ctx == NULL || olen < 1 || olen > ctx->
len )
260 while( dhm_check_range( &ctx->
X, &ctx->
P ) != 0 );
263 &ctx->
P , &ctx->
RP ) );
265 if( ( ret = dhm_check_range( &ctx->
GX, &ctx->
P ) ) != 0 )
285 int (*f_rng)(
void *,
unsigned char *,
size_t),
void *p_rng )
347 unsigned char *output,
size_t *olen,
348 int (*f_rng)(
void *,
unsigned char *,
size_t),
354 if( ctx == NULL || *olen < ctx->len )
357 if( ( ret = dhm_check_range( &ctx->
GY, &ctx->
P ) ) != 0 )
365 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
374 &ctx->
P, &ctx->
RP ) );
409 #if defined(POLARSSL_ASN1_PARSE_C)
418 unsigned char *p, *end;
419 #if defined(POLARSSL_PEM_PARSE_C)
424 ret = pem_read_buffer( &pem,
425 "-----BEGIN DH PARAMETERS-----",
426 "-----END DH PARAMETERS-----",
427 dhmin, NULL, 0, &dhminlen );
434 dhminlen = pem.buflen;
439 p = ( ret == 0 ) ? pem.buf : (
unsigned char *) dhmin;
441 p = (
unsigned char *) dhmin;
479 #if defined(POLARSSL_PEM_PARSE_C)
488 #if defined(POLARSSL_FS_IO)
492 static int load_file(
const char *path,
unsigned char **buf,
size_t *n )
497 if( ( f = fopen( path,
"rb" ) ) == NULL )
500 fseek( f, 0, SEEK_END );
501 if( ( size = ftell( f ) ) == -1 )
506 fseek( f, 0, SEEK_SET );
517 if( fread( *buf, 1, *n, f ) != *n )
540 if( ( ret = load_file( path, &buf, &n ) ) != 0 )
545 polarssl_zeroize( buf, n + 1 );
553 #if defined(POLARSSL_SELF_TEST)
562 #if defined(POLARSSL_CERTS_C)
571 if( ( ret =
dhm_parse_dhm( &dhm, (
const unsigned char *) test_dhm_params,
572 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.
void dhm_init(dhm_context *ctx)
Initialize DHM context.
#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 and clear 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.