mirror of
https://git.suyu.dev/suyu/mbedtls.git
synced 2025-12-21 21:36:21 +01:00
Use platform layer in programs for consistency.
This commit is contained in:
parent
e94e6e5b9c
commit
f90016aade
48 changed files with 1572 additions and 1145 deletions
|
|
@ -26,6 +26,15 @@
|
|||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#include "polarssl/platform.h"
|
||||
#else
|
||||
#define polarssl_printf printf
|
||||
#define polarssl_fprintf fprintf
|
||||
#define polarssl_malloc malloc
|
||||
#define polarssl_free free
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
|
@ -49,7 +58,7 @@ int main( int argc, char *argv[] )
|
|||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
printf("POLARSSL_AES_C and/or POLARSSL_DHM_C and/or POLARSSL_ENTROPY_C "
|
||||
polarssl_printf("POLARSSL_AES_C and/or POLARSSL_DHM_C and/or POLARSSL_ENTROPY_C "
|
||||
"and/or POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
|
||||
"POLARSSL_SHA1_C and/or POLARSSL_FS_IO and/or "
|
||||
"POLARSSL_CTR_DBRG_C not defined.\n");
|
||||
|
|
@ -86,7 +95,7 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* 1. Setup the RNG
|
||||
*/
|
||||
printf( "\n . Seeding the random number generator" );
|
||||
polarssl_printf( "\n . Seeding the random number generator" );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
|
|
@ -94,20 +103,20 @@ int main( int argc, char *argv[] )
|
|||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 2a. Read the server's private RSA key
|
||||
*/
|
||||
printf( "\n . Reading private key from rsa_priv.txt" );
|
||||
polarssl_printf( "\n . Reading private key from rsa_priv.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
polarssl_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -123,7 +132,7 @@ int main( int argc, char *argv[] )
|
|||
( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
|
||||
polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -134,13 +143,13 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* 2b. Get the DHM modulus and generator
|
||||
*/
|
||||
printf( "\n . Reading DH parameters from dh_prime.txt" );
|
||||
polarssl_printf( "\n . Reading DH parameters from dh_prime.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
printf( " failed\n ! Could not open dh_prime.txt\n" \
|
||||
polarssl_printf( " failed\n ! Could not open dh_prime.txt\n" \
|
||||
" ! Please run dh_genprime first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -148,7 +157,7 @@ int main( int argc, char *argv[] )
|
|||
if( mpi_read_file( &dhm.P, 16, f ) != 0 ||
|
||||
mpi_read_file( &dhm.G, 16, f ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! Invalid DH parameter file\n\n" );
|
||||
polarssl_printf( " failed\n ! Invalid DH parameter file\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -157,25 +166,25 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* 3. Wait for a client to connect
|
||||
*/
|
||||
printf( "\n . Waiting for a remote connection" );
|
||||
polarssl_printf( "\n . Waiting for a remote connection" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = net_bind( &listen_fd, NULL, SERVER_PORT ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! net_bind returned %d\n\n", ret );
|
||||
polarssl_printf( " failed\n ! net_bind returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! net_accept returned %d\n\n", ret );
|
||||
polarssl_printf( " failed\n ! net_accept returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 4. Setup the DH parameters (P,G,Ys)
|
||||
*/
|
||||
printf( "\n . Sending the server's DH parameters" );
|
||||
polarssl_printf( "\n . Sending the server's DH parameters" );
|
||||
fflush( stdout );
|
||||
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
|
@ -183,7 +192,7 @@ int main( int argc, char *argv[] )
|
|||
if( ( ret = dhm_make_params( &dhm, (int) mpi_size( &dhm.P ), buf, &n,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! dhm_make_params returned %d\n\n", ret );
|
||||
polarssl_printf( " failed\n ! dhm_make_params returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -198,7 +207,7 @@ int main( int argc, char *argv[] )
|
|||
if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_SHA1,
|
||||
0, hash, buf + n + 2 ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
|
||||
polarssl_printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -209,14 +218,14 @@ int main( int argc, char *argv[] )
|
|||
if( ( ret = net_send( &client_fd, buf2, 2 ) ) != 2 ||
|
||||
( ret = net_send( &client_fd, buf, buflen ) ) != (int) buflen )
|
||||
{
|
||||
printf( " failed\n ! net_send returned %d\n\n", ret );
|
||||
polarssl_printf( " failed\n ! net_send returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 6. Get the client's public value: Yc = G ^ Xc mod P
|
||||
*/
|
||||
printf( "\n . Receiving the client's public value" );
|
||||
polarssl_printf( "\n . Receiving the client's public value" );
|
||||
fflush( stdout );
|
||||
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
|
@ -224,31 +233,31 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( ret = net_recv( &client_fd, buf, n ) ) != (int) n )
|
||||
{
|
||||
printf( " failed\n ! net_recv returned %d\n\n", ret );
|
||||
polarssl_printf( " failed\n ! net_recv returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = dhm_read_public( &dhm, buf, dhm.len ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! dhm_read_public returned %d\n\n", ret );
|
||||
polarssl_printf( " failed\n ! dhm_read_public returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 7. Derive the shared secret: K = Ys ^ Xc mod P
|
||||
*/
|
||||
printf( "\n . Shared secret: " );
|
||||
polarssl_printf( "\n . Shared secret: " );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = dhm_calc_secret( &dhm, buf, &n,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
|
||||
polarssl_printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( n = 0; n < 16; n++ )
|
||||
printf( "%02x", buf[n] );
|
||||
polarssl_printf( "%02x", buf[n] );
|
||||
|
||||
/*
|
||||
* 8. Setup the AES-256 encryption key
|
||||
|
|
@ -258,7 +267,7 @@ int main( int argc, char *argv[] )
|
|||
* the keying material for the encryption/decryption keys
|
||||
* and MACs.
|
||||
*/
|
||||
printf( "...\n . Encrypting and sending the ciphertext" );
|
||||
polarssl_printf( "...\n . Encrypting and sending the ciphertext" );
|
||||
fflush( stdout );
|
||||
|
||||
aes_setkey_enc( &aes, buf, 256 );
|
||||
|
|
@ -267,11 +276,11 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( ret = net_send( &client_fd, buf, 16 ) ) != 16 )
|
||||
{
|
||||
printf( " failed\n ! net_send returned %d\n\n", ret );
|
||||
polarssl_printf( " failed\n ! net_send returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
printf( "\n\n" );
|
||||
polarssl_printf( "\n\n" );
|
||||
|
||||
exit:
|
||||
|
||||
|
|
@ -285,7 +294,7 @@ exit:
|
|||
entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
printf( " + Press Enter to exit this program.\n" );
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue