55#if !defined(MBEDTLS_CONFIG_FILE)
58#include MBEDTLS_CONFIG_FILE
61#if defined(MBEDTLS_DHM_C)
68#if defined(MBEDTLS_PEM_PARSE_C)
72#if defined(MBEDTLS_ASN1_PARSE_C)
76#if defined(MBEDTLS_PLATFORM_C)
81#define mbedtls_printf printf
82#define mbedtls_calloc calloc
83#define mbedtls_free free
86#if !defined(MBEDTLS_DHM_ALT)
88#define DHM_VALIDATE_RET( cond ) \
89 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_DHM_BAD_INPUT_DATA )
90#define DHM_VALIDATE( cond ) \
91 MBEDTLS_INTERNAL_VALIDATE( cond )
98 const unsigned char *
end )
105 n = ( (*p)[0] << 8 ) | (*
p)[1];
108 if( (
int)(
end - *
p ) <
n )
162 const unsigned char *
end )
165 DHM_VALIDATE_RET(
ctx !=
NULL );
167 DHM_VALIDATE_RET(
end !=
NULL );
169 if( (
ret = dhm_read_bignum( &
ctx->P,
p,
end ) ) != 0 ||
170 (
ret = dhm_read_bignum( &
ctx->G,
p,
end ) ) != 0 ||
171 (
ret = dhm_read_bignum( &
ctx->GY,
p,
end ) ) != 0 )
174 if( (
ret = dhm_check_range( &
ctx->GY, &
ctx->P ) ) != 0 )
186 int (*f_rng)(
void *,
unsigned char *,
size_t),
void *p_rng )
201 while( dhm_check_range(
R,
M ) != 0 );
208 int (*f_rng)(
void *,
unsigned char *,
size_t),
225 ret = dhm_random_below( &
ctx->X, &
ctx->P, f_rng, p_rng );
238 if( (
ret = dhm_check_range( &
ctx->GX, &
ctx->P ) ) != 0 )
249 unsigned char *output,
size_t *olen,
250 int (*f_rng)(
void *,
unsigned char *,
size_t),
256 DHM_VALIDATE_RET(
ctx !=
NULL );
257 DHM_VALIDATE_RET( output !=
NULL );
258 DHM_VALIDATE_RET( olen !=
NULL );
259 DHM_VALIDATE_RET( f_rng !=
NULL );
261 ret = dhm_make_common(
ctx, x_size, f_rng, p_rng );
269#define DHM_MPI_EXPORT( X, n ) \
271 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( ( X ), \
274 *p++ = (unsigned char)( ( n ) >> 8 ); \
275 *p++ = (unsigned char)( ( n ) ); \
284 DHM_MPI_EXPORT( &
ctx->P ,
n1 );
285 DHM_MPI_EXPORT( &
ctx->G ,
n2 );
286 DHM_MPI_EXPORT( &
ctx->GX,
n3 );
293 if(
ret != 0 &&
ret > -128 )
306 DHM_VALIDATE_RET(
ctx !=
NULL );
307 DHM_VALIDATE_RET(
P !=
NULL );
308 DHM_VALIDATE_RET(
G !=
NULL );
324 const unsigned char *
input,
size_t ilen )
327 DHM_VALIDATE_RET(
ctx !=
NULL );
330 if( ilen < 1 || ilen >
ctx->len )
343 unsigned char *output,
size_t olen,
344 int (*f_rng)(
void *,
unsigned char *,
size_t),
348 DHM_VALIDATE_RET(
ctx !=
NULL );
349 DHM_VALIDATE_RET( output !=
NULL );
350 DHM_VALIDATE_RET( f_rng !=
NULL );
352 if( olen < 1 || olen >
ctx->len )
355 ret = dhm_make_common(
ctx, x_size, f_rng, p_rng );
364 if(
ret != 0 &&
ret > -128 )
378 int (*f_rng)(
void *,
unsigned char *,
size_t),
void *p_rng )
442 unsigned char *output,
size_t output_size,
size_t *olen,
443 int (*f_rng)(
void *,
unsigned char *,
size_t),
448 DHM_VALIDATE_RET(
ctx !=
NULL );
449 DHM_VALIDATE_RET( output !=
NULL );
450 DHM_VALIDATE_RET( olen !=
NULL );
452 if( output_size < ctx->
len )
455 if( (
ret = dhm_check_range( &
ctx->GY, &
ctx->P ) ) != 0 )
517#if defined(MBEDTLS_ASN1_PARSE_C)
526 unsigned char *
p, *
end;
527#if defined(MBEDTLS_PEM_PARSE_C)
528 mbedtls_pem_context pem;
531 DHM_VALIDATE_RET( dhm !=
NULL );
532 DHM_VALIDATE_RET( dhmin !=
NULL );
534#if defined(MBEDTLS_PEM_PARSE_C)
535 mbedtls_pem_init( &pem );
538 if( dhminlen == 0 || dhmin[dhminlen - 1] !=
'\0' )
541 ret = mbedtls_pem_read_buffer( &pem,
542 "-----BEGIN DH PARAMETERS-----",
543 "-----END DH PARAMETERS-----",
544 dhmin,
NULL, 0, &dhminlen );
551 dhminlen = pem.buflen;
556 p = (
ret == 0 ) ? pem.buf : (
unsigned char *) dhmin;
558 p = (
unsigned char *) dhmin;
611#if defined(MBEDTLS_PEM_PARSE_C)
612 mbedtls_pem_free( &pem );
620#if defined(MBEDTLS_FS_IO)
681 DHM_VALIDATE_RET( dhm !=
NULL );
698#if defined(MBEDTLS_SELF_TEST)
700#if defined(MBEDTLS_PEM_PARSE_C)
701static const char mbedtls_test_dhm_params[] =
702"-----BEGIN DH PARAMETERS-----\r\n"
703"MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh\r\n"
704"1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32\r\n"
705"9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC\r\n"
706"-----END DH PARAMETERS-----\r\n";
708static const char mbedtls_test_dhm_params[] = {
709 0x30, 0x81, 0x87, 0x02, 0x81, 0x81, 0x00, 0x9e, 0x35, 0xf4, 0x30, 0x44,
710 0x3a, 0x09, 0x90, 0x4f, 0x3a, 0x39, 0xa9, 0x79, 0x79, 0x7d, 0x07, 0x0d,
711 0xf5, 0x33, 0x78, 0xe7, 0x9c, 0x24, 0x38, 0xbe, 0xf4, 0xe7, 0x61, 0xf3,
712 0xc7, 0x14, 0x55, 0x33, 0x28, 0x58, 0x9b, 0x04, 0x1c, 0x80, 0x9b, 0xe1,
713 0xd6, 0xc6, 0xb5, 0xf1, 0xfc, 0x9f, 0x47, 0xd3, 0xa2, 0x54, 0x43, 0x18,
714 0x82, 0x53, 0xa9, 0x92, 0xa5, 0x68, 0x18, 0xb3, 0x7b, 0xa9, 0xde, 0x5a,
715 0x40, 0xd3, 0x62, 0xe5, 0x6e, 0xff, 0x0b, 0xe5, 0x41, 0x74, 0x74, 0xc1,
716 0x25, 0xc1, 0x99, 0x27, 0x2c, 0x8f, 0xe4, 0x1d, 0xea, 0x73, 0x3d, 0xf6,
717 0xf6, 0x62, 0xc9, 0x2a, 0xe7, 0x65, 0x56, 0xe7, 0x55, 0xd1, 0x0c, 0x64,
718 0xe6, 0xa5, 0x09, 0x68, 0xf6, 0x7f, 0xc6, 0xea, 0x73, 0xd0, 0xdc, 0xa8,
719 0x56, 0x9b, 0xe2, 0xba, 0x20, 0x4e, 0x23, 0x58, 0x0d, 0x8b, 0xca, 0x2f,
720 0x49, 0x75, 0xb3, 0x02, 0x01, 0x02 };
723static const size_t mbedtls_test_dhm_params_len =
sizeof( mbedtls_test_dhm_params );
728int mbedtls_dhm_self_test(
int verbose )
739 (
const unsigned char *) mbedtls_test_dhm_params,
740 mbedtls_test_dhm_params_len ) ) != 0 )
char * strstr(char *String1, char *String2)
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Perform a signed subtraction of an MPI and an integer: X = A - b.
#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Make a copy of an MPI.
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Return the total size of an MPI value in bytes.
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR)
Perform a sliding-window exponentiation: X = A^E mod N.
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Store integer value in MPI.
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Return the number of bits up to and including the most significant bit of value 1.
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Import an MPI from unsigned big endian binary data.
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare two MPIs.
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a modular reduction. R = A mod B.
int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Fill an MPI with a number of random bytes.
void mbedtls_mpi_init(mbedtls_mpi *X)
Initialize an MPI context.
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a multiplication of two MPIs: X = A * B.
#define MBEDTLS_MPI_CHK(f)
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Compute the modular inverse: X = A^-1 mod N.
void mbedtls_mpi_free(mbedtls_mpi *X)
This function frees the components of an MPI context.
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
Export an MPI into unsigned big endian binary data of fixed size.
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Compare an MPI with an integer.
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Perform a right-shift on an MPI: X >>= count.
#define G(r, i, a, b, c, d)
This file contains Diffie-Hellman-Merkle (DHM) key exchange definitions and functions.
void mbedtls_dhm_free(mbedtls_dhm_context *ctx)
This function frees and clears the components of a DHM context.
#define MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED
int mbedtls_dhm_read_public(mbedtls_dhm_context *ctx, const unsigned char *input, size_t ilen)
This function imports the raw public value of the peer.
int mbedtls_dhm_set_group(mbedtls_dhm_context *ctx, const mbedtls_mpi *P, const mbedtls_mpi *G)
This function sets the prime modulus and generator.
#define MBEDTLS_ERR_DHM_CALC_SECRET_FAILED
#define MBEDTLS_ERR_DHM_SET_GROUP_FAILED
#define MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED
int mbedtls_dhm_read_params(mbedtls_dhm_context *ctx, unsigned char **p, const unsigned char *end)
This function parses the DHM parameters in a TLS ServerKeyExchange handshake message (DHM modulus,...
#define MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED
#define MBEDTLS_ERR_DHM_FILE_IO_ERROR
#define MBEDTLS_ERR_DHM_ALLOC_FAILED
void mbedtls_dhm_init(mbedtls_dhm_context *ctx)
This function initializes the DHM context.
int mbedtls_dhm_calc_secret(mbedtls_dhm_context *ctx, unsigned char *output, size_t output_size, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function derives and exports the shared secret (G^Y)^X mod P.
#define MBEDTLS_ERR_DHM_INVALID_FORMAT
int mbedtls_dhm_make_params(mbedtls_dhm_context *ctx, int x_size, unsigned char *output, size_t *olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates a DHM key pair and exports its public part together with the DHM parameters i...
#define MBEDTLS_ERR_DHM_BAD_INPUT_DATA
#define MBEDTLS_ERR_DHM_READ_PARAMS_FAILED
int mbedtls_dhm_make_public(mbedtls_dhm_context *ctx, int x_size, unsigned char *output, size_t olen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function creates a DHM key pair and exports the raw public key in big-endian format.
static void cleanup(void)
static UINT load_file(MSIRECORD *row, LPVOID param)
GLuint GLuint GLsizei count
GLenum GLuint GLenum GLsizei const GLchar * buf
GLenum GLenum GLenum input
int mbedtls_asn1_get_mpi(unsigned char **p, const unsigned char *end, mbedtls_mpi *X)
Retrieve a MPI value from an integer ASN.1 tag. Updates the pointer to immediately behind the full ta...
#define MBEDTLS_ASN1_SEQUENCE
#define MBEDTLS_ASN1_CONSTRUCTED
#define MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
int mbedtls_asn1_get_tag(unsigned char **p, const unsigned char *end, size_t *len, int tag)
Get the tag and length of the tag. Check for the requested tag. Updates the pointer to immediately be...
int mbedtls_dhm_parse_dhm(mbedtls_dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen)
This function parses DHM parameters in PEM or DER format.
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP long __cdecl ftell(_Inout_ FILE *_File)
Privacy Enhanced Mail (PEM) decoding.
#define MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT
Configuration options (set of defines)
The DHM context structure.