mirror of
https://git.suyu.dev/suyu/mbedtls.git
synced 2026-01-05 05:59:23 +01:00
The Great Renaming
A simple execution of tmp/invoke-rename.pl
This commit is contained in:
parent
b5904d25ef
commit
2cf5a7c98e
291 changed files with 36012 additions and 36012 deletions
|
|
@ -20,23 +20,23 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_AES_C) && defined(POLARSSL_DHM_C) && \
|
||||
defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_NET_C) && \
|
||||
defined(POLARSSL_RSA_C) && defined(POLARSSL_SHA256_C) && \
|
||||
defined(POLARSSL_FS_IO) && defined(POLARSSL_CTR_DRBG_C)
|
||||
#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
|
||||
defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/net.h"
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/dhm.h"
|
||||
|
|
@ -52,16 +52,16 @@
|
|||
#define SERVER_NAME "localhost"
|
||||
#define SERVER_PORT 11999
|
||||
|
||||
#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_DHM_C) || \
|
||||
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_NET_C) || \
|
||||
!defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA256_C) || \
|
||||
!defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
|
||||
#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \
|
||||
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \
|
||||
!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
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_SHA256_C and/or POLARSSL_FS_IO and/or "
|
||||
"POLARSSL_CTR_DRBG_C not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
|
||||
"and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
|
||||
"MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -78,90 +78,90 @@ int main( void )
|
|||
unsigned char hash[20];
|
||||
const char *pers = "dh_client";
|
||||
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
rsa_context rsa;
|
||||
dhm_context dhm;
|
||||
aes_context aes;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_dhm_context dhm;
|
||||
mbedtls_aes_context aes;
|
||||
|
||||
memset( &rsa, 0, sizeof( rsa ) );
|
||||
dhm_init( &dhm );
|
||||
aes_init( &aes );
|
||||
mbedtls_dhm_init( &dhm );
|
||||
mbedtls_aes_init( &aes );
|
||||
|
||||
/*
|
||||
* 1. Setup the RNG
|
||||
*/
|
||||
polarssl_printf( "\n . Seeding the random number generator" );
|
||||
mbedtls_printf( "\n . Seeding the random number generator" );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 2. Read the server's public RSA key
|
||||
*/
|
||||
polarssl_printf( "\n . Reading public key from rsa_pub.txt" );
|
||||
mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
|
||||
if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
|
||||
fclose( f );
|
||||
|
||||
/*
|
||||
* 3. Initiate the connection
|
||||
*/
|
||||
polarssl_printf( "\n . Connecting to tcp/%s/%d", SERVER_NAME,
|
||||
mbedtls_printf( "\n . Connecting to tcp/%s/%d", SERVER_NAME,
|
||||
SERVER_PORT );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = net_connect( &server_fd, SERVER_NAME,
|
||||
SERVER_PORT, NET_PROTO_TCP ) ) != 0 )
|
||||
if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
|
||||
SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 4a. First get the buffer length
|
||||
*/
|
||||
polarssl_printf( "\n . Receiving the server's DH parameters" );
|
||||
mbedtls_printf( "\n . Receiving the server's DH parameters" );
|
||||
fflush( stdout );
|
||||
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
if( ( ret = net_recv( &server_fd, buf, 2 ) ) != 2 )
|
||||
if( ( ret = mbedtls_net_recv( &server_fd, buf, 2 ) ) != 2 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! net_recv returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_net_recv returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
n = buflen = ( buf[0] << 8 ) | buf[1];
|
||||
if( buflen < 1 || buflen > sizeof( buf ) )
|
||||
{
|
||||
polarssl_printf( " failed\n ! Got an invalid buffer length\n\n" );
|
||||
mbedtls_printf( " failed\n ! Got an invalid buffer length\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -170,24 +170,24 @@ int main( void )
|
|||
*/
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
if( ( ret = net_recv( &server_fd, buf, n ) ) != (int) n )
|
||||
if( ( ret = mbedtls_net_recv( &server_fd, buf, n ) ) != (int) n )
|
||||
{
|
||||
polarssl_printf( " failed\n ! net_recv returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_net_recv returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
p = buf, end = buf + buflen;
|
||||
|
||||
if( ( ret = dhm_read_params( &dhm, &p, end ) ) != 0 )
|
||||
if( ( ret = mbedtls_dhm_read_params( &dhm, &p, end ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! dhm_read_params returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_dhm_read_params returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( dhm.len < 64 || dhm.len > 512 )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Invalid DHM modulus size\n\n" );
|
||||
mbedtls_printf( " failed\n ! Invalid DHM modulus size\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -195,7 +195,7 @@ int main( void )
|
|||
* 5. Check that the server's RSA signature matches
|
||||
* the SHA-256 hash of (P,G,Ys)
|
||||
*/
|
||||
polarssl_printf( "\n . Verifying the server's RSA signature" );
|
||||
mbedtls_printf( "\n . Verifying the server's RSA signature" );
|
||||
fflush( stdout );
|
||||
|
||||
p += 2;
|
||||
|
|
@ -203,55 +203,55 @@ int main( void )
|
|||
if( ( n = (size_t) ( end - p ) ) != rsa.len )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Invalid RSA signature size\n\n" );
|
||||
mbedtls_printf( " failed\n ! Invalid RSA signature size\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
sha1( buf, (int)( p - 2 - buf ), hash );
|
||||
mbedtls_sha1( buf, (int)( p - 2 - buf ), hash );
|
||||
|
||||
if( ( ret = rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC,
|
||||
POLARSSL_MD_SHA256, 0, hash, p ) ) != 0 )
|
||||
if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
|
||||
MBEDTLS_MD_SHA256, 0, hash, p ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_verify returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 6. Send our public value: Yc = G ^ Xc mod P
|
||||
*/
|
||||
polarssl_printf( "\n . Sending own public value to server" );
|
||||
mbedtls_printf( "\n . Sending own public value to server" );
|
||||
fflush( stdout );
|
||||
|
||||
n = dhm.len;
|
||||
if( ( ret = dhm_make_public( &dhm, (int) dhm.len, buf, n,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
if( ( ret = mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, n,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! dhm_make_public returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_dhm_make_public returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = net_send( &server_fd, buf, n ) ) != (int) n )
|
||||
if( ( ret = mbedtls_net_send( &server_fd, buf, n ) ) != (int) n )
|
||||
{
|
||||
polarssl_printf( " failed\n ! net_send returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_net_send returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 7. Derive the shared secret: K = Ys ^ Xc mod P
|
||||
*/
|
||||
polarssl_printf( "\n . Shared secret: " );
|
||||
mbedtls_printf( "\n . Shared secret: " );
|
||||
fflush( stdout );
|
||||
|
||||
n = dhm.len;
|
||||
if( ( ret = dhm_calc_secret( &dhm, buf, &n,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
if( ( ret = mbedtls_dhm_calc_secret( &dhm, buf, &n,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( n = 0; n < 16; n++ )
|
||||
polarssl_printf( "%02x", buf[n] );
|
||||
mbedtls_printf( "%02x", buf[n] );
|
||||
|
||||
/*
|
||||
* 8. Setup the AES-256 decryption key
|
||||
|
|
@ -261,41 +261,41 @@ int main( void )
|
|||
* the keying material for the encryption/decryption keys,
|
||||
* IVs and MACs.
|
||||
*/
|
||||
polarssl_printf( "...\n . Receiving and decrypting the ciphertext" );
|
||||
mbedtls_printf( "...\n . Receiving and decrypting the ciphertext" );
|
||||
fflush( stdout );
|
||||
|
||||
aes_setkey_dec( &aes, buf, 256 );
|
||||
mbedtls_aes_setkey_dec( &aes, buf, 256 );
|
||||
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
if( ( ret = net_recv( &server_fd, buf, 16 ) ) != 16 )
|
||||
if( ( ret = mbedtls_net_recv( &server_fd, buf, 16 ) ) != 16 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! net_recv returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_net_recv returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
aes_crypt_ecb( &aes, AES_DECRYPT, buf, buf );
|
||||
mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_DECRYPT, buf, buf );
|
||||
buf[16] = '\0';
|
||||
polarssl_printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
|
||||
mbedtls_printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
|
||||
|
||||
exit:
|
||||
|
||||
if( server_fd != -1 )
|
||||
net_close( server_fd );
|
||||
mbedtls_net_close( server_fd );
|
||||
|
||||
aes_free( &aes );
|
||||
rsa_free( &rsa );
|
||||
dhm_free( &dhm );
|
||||
ctr_drbg_free( &ctr_drbg );
|
||||
entropy_free( &entropy );
|
||||
mbedtls_aes_free( &aes );
|
||||
mbedtls_rsa_free( &rsa );
|
||||
mbedtls_dhm_free( &dhm );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_ENTROPY_C &&
|
||||
POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA256_C &&
|
||||
POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */
|
||||
#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -20,22 +20,22 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_ENTROPY_C) && \
|
||||
defined(POLARSSL_FS_IO) && defined(POLARSSL_CTR_DRBG_C) && \
|
||||
defined(POLARSSL_GENPRIME)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) && \
|
||||
defined(MBEDTLS_GENPRIME)
|
||||
#include "mbedtls/bignum.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
|
@ -51,121 +51,121 @@
|
|||
#define DH_P_SIZE 1024
|
||||
#define GENERATOR "4"
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
|
||||
!defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C) || \
|
||||
!defined(POLARSSL_GENPRIME)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
|
||||
!defined(MBEDTLS_GENPRIME)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
|
||||
"POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C and/or "
|
||||
"POLARSSL_GENPRIME not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
|
||||
"MBEDTLS_GENPRIME not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
int main( void )
|
||||
{
|
||||
int ret = 1;
|
||||
mpi G, P, Q;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
mbedtls_mpi G, P, Q;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
const char *pers = "dh_genprime";
|
||||
FILE *fout;
|
||||
|
||||
mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
|
||||
entropy_init( &entropy );
|
||||
mbedtls_mpi_init( &G ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
|
||||
if( ( ret = mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_read_string returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\nWARNING: You should not generate and use your own DHM primes\n" );
|
||||
polarssl_printf( " unless you are very certain of what you are doing!\n" );
|
||||
polarssl_printf( " Failing to follow this instruction may result in\n" );
|
||||
polarssl_printf( " weak security for your connections! Use the\n" );
|
||||
polarssl_printf( " predefined DHM parameters from dhm.h instead!\n\n" );
|
||||
polarssl_printf( "============================================================\n\n" );
|
||||
mbedtls_printf( "\nWARNING: You should not generate and use your own DHM primes\n" );
|
||||
mbedtls_printf( " unless you are very certain of what you are doing!\n" );
|
||||
mbedtls_printf( " Failing to follow this instruction may result in\n" );
|
||||
mbedtls_printf( " weak security for your connections! Use the\n" );
|
||||
mbedtls_printf( " predefined DHM parameters from dhm.h instead!\n\n" );
|
||||
mbedtls_printf( "============================================================\n\n" );
|
||||
|
||||
polarssl_printf( " ! Generating large primes may take minutes!\n" );
|
||||
mbedtls_printf( " ! Generating large primes may take minutes!\n" );
|
||||
|
||||
polarssl_printf( "\n . Seeding the random number generator..." );
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n . Generating the modulus, please wait..." );
|
||||
mbedtls_printf( " ok\n . Generating the modulus, please wait..." );
|
||||
fflush( stdout );
|
||||
|
||||
/*
|
||||
* This can take a long time...
|
||||
*/
|
||||
if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_gen_prime( &P, DH_P_SIZE, 1,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_gen_prime returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
|
||||
mbedtls_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_sub_int( &Q, &P, 1 ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_sub_int returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_div_int returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mpi_is_prime( &Q, ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_is_prime( &Q, mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_is_prime returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n . Exporting the value in dh_prime.txt..." );
|
||||
mbedtls_printf( " ok\n . Exporting the value in dh_prime.txt..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not create dh_prime.txt\n\n" );
|
||||
mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
|
||||
( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
|
||||
if( ( ret = mbedtls_mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
|
||||
( ret = mbedtls_mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n\n" );
|
||||
mbedtls_printf( " ok\n\n" );
|
||||
fclose( fout );
|
||||
|
||||
exit:
|
||||
|
||||
mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
|
||||
ctr_drbg_free( &ctr_drbg );
|
||||
entropy_free( &entropy );
|
||||
mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_FS_IO &&
|
||||
POLARSSL_CTR_DRBG_C && POLARSSL_GENPRIME */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */
|
||||
|
|
|
|||
|
|
@ -20,23 +20,23 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_AES_C) && defined(POLARSSL_DHM_C) && \
|
||||
defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_NET_C) && \
|
||||
defined(POLARSSL_RSA_C) && defined(POLARSSL_SHA256_C) && \
|
||||
defined(POLARSSL_FS_IO) && defined(POLARSSL_CTR_DRBG_C)
|
||||
#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
|
||||
defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/net.h"
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/dhm.h"
|
||||
|
|
@ -52,16 +52,16 @@
|
|||
#define SERVER_PORT 11999
|
||||
#define PLAINTEXT "==Hello there!=="
|
||||
|
||||
#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_DHM_C) || \
|
||||
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_NET_C) || \
|
||||
!defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA256_C) || \
|
||||
!defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
|
||||
#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \
|
||||
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \
|
||||
!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
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_SHA256_C and/or POLARSSL_FS_IO and/or "
|
||||
"POLARSSL_CTR_DRBG_C not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
|
||||
"and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
|
||||
"MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -79,82 +79,82 @@ int main( void )
|
|||
unsigned char buf2[2];
|
||||
const char *pers = "dh_server";
|
||||
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
rsa_context rsa;
|
||||
dhm_context dhm;
|
||||
aes_context aes;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_dhm_context dhm;
|
||||
mbedtls_aes_context aes;
|
||||
|
||||
memset( &rsa, 0, sizeof( rsa ) );
|
||||
dhm_init( &dhm );
|
||||
aes_init( &aes );
|
||||
mbedtls_dhm_init( &dhm );
|
||||
mbedtls_aes_init( &aes );
|
||||
|
||||
/*
|
||||
* 1. Setup the RNG
|
||||
*/
|
||||
polarssl_printf( "\n . Seeding the random number generator" );
|
||||
mbedtls_printf( "\n . Seeding the random number generator" );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 2a. Read the server's private RSA key
|
||||
*/
|
||||
polarssl_printf( "\n . Reading private key from rsa_priv.txt" );
|
||||
mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
|
||||
if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
|
||||
fclose( f );
|
||||
|
||||
/*
|
||||
* 2b. Get the DHM modulus and generator
|
||||
*/
|
||||
polarssl_printf( "\n . Reading DH parameters from dh_prime.txt" );
|
||||
mbedtls_printf( "\n . Reading DH parameters from dh_prime.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not open dh_prime.txt\n" \
|
||||
mbedtls_printf( " failed\n ! Could not open dh_prime.txt\n" \
|
||||
" ! Please run dh_genprime first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( mpi_read_file( &dhm.P, 16, f ) != 0 ||
|
||||
mpi_read_file( &dhm.G, 16, f ) != 0 )
|
||||
if( mbedtls_mpi_read_file( &dhm.P, 16, f ) != 0 ||
|
||||
mbedtls_mpi_read_file( &dhm.G, 16, f ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! Invalid DH parameter file\n\n" );
|
||||
mbedtls_printf( " failed\n ! Invalid DH parameter file\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -163,48 +163,48 @@ int main( void )
|
|||
/*
|
||||
* 3. Wait for a client to connect
|
||||
*/
|
||||
polarssl_printf( "\n . Waiting for a remote connection" );
|
||||
mbedtls_printf( "\n . Waiting for a remote connection" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = net_bind( &listen_fd, NULL, SERVER_PORT, NET_PROTO_TCP ) ) != 0 )
|
||||
if( ( ret = mbedtls_net_bind( &listen_fd, NULL, SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! net_bind returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
|
||||
if( ( ret = mbedtls_net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! net_accept returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 4. Setup the DH parameters (P,G,Ys)
|
||||
*/
|
||||
polarssl_printf( "\n . Sending the server's DH parameters" );
|
||||
mbedtls_printf( "\n . Sending the server's DH parameters" );
|
||||
fflush( stdout );
|
||||
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
if( ( ret = dhm_make_params( &dhm, (int) mpi_size( &dhm.P ), buf, &n,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
if( ( ret = mbedtls_dhm_make_params( &dhm, (int) mbedtls_mpi_size( &dhm.P ), buf, &n,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! dhm_make_params returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_dhm_make_params returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 5. Sign the parameters and send them
|
||||
*/
|
||||
sha1( buf, n, hash );
|
||||
mbedtls_sha1( buf, n, hash );
|
||||
|
||||
buf[n ] = (unsigned char)( rsa.len >> 8 );
|
||||
buf[n + 1] = (unsigned char)( rsa.len );
|
||||
|
||||
if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_SHA256,
|
||||
if( ( ret = mbedtls_rsa_pkcs1_sign( &rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256,
|
||||
0, hash, buf + n + 2 ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_sign returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -212,49 +212,49 @@ int main( void )
|
|||
buf2[0] = (unsigned char)( buflen >> 8 );
|
||||
buf2[1] = (unsigned char)( buflen );
|
||||
|
||||
if( ( ret = net_send( &client_fd, buf2, 2 ) ) != 2 ||
|
||||
( ret = net_send( &client_fd, buf, buflen ) ) != (int) buflen )
|
||||
if( ( ret = mbedtls_net_send( &client_fd, buf2, 2 ) ) != 2 ||
|
||||
( ret = mbedtls_net_send( &client_fd, buf, buflen ) ) != (int) buflen )
|
||||
{
|
||||
polarssl_printf( " failed\n ! net_send returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_net_send returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 6. Get the client's public value: Yc = G ^ Xc mod P
|
||||
*/
|
||||
polarssl_printf( "\n . Receiving the client's public value" );
|
||||
mbedtls_printf( "\n . Receiving the client's public value" );
|
||||
fflush( stdout );
|
||||
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
n = dhm.len;
|
||||
|
||||
if( ( ret = net_recv( &client_fd, buf, n ) ) != (int) n )
|
||||
if( ( ret = mbedtls_net_recv( &client_fd, buf, n ) ) != (int) n )
|
||||
{
|
||||
polarssl_printf( " failed\n ! net_recv returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_net_recv returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = dhm_read_public( &dhm, buf, dhm.len ) ) != 0 )
|
||||
if( ( ret = mbedtls_dhm_read_public( &dhm, buf, dhm.len ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! dhm_read_public returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_dhm_read_public returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 7. Derive the shared secret: K = Ys ^ Xc mod P
|
||||
*/
|
||||
polarssl_printf( "\n . Shared secret: " );
|
||||
mbedtls_printf( "\n . Shared secret: " );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = dhm_calc_secret( &dhm, buf, &n,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
if( ( ret = mbedtls_dhm_calc_secret( &dhm, buf, &n,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( n = 0; n < 16; n++ )
|
||||
polarssl_printf( "%02x", buf[n] );
|
||||
mbedtls_printf( "%02x", buf[n] );
|
||||
|
||||
/*
|
||||
* 8. Setup the AES-256 encryption key
|
||||
|
|
@ -264,39 +264,39 @@ int main( void )
|
|||
* the keying material for the encryption/decryption keys
|
||||
* and MACs.
|
||||
*/
|
||||
polarssl_printf( "...\n . Encrypting and sending the ciphertext" );
|
||||
mbedtls_printf( "...\n . Encrypting and sending the ciphertext" );
|
||||
fflush( stdout );
|
||||
|
||||
aes_setkey_enc( &aes, buf, 256 );
|
||||
mbedtls_aes_setkey_enc( &aes, buf, 256 );
|
||||
memcpy( buf, PLAINTEXT, 16 );
|
||||
aes_crypt_ecb( &aes, AES_ENCRYPT, buf, buf );
|
||||
mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_ENCRYPT, buf, buf );
|
||||
|
||||
if( ( ret = net_send( &client_fd, buf, 16 ) ) != 16 )
|
||||
if( ( ret = mbedtls_net_send( &client_fd, buf, 16 ) ) != 16 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! net_send returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_net_send returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n\n" );
|
||||
mbedtls_printf( "\n\n" );
|
||||
|
||||
exit:
|
||||
|
||||
if( client_fd != -1 )
|
||||
net_close( client_fd );
|
||||
mbedtls_net_close( client_fd );
|
||||
|
||||
aes_free( &aes );
|
||||
rsa_free( &rsa );
|
||||
dhm_free( &dhm );
|
||||
ctr_drbg_free( &ctr_drbg );
|
||||
entropy_free( &entropy );
|
||||
mbedtls_aes_free( &aes );
|
||||
mbedtls_rsa_free( &rsa );
|
||||
mbedtls_dhm_free( &dhm );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_ENTROPY_C &&
|
||||
POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA256_C &&
|
||||
POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */
|
||||
#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -20,21 +20,21 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ECDSA_C) && \
|
||||
defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_CTR_DRBG_C)
|
||||
#if defined(MBEDTLS_ECDSA_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
|
|
@ -50,18 +50,18 @@
|
|||
/*
|
||||
* Uncomment to force use of a specific curve
|
||||
*/
|
||||
#define ECPARAMS POLARSSL_ECP_DP_SECP192R1
|
||||
#define ECPARAMS MBEDTLS_ECP_DP_SECP192R1
|
||||
|
||||
#if !defined(ECPARAMS)
|
||||
#define ECPARAMS ecp_curve_list()->grp_id
|
||||
#define ECPARAMS mbedtls_ecp_curve_list()->grp_id
|
||||
#endif
|
||||
|
||||
#if !defined(POLARSSL_ECDSA_C) || !defined(POLARSSL_SHA256_C) || \
|
||||
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
|
||||
#if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_ECDSA_C and/or POLARSSL_SHA256_C and/or "
|
||||
"POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C not defined\n");
|
||||
mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
|
||||
"MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -70,22 +70,22 @@ static void dump_buf( const char *title, unsigned char *buf, size_t len )
|
|||
{
|
||||
size_t i;
|
||||
|
||||
polarssl_printf( "%s", title );
|
||||
mbedtls_printf( "%s", title );
|
||||
for( i = 0; i < len; i++ )
|
||||
polarssl_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
|
||||
mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
|
||||
"0123456789ABCDEF" [buf[i] % 16] );
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
}
|
||||
|
||||
static void dump_pubkey( const char *title, ecdsa_context *key )
|
||||
static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key )
|
||||
{
|
||||
unsigned char buf[300];
|
||||
size_t len;
|
||||
|
||||
if( ecp_point_write_binary( &key->grp, &key->Q,
|
||||
POLARSSL_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
|
||||
if( mbedtls_ecp_point_write_binary( &key->grp, &key->Q,
|
||||
MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
|
||||
{
|
||||
polarssl_printf("internal error\n");
|
||||
mbedtls_printf("internal error\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -99,27 +99,27 @@ static void dump_pubkey( const char *title, ecdsa_context *key )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret;
|
||||
ecdsa_context ctx_sign, ctx_verify;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
mbedtls_ecdsa_context ctx_sign, ctx_verify;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char hash[] = "This should be the hash of a message.";
|
||||
unsigned char sig[512];
|
||||
size_t sig_len;
|
||||
const char *pers = "ecdsa";
|
||||
((void) argv);
|
||||
|
||||
ecdsa_init( &ctx_sign );
|
||||
ecdsa_init( &ctx_verify );
|
||||
mbedtls_ecdsa_init( &ctx_sign );
|
||||
mbedtls_ecdsa_init( &ctx_verify );
|
||||
|
||||
memset(sig, 0, sizeof( sig ) );
|
||||
ret = 1;
|
||||
|
||||
if( argc != 1 )
|
||||
{
|
||||
polarssl_printf( "usage: ecdsa\n" );
|
||||
mbedtls_printf( "usage: ecdsa\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
|
|
@ -128,47 +128,47 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* Generate a key pair for signing
|
||||
*/
|
||||
polarssl_printf( "\n . Seeding the random number generator..." );
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n . Generating key pair..." );
|
||||
mbedtls_printf( " ok\n . Generating key pair..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = ecdsa_genkey( &ctx_sign, ECPARAMS,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
if( ( ret = mbedtls_ecdsa_genkey( &ctx_sign, ECPARAMS,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ecdsa_genkey returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
|
||||
mbedtls_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
|
||||
|
||||
dump_pubkey( " + Public key: ", &ctx_sign );
|
||||
|
||||
/*
|
||||
* Sign some message hash
|
||||
*/
|
||||
polarssl_printf( " . Signing message..." );
|
||||
mbedtls_printf( " . Signing message..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = ecdsa_write_signature( &ctx_sign, POLARSSL_MD_SHA256,
|
||||
if( ( ret = mbedtls_ecdsa_write_signature( &ctx_sign, MBEDTLS_MD_SHA256,
|
||||
hash, sizeof( hash ),
|
||||
sig, &sig_len,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ecdsa_genkey returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
polarssl_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
|
||||
mbedtls_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
|
||||
|
||||
dump_buf( " + Hash: ", hash, sizeof hash );
|
||||
dump_buf( " + Signature: ", sig, sig_len );
|
||||
|
|
@ -180,18 +180,18 @@ int main( int argc, char *argv[] )
|
|||
* chose to use a new one in order to make it clear that the verifying
|
||||
* context only needs the public key (Q), and not the private key (d).
|
||||
*/
|
||||
polarssl_printf( " . Preparing verification context..." );
|
||||
mbedtls_printf( " . Preparing verification context..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
|
||||
if( ( ret = mbedtls_ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ecp_group_copy returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecp_group_copy returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
|
||||
if( ( ret = mbedtls_ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ecp_copy returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecp_copy returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -200,32 +200,32 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* Verify signature
|
||||
*/
|
||||
polarssl_printf( " ok\n . Verifying signature..." );
|
||||
mbedtls_printf( " ok\n . Verifying signature..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = ecdsa_read_signature( &ctx_verify,
|
||||
if( ( ret = mbedtls_ecdsa_read_signature( &ctx_verify,
|
||||
hash, sizeof( hash ),
|
||||
sig, sig_len ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ecdsa_read_signature returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n" );
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit:
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
ecdsa_free( &ctx_verify );
|
||||
ecdsa_free( &ctx_sign );
|
||||
ctr_drbg_free( &ctr_drbg );
|
||||
entropy_free( &entropy );
|
||||
mbedtls_ecdsa_free( &ctx_verify );
|
||||
mbedtls_ecdsa_free( &ctx_sign );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_ECDSA_C && POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
|
||||
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
|
||||
ECPARAMS */
|
||||
|
|
|
|||
|
|
@ -20,21 +20,21 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_FS_IO) && \
|
||||
defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_CTR_DRBG_C)
|
||||
#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
|
|
@ -64,7 +64,7 @@ int dev_random_entropy_poll( void *data, unsigned char *output,
|
|||
|
||||
file = fopen( "/dev/random", "rb" );
|
||||
if( file == NULL )
|
||||
return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
|
||||
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
||||
|
||||
while( left > 0 )
|
||||
{
|
||||
|
|
@ -73,7 +73,7 @@ int dev_random_entropy_poll( void *data, unsigned char *output,
|
|||
if( ret == 0 && ferror( file ) )
|
||||
{
|
||||
fclose( file );
|
||||
return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
|
||||
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
||||
}
|
||||
|
||||
p += ret;
|
||||
|
|
@ -88,23 +88,23 @@ int dev_random_entropy_poll( void *data, unsigned char *output,
|
|||
#endif /* !_WIN32 */
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
#define DFL_EC_CURVE ecp_curve_list()->grp_id
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
|
||||
#else
|
||||
#define DFL_EC_CURVE 0
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
|
||||
#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
|
||||
#define USAGE_DEV_RANDOM \
|
||||
" use_dev_random=0|1 default: 0\n"
|
||||
#else
|
||||
#define USAGE_DEV_RANDOM ""
|
||||
#endif /* !_WIN32 && POLARSSL_FS_IO */
|
||||
#endif /* !_WIN32 && MBEDTLS_FS_IO */
|
||||
|
||||
#define FORMAT_PEM 0
|
||||
#define FORMAT_DER 1
|
||||
|
||||
#define DFL_TYPE POLARSSL_PK_RSA
|
||||
#define DFL_TYPE MBEDTLS_PK_RSA
|
||||
#define DFL_RSA_KEYSIZE 4096
|
||||
#define DFL_FILENAME "keyfile.key"
|
||||
#define DFL_FORMAT FORMAT_PEM
|
||||
|
|
@ -121,12 +121,12 @@ int dev_random_entropy_poll( void *data, unsigned char *output,
|
|||
USAGE_DEV_RANDOM \
|
||||
"\n"
|
||||
|
||||
#if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO) || \
|
||||
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
|
||||
#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_FS_IO) || \
|
||||
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO and/or "
|
||||
"POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
|
||||
mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
|
||||
"not defined.\n" );
|
||||
return( 0 );
|
||||
}
|
||||
|
|
@ -144,7 +144,7 @@ struct options
|
|||
int use_dev_random; /* use /dev/random as entropy source */
|
||||
} opt;
|
||||
|
||||
static int write_private_key( pk_context *key, const char *output_file )
|
||||
static int write_private_key( mbedtls_pk_context *key, const char *output_file )
|
||||
{
|
||||
int ret;
|
||||
FILE *f;
|
||||
|
|
@ -155,14 +155,14 @@ static int write_private_key( pk_context *key, const char *output_file )
|
|||
memset(output_buf, 0, 16000);
|
||||
if( opt.format == FORMAT_PEM )
|
||||
{
|
||||
if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
len = strlen( (char *) output_buf );
|
||||
}
|
||||
else
|
||||
{
|
||||
if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
|
||||
if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
|
||||
return( ret );
|
||||
|
||||
len = ret;
|
||||
|
|
@ -186,34 +186,34 @@ static int write_private_key( pk_context *key, const char *output_file )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
pk_context key;
|
||||
mbedtls_pk_context key;
|
||||
char buf[1024];
|
||||
int i;
|
||||
char *p, *q;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
const char *pers = "gen_key";
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
const ecp_curve_info *curve_info;
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
const mbedtls_ecp_curve_info *curve_info;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Set to sane values
|
||||
*/
|
||||
pk_init( &key );
|
||||
mbedtls_pk_init( &key );
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
ret = 1;
|
||||
polarssl_printf( USAGE );
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
polarssl_printf( " availabled ec_curve values:\n" );
|
||||
curve_info = ecp_curve_list();
|
||||
polarssl_printf( " %s (default)\n", curve_info->name );
|
||||
mbedtls_printf( USAGE );
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
mbedtls_printf( " availabled ec_curve values:\n" );
|
||||
curve_info = mbedtls_ecp_curve_list();
|
||||
mbedtls_printf( " %s (default)\n", curve_info->name );
|
||||
while( ( ++curve_info )->name != NULL )
|
||||
polarssl_printf( " %s\n", curve_info->name );
|
||||
mbedtls_printf( " %s\n", curve_info->name );
|
||||
#endif
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -235,9 +235,9 @@ int main( int argc, char *argv[] )
|
|||
if( strcmp( p, "type" ) == 0 )
|
||||
{
|
||||
if( strcmp( q, "rsa" ) == 0 )
|
||||
opt.type = POLARSSL_PK_RSA;
|
||||
opt.type = MBEDTLS_PK_RSA;
|
||||
else if( strcmp( q, "ec" ) == 0 )
|
||||
opt.type = POLARSSL_PK_ECKEY;
|
||||
opt.type = MBEDTLS_PK_ECKEY;
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
|
|
@ -254,13 +254,13 @@ int main( int argc, char *argv[] )
|
|||
{
|
||||
opt.rsa_keysize = atoi( q );
|
||||
if( opt.rsa_keysize < 1024 ||
|
||||
opt.rsa_keysize > POLARSSL_MPI_MAX_BITS )
|
||||
opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS )
|
||||
goto usage;
|
||||
}
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
else if( strcmp( p, "ec_curve" ) == 0 )
|
||||
{
|
||||
if( ( curve_info = ecp_curve_info_from_name( q ) ) == NULL )
|
||||
if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL )
|
||||
goto usage;
|
||||
opt.ec_curve = curve_info->grp_id;
|
||||
}
|
||||
|
|
@ -277,144 +277,144 @@ int main( int argc, char *argv[] )
|
|||
goto usage;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Seeding the random number generator..." );
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
|
||||
mbedtls_entropy_init( &entropy );
|
||||
#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
|
||||
if( opt.use_dev_random )
|
||||
{
|
||||
if( ( ret = entropy_add_source( &entropy, dev_random_entropy_poll,
|
||||
if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll,
|
||||
NULL, DEV_RANDOM_THRESHOLD ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! entropy_add_source returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf("\n Using /dev/random, so can take a long time! " );
|
||||
mbedtls_printf("\n Using /dev/random, so can take a long time! " );
|
||||
fflush( stdout );
|
||||
}
|
||||
#endif /* !_WIN32 && POLARSSL_FS_IO */
|
||||
#endif /* !_WIN32 && MBEDTLS_FS_IO */
|
||||
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_init returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 1.1. Generate the key
|
||||
*/
|
||||
polarssl_printf( "\n . Generating the private key ..." );
|
||||
mbedtls_printf( "\n . Generating the private key ..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = pk_init_ctx( &key, pk_info_from_type( opt.type ) ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_init_ctx( &key, mbedtls_pk_info_from_type( opt.type ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! pk_init_ctx returned -0x%04x", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_init_ctx returned -0x%04x", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
|
||||
if( opt.type == POLARSSL_PK_RSA )
|
||||
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
|
||||
if( opt.type == MBEDTLS_PK_RSA )
|
||||
{
|
||||
ret = rsa_gen_key( pk_rsa( key ), ctr_drbg_random, &ctr_drbg,
|
||||
ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg,
|
||||
opt.rsa_keysize, 65537 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif /* POLARSSL_RSA_C */
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
if( opt.type == POLARSSL_PK_ECKEY )
|
||||
#endif /* MBEDTLS_RSA_C */
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( opt.type == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
ret = ecp_gen_key( opt.ec_curve, pk_ec( key ),
|
||||
ctr_drbg_random, &ctr_drbg );
|
||||
ret = mbedtls_ecp_gen_key( opt.ec_curve, mbedtls_pk_ec( key ),
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg );
|
||||
if( ret != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif /* POLARSSL_ECP_C */
|
||||
#endif /* MBEDTLS_ECP_C */
|
||||
{
|
||||
polarssl_printf( " failed\n ! key type not supported\n" );
|
||||
mbedtls_printf( " failed\n ! key type not supported\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 1.2 Print the key
|
||||
*/
|
||||
polarssl_printf( " ok\n . Key information:\n" );
|
||||
mbedtls_printf( " ok\n . Key information:\n" );
|
||||
|
||||
#if defined(POLARSSL_RSA_C)
|
||||
if( pk_get_type( &key ) == POLARSSL_PK_RSA )
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
|
||||
{
|
||||
rsa_context *rsa = pk_rsa( key );
|
||||
mpi_write_file( "N: ", &rsa->N, 16, NULL );
|
||||
mpi_write_file( "E: ", &rsa->E, 16, NULL );
|
||||
mpi_write_file( "D: ", &rsa->D, 16, NULL );
|
||||
mpi_write_file( "P: ", &rsa->P, 16, NULL );
|
||||
mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
|
||||
mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
|
||||
mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
|
||||
mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
|
||||
mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
|
||||
mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
|
||||
mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL );
|
||||
mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
|
||||
mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
ecp_keypair *ecp = pk_ec( key );
|
||||
polarssl_printf( "curve: %s\n",
|
||||
ecp_curve_info_from_grp_id( ecp->grp.id )->name );
|
||||
mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
|
||||
mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
|
||||
mpi_write_file( "D: ", &ecp->d , 16, NULL );
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
|
||||
mbedtls_printf( "curve: %s\n",
|
||||
mbedtls_ecp_curve_info_from_grp_id( ecp->grp.id )->name );
|
||||
mbedtls_mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D: ", &ecp->d , 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
polarssl_printf(" ! key type not supported\n");
|
||||
mbedtls_printf(" ! key type not supported\n");
|
||||
|
||||
/*
|
||||
* 1.3 Export key
|
||||
*/
|
||||
polarssl_printf( " . Writing key to file..." );
|
||||
mbedtls_printf( " . Writing key to file..." );
|
||||
|
||||
if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n" );
|
||||
mbedtls_printf( " failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n" );
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit:
|
||||
|
||||
if( ret != 0 && ret != 1)
|
||||
{
|
||||
#ifdef POLARSSL_ERROR_C
|
||||
polarssl_strerror( ret, buf, sizeof( buf ) );
|
||||
polarssl_printf( " - %s\n", buf );
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
mbedtls_strerror( ret, buf, sizeof( buf ) );
|
||||
mbedtls_printf( " - %s\n", buf );
|
||||
#else
|
||||
polarssl_printf("\n");
|
||||
mbedtls_printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
pk_free( &key );
|
||||
ctr_drbg_free( &ctr_drbg );
|
||||
entropy_free( &entropy );
|
||||
mbedtls_pk_free( &key );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_PK_WRITE_C && POLARSSL_FS_IO */
|
||||
#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -20,21 +20,21 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && \
|
||||
defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_FS_IO)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && \
|
||||
defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/x509.h"
|
||||
|
|
@ -62,12 +62,12 @@
|
|||
"\n"
|
||||
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || \
|
||||
!defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || \
|
||||
!defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or "
|
||||
"POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
|
||||
"MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -85,7 +85,7 @@ struct options
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
pk_context pk;
|
||||
mbedtls_pk_context pk;
|
||||
char buf[1024];
|
||||
int i;
|
||||
char *p, *q;
|
||||
|
|
@ -93,13 +93,13 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* Set to sane values
|
||||
*/
|
||||
pk_init( &pk );
|
||||
mbedtls_pk_init( &pk );
|
||||
memset( buf, 0, sizeof(buf) );
|
||||
|
||||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
polarssl_printf( USAGE );
|
||||
mbedtls_printf( USAGE );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -138,7 +138,7 @@ int main( int argc, char *argv[] )
|
|||
{
|
||||
if( strlen( opt.password ) && strlen( opt.password_file ) )
|
||||
{
|
||||
polarssl_printf( "Error: cannot have both password and password_file\n" );
|
||||
mbedtls_printf( "Error: cannot have both password and password_file\n" );
|
||||
goto usage;
|
||||
}
|
||||
|
||||
|
|
@ -146,16 +146,16 @@ int main( int argc, char *argv[] )
|
|||
{
|
||||
FILE *f;
|
||||
|
||||
polarssl_printf( "\n . Loading the password file ..." );
|
||||
mbedtls_printf( "\n . Loading the password file ..." );
|
||||
if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
|
||||
{
|
||||
polarssl_printf( " failed\n ! fopen returned NULL\n" );
|
||||
mbedtls_printf( " failed\n ! fopen returned NULL\n" );
|
||||
goto exit;
|
||||
}
|
||||
if( fgets( buf, sizeof(buf), f ) == NULL )
|
||||
{
|
||||
fclose( f );
|
||||
polarssl_printf( "Error: fgets() failed to retrieve password\n" );
|
||||
mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
|
||||
goto exit;
|
||||
}
|
||||
fclose( f );
|
||||
|
|
@ -169,51 +169,51 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* 1.1. Load the key
|
||||
*/
|
||||
polarssl_printf( "\n . Loading the private key ..." );
|
||||
mbedtls_printf( "\n . Loading the private key ..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = pk_parse_keyfile( &pk, opt.filename, opt.password );
|
||||
ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n" );
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* 1.2 Print the key
|
||||
*/
|
||||
polarssl_printf( " . Key information ...\n" );
|
||||
#if defined(POLARSSL_RSA_C)
|
||||
if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
|
||||
mbedtls_printf( " . Key information ...\n" );
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
|
||||
{
|
||||
rsa_context *rsa = pk_rsa( pk );
|
||||
mpi_write_file( "N: ", &rsa->N, 16, NULL );
|
||||
mpi_write_file( "E: ", &rsa->E, 16, NULL );
|
||||
mpi_write_file( "D: ", &rsa->D, 16, NULL );
|
||||
mpi_write_file( "P: ", &rsa->P, 16, NULL );
|
||||
mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
|
||||
mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
|
||||
mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
|
||||
mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
|
||||
mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
|
||||
mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
|
||||
mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL );
|
||||
mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
|
||||
mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
ecp_keypair *ecp = pk_ec( pk );
|
||||
mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
|
||||
mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
|
||||
mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
|
||||
mpi_write_file( "D : ", &ecp->d , 16, NULL );
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
|
||||
mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
polarssl_printf("Do not know how to print key information for this type\n" );
|
||||
mbedtls_printf("Do not know how to print key information for this type\n" );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
|
@ -222,41 +222,41 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* 1.1. Load the key
|
||||
*/
|
||||
polarssl_printf( "\n . Loading the public key ..." );
|
||||
mbedtls_printf( "\n . Loading the public key ..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = pk_parse_public_keyfile( &pk, opt.filename );
|
||||
ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n" );
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
polarssl_printf( " . Key information ...\n" );
|
||||
#if defined(POLARSSL_RSA_C)
|
||||
if( pk_get_type( &pk ) == POLARSSL_PK_RSA )
|
||||
mbedtls_printf( " . Key information ...\n" );
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
|
||||
{
|
||||
rsa_context *rsa = pk_rsa( pk );
|
||||
mpi_write_file( "N: ", &rsa->N, 16, NULL );
|
||||
mpi_write_file( "E: ", &rsa->E, 16, NULL );
|
||||
mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
|
||||
mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
|
||||
mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY )
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
ecp_keypair *ecp = pk_ec( pk );
|
||||
mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
|
||||
mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
|
||||
mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
|
||||
mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
polarssl_printf("Do not know how to print key information for this type\n" );
|
||||
mbedtls_printf("Do not know how to print key information for this type\n" );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
|
@ -265,18 +265,18 @@ int main( int argc, char *argv[] )
|
|||
|
||||
exit:
|
||||
|
||||
#if defined(POLARSSL_ERROR_C)
|
||||
polarssl_strerror( ret, buf, sizeof(buf) );
|
||||
polarssl_printf( " ! Last error was: %s\n", buf );
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
mbedtls_strerror( ret, buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
#endif
|
||||
|
||||
pk_free( &pk );
|
||||
mbedtls_pk_free( &pk );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -20,20 +20,20 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_FS_IO)
|
||||
#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/error.h"
|
||||
|
|
@ -42,7 +42,7 @@
|
|||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PEM_WRITE_C)
|
||||
#if defined(MBEDTLS_PEM_WRITE_C)
|
||||
#define USAGE_OUT \
|
||||
" output_file=%%s default: keyfile.pem\n" \
|
||||
" output_format=pem|der default: pem\n"
|
||||
|
|
@ -52,7 +52,7 @@
|
|||
" output_format=der default: der\n"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PEM_WRITE_C)
|
||||
#if defined(MBEDTLS_PEM_WRITE_C)
|
||||
#define DFL_OUTPUT_FILENAME "keyfile.pem"
|
||||
#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
|
||||
#else
|
||||
|
|
@ -85,10 +85,10 @@
|
|||
USAGE_OUT \
|
||||
"\n"
|
||||
|
||||
#if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO)
|
||||
#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO not defined.\n" );
|
||||
mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n" );
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -104,7 +104,7 @@ struct options
|
|||
int output_format; /* the output format to use */
|
||||
} opt;
|
||||
|
||||
static int write_public_key( pk_context *key, const char *output_file )
|
||||
static int write_public_key( mbedtls_pk_context *key, const char *output_file )
|
||||
{
|
||||
int ret;
|
||||
FILE *f;
|
||||
|
|
@ -114,10 +114,10 @@ static int write_public_key( pk_context *key, const char *output_file )
|
|||
|
||||
memset(output_buf, 0, 16000);
|
||||
|
||||
#if defined(POLARSSL_PEM_WRITE_C)
|
||||
#if defined(MBEDTLS_PEM_WRITE_C)
|
||||
if( opt.output_format == OUTPUT_FORMAT_PEM )
|
||||
{
|
||||
if( ( ret = pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
len = strlen( (char *) output_buf );
|
||||
|
|
@ -125,7 +125,7 @@ static int write_public_key( pk_context *key, const char *output_file )
|
|||
else
|
||||
#endif
|
||||
{
|
||||
if( ( ret = pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
|
||||
if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
|
||||
return( ret );
|
||||
|
||||
len = ret;
|
||||
|
|
@ -146,7 +146,7 @@ static int write_public_key( pk_context *key, const char *output_file )
|
|||
return( 0 );
|
||||
}
|
||||
|
||||
static int write_private_key( pk_context *key, const char *output_file )
|
||||
static int write_private_key( mbedtls_pk_context *key, const char *output_file )
|
||||
{
|
||||
int ret;
|
||||
FILE *f;
|
||||
|
|
@ -156,10 +156,10 @@ static int write_private_key( pk_context *key, const char *output_file )
|
|||
|
||||
memset(output_buf, 0, 16000);
|
||||
|
||||
#if defined(POLARSSL_PEM_WRITE_C)
|
||||
#if defined(MBEDTLS_PEM_WRITE_C)
|
||||
if( opt.output_format == OUTPUT_FORMAT_PEM )
|
||||
{
|
||||
if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
len = strlen( (char *) output_buf );
|
||||
|
|
@ -167,7 +167,7 @@ static int write_private_key( pk_context *key, const char *output_file )
|
|||
else
|
||||
#endif
|
||||
{
|
||||
if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
|
||||
if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
|
||||
return( ret );
|
||||
|
||||
len = ret;
|
||||
|
|
@ -191,7 +191,7 @@ static int write_private_key( pk_context *key, const char *output_file )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
pk_context key;
|
||||
mbedtls_pk_context key;
|
||||
char buf[1024];
|
||||
int i;
|
||||
char *p, *q;
|
||||
|
|
@ -199,14 +199,14 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* Set to sane values
|
||||
*/
|
||||
pk_init( &key );
|
||||
mbedtls_pk_init( &key );
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
ret = 1;
|
||||
polarssl_printf( USAGE );
|
||||
mbedtls_printf( USAGE );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -243,7 +243,7 @@ int main( int argc, char *argv[] )
|
|||
}
|
||||
else if( strcmp( p, "output_format" ) == 0 )
|
||||
{
|
||||
#if defined(POLARSSL_PEM_WRITE_C)
|
||||
#if defined(MBEDTLS_PEM_WRITE_C)
|
||||
if( strcmp( q, "pem" ) == 0 )
|
||||
opt.output_format = OUTPUT_FORMAT_PEM;
|
||||
else
|
||||
|
|
@ -263,13 +263,13 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
|
||||
{
|
||||
polarssl_printf( "\nCannot output a key without reading one.\n");
|
||||
mbedtls_printf( "\nCannot output a key without reading one.\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
|
||||
{
|
||||
polarssl_printf( "\nCannot output a private key from a public key.\n");
|
||||
mbedtls_printf( "\nCannot output a private key from a public key.\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -278,52 +278,52 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* 1.1. Load the key
|
||||
*/
|
||||
polarssl_printf( "\n . Loading the private key ..." );
|
||||
mbedtls_printf( "\n . Loading the private key ..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = pk_parse_keyfile( &key, opt.filename, NULL );
|
||||
ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
polarssl_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n" );
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* 1.2 Print the key
|
||||
*/
|
||||
polarssl_printf( " . Key information ...\n" );
|
||||
mbedtls_printf( " . Key information ...\n" );
|
||||
|
||||
#if defined(POLARSSL_RSA_C)
|
||||
if( pk_get_type( &key ) == POLARSSL_PK_RSA )
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
|
||||
{
|
||||
rsa_context *rsa = pk_rsa( key );
|
||||
mpi_write_file( "N: ", &rsa->N, 16, NULL );
|
||||
mpi_write_file( "E: ", &rsa->E, 16, NULL );
|
||||
mpi_write_file( "D: ", &rsa->D, 16, NULL );
|
||||
mpi_write_file( "P: ", &rsa->P, 16, NULL );
|
||||
mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
|
||||
mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
|
||||
mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
|
||||
mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
|
||||
mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
|
||||
mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
|
||||
mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL );
|
||||
mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
|
||||
mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
ecp_keypair *ecp = pk_ec( key );
|
||||
mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
|
||||
mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
|
||||
mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
|
||||
mpi_write_file( "D : ", &ecp->d , 16, NULL );
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
|
||||
mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
polarssl_printf("key type not supported yet\n");
|
||||
mbedtls_printf("key type not supported yet\n");
|
||||
|
||||
}
|
||||
else if( opt.mode == MODE_PUBLIC )
|
||||
|
|
@ -331,45 +331,45 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* 1.1. Load the key
|
||||
*/
|
||||
polarssl_printf( "\n . Loading the public key ..." );
|
||||
mbedtls_printf( "\n . Loading the public key ..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = pk_parse_public_keyfile( &key, opt.filename );
|
||||
ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
polarssl_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
polarssl_printf( " failed\n ! pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n" );
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* 1.2 Print the key
|
||||
*/
|
||||
polarssl_printf( " . Key information ...\n" );
|
||||
mbedtls_printf( " . Key information ...\n" );
|
||||
|
||||
#if defined(POLARSSL_RSA_C)
|
||||
if( pk_get_type( &key ) == POLARSSL_PK_RSA )
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
|
||||
{
|
||||
rsa_context *rsa = pk_rsa( key );
|
||||
mpi_write_file( "N: ", &rsa->N, 16, NULL );
|
||||
mpi_write_file( "E: ", &rsa->E, 16, NULL );
|
||||
mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
|
||||
mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
|
||||
mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
ecp_keypair *ecp = pk_ec( key );
|
||||
mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
|
||||
mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
|
||||
mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
|
||||
mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
polarssl_printf("key type not supported yet\n");
|
||||
mbedtls_printf("key type not supported yet\n");
|
||||
}
|
||||
else
|
||||
goto usage;
|
||||
|
|
@ -387,21 +387,21 @@ exit:
|
|||
|
||||
if( ret != 0 && ret != 1)
|
||||
{
|
||||
#ifdef POLARSSL_ERROR_C
|
||||
polarssl_strerror( ret, buf, sizeof( buf ) );
|
||||
polarssl_printf( " - %s\n", buf );
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
mbedtls_strerror( ret, buf, sizeof( buf ) );
|
||||
mbedtls_printf( " - %s\n", buf );
|
||||
#else
|
||||
polarssl_printf("\n");
|
||||
mbedtls_printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
pk_free( &key );
|
||||
mbedtls_pk_free( &key );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_PK_WRITE_C && POLARSSL_FS_IO */
|
||||
#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -20,91 +20,91 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_FS_IO)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/bignum.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_FS_IO)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_FS_IO not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
int main( void )
|
||||
{
|
||||
int ret;
|
||||
mpi E, P, Q, N, H, D, X, Y, Z;
|
||||
mbedtls_mpi E, P, Q, N, H, D, X, Y, Z;
|
||||
|
||||
mpi_init( &E ); mpi_init( &P ); mpi_init( &Q ); mpi_init( &N );
|
||||
mpi_init( &H ); mpi_init( &D ); mpi_init( &X ); mpi_init( &Y );
|
||||
mpi_init( &Z );
|
||||
mbedtls_mpi_init( &E ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &N );
|
||||
mbedtls_mpi_init( &H ); mbedtls_mpi_init( &D ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
||||
mbedtls_mpi_init( &Z );
|
||||
|
||||
MPI_CHK( mpi_read_string( &P, 10, "2789" ) );
|
||||
MPI_CHK( mpi_read_string( &Q, 10, "3203" ) );
|
||||
MPI_CHK( mpi_read_string( &E, 10, "257" ) );
|
||||
MPI_CHK( mpi_mul_mpi( &N, &P, &Q ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P, 10, "2789" ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &Q, 10, "3203" ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 10, "257" ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &N, &P, &Q ) );
|
||||
|
||||
polarssl_printf( "\n Public key:\n\n" );
|
||||
MPI_CHK( mpi_write_file( " N = ", &N, 10, NULL ) );
|
||||
MPI_CHK( mpi_write_file( " E = ", &E, 10, NULL ) );
|
||||
mbedtls_printf( "\n Public key:\n\n" );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " N = ", &N, 10, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " E = ", &E, 10, NULL ) );
|
||||
|
||||
polarssl_printf( "\n Private key:\n\n" );
|
||||
MPI_CHK( mpi_write_file( " P = ", &P, 10, NULL ) );
|
||||
MPI_CHK( mpi_write_file( " Q = ", &Q, 10, NULL ) );
|
||||
mbedtls_printf( "\n Private key:\n\n" );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " P = ", &P, 10, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " Q = ", &Q, 10, NULL ) );
|
||||
|
||||
#if defined(POLARSSL_GENPRIME)
|
||||
MPI_CHK( mpi_sub_int( &P, &P, 1 ) );
|
||||
MPI_CHK( mpi_sub_int( &Q, &Q, 1 ) );
|
||||
MPI_CHK( mpi_mul_mpi( &H, &P, &Q ) );
|
||||
MPI_CHK( mpi_inv_mod( &D, &E, &H ) );
|
||||
#if defined(MBEDTLS_GENPRIME)
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P, &P, 1 ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q, &Q, 1 ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P, &Q ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &D, &E, &H ) );
|
||||
|
||||
mpi_write_file( " D = E^-1 mod (P-1)*(Q-1) = ",
|
||||
mbedtls_mpi_write_file( " D = E^-1 mod (P-1)*(Q-1) = ",
|
||||
&D, 10, NULL );
|
||||
#else
|
||||
polarssl_printf("\nTest skipped (POLARSSL_GENPRIME not defined).\n\n");
|
||||
mbedtls_printf("\nTest skipped (MBEDTLS_GENPRIME not defined).\n\n");
|
||||
#endif
|
||||
MPI_CHK( mpi_read_string( &X, 10, "55555" ) );
|
||||
MPI_CHK( mpi_exp_mod( &Y, &X, &E, &N, NULL ) );
|
||||
MPI_CHK( mpi_exp_mod( &Z, &Y, &D, &N, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &X, 10, "55555" ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &Y, &X, &E, &N, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &Z, &Y, &D, &N, NULL ) );
|
||||
|
||||
polarssl_printf( "\n RSA operation:\n\n" );
|
||||
MPI_CHK( mpi_write_file( " X (plaintext) = ", &X, 10, NULL ) );
|
||||
MPI_CHK( mpi_write_file( " Y (ciphertext) = X^E mod N = ", &Y, 10, NULL ) );
|
||||
MPI_CHK( mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL ) );
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n RSA operation:\n\n" );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " X (plaintext) = ", &X, 10, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " Y (ciphertext) = X^E mod N = ", &Y, 10, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL ) );
|
||||
mbedtls_printf( "\n" );
|
||||
|
||||
cleanup:
|
||||
mpi_free( &E ); mpi_free( &P ); mpi_free( &Q ); mpi_free( &N );
|
||||
mpi_free( &H ); mpi_free( &D ); mpi_free( &X ); mpi_free( &Y );
|
||||
mpi_free( &Z );
|
||||
mbedtls_mpi_free( &E ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &N );
|
||||
mbedtls_mpi_free( &H ); mbedtls_mpi_free( &D ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
||||
mbedtls_mpi_free( &Z );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
polarssl_printf( "\nAn error occured.\n" );
|
||||
mbedtls_printf( "\nAn error occured.\n" );
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_FS_IO */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -20,22 +20,22 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_PK_PARSE_C) && \
|
||||
defined(POLARSSL_FS_IO) && defined(POLARSSL_ENTROPY_C) && \
|
||||
defined(POLARSSL_CTR_DRBG_C)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
|
|
@ -46,14 +46,14 @@
|
|||
#endif
|
||||
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_PK_PARSE_C) || \
|
||||
!defined(POLARSSL_FS_IO) || !defined(POLARSSL_ENTROPY_C) || \
|
||||
!defined(POLARSSL_CTR_DRBG_C)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_PK_PARSE_C and/or "
|
||||
"POLARSSL_FS_IO and/or POLARSSL_ENTROPY_C and/or "
|
||||
"POLARSSL_CTR_DRBG_C not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
|
||||
"MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -62,12 +62,12 @@ int main( int argc, char *argv[] )
|
|||
FILE *f;
|
||||
int ret, c;
|
||||
size_t i, olen = 0;
|
||||
pk_context pk;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char result[1024];
|
||||
unsigned char buf[512];
|
||||
const char *pers = "pk_decrypt";
|
||||
const char *pers = "mbedtls_pk_decrypt";
|
||||
((void) argv);
|
||||
|
||||
memset(result, 0, sizeof( result ) );
|
||||
|
|
@ -75,35 +75,35 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( argc != 2 )
|
||||
{
|
||||
polarssl_printf( "usage: pk_decrypt <key_file>\n" );
|
||||
mbedtls_printf( "usage: mbedtls_pk_decrypt <key_file>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Seeding the random number generator..." );
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Reading private key from '%s'", argv[1] );
|
||||
mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
pk_init( &pk );
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -114,7 +114,7 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
polarssl_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -129,37 +129,37 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* Decrypt the encrypted RSA data and print the result.
|
||||
*/
|
||||
polarssl_printf( "\n . Decrypting the encrypted data" );
|
||||
mbedtls_printf( "\n . Decrypting the encrypted data" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! pk_decrypt returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_decrypt returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . OK\n\n" );
|
||||
mbedtls_printf( "\n . OK\n\n" );
|
||||
|
||||
polarssl_printf( "The decrypted result is: '%s'\n\n", result );
|
||||
mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
|
||||
|
||||
ret = 0;
|
||||
|
||||
exit:
|
||||
ctr_drbg_free( &ctr_drbg );
|
||||
entropy_free( &entropy );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(POLARSSL_ERROR_C)
|
||||
polarssl_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
polarssl_printf( " ! Last error was: %s\n", buf );
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
|
||||
POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -20,23 +20,23 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_fprintf fprintf
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_PK_PARSE_C) && \
|
||||
defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_FS_IO) && \
|
||||
defined(POLARSSL_CTR_DRBG_C)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
|
||||
defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
|
|
@ -46,14 +46,14 @@
|
|||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_PK_PARSE_C) || \
|
||||
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \
|
||||
!defined(POLARSSL_CTR_DRBG_C)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
|
||||
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_PK_PARSE_C and/or "
|
||||
"POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or "
|
||||
"POLARSSL_CTR_DRBG_C not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
|
||||
"MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -62,52 +62,52 @@ int main( int argc, char *argv[] )
|
|||
FILE *f;
|
||||
int ret;
|
||||
size_t i, olen = 0;
|
||||
pk_context pk;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char input[1024];
|
||||
unsigned char buf[512];
|
||||
const char *pers = "pk_encrypt";
|
||||
const char *pers = "mbedtls_pk_encrypt";
|
||||
|
||||
ret = 1;
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
polarssl_printf( "usage: pk_encrypt <key_file> <string of max 100 characters>\n" );
|
||||
mbedtls_printf( "usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Seeding the random number generator..." );
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_init returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Reading public key from '%s'", argv[1] );
|
||||
mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
pk_init( &pk );
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( strlen( argv[2] ) > 100 )
|
||||
{
|
||||
polarssl_printf( " Input data larger than 100 characters.\n\n" );
|
||||
mbedtls_printf( " Input data larger than 100 characters.\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -116,14 +116,14 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* Calculate the RSA encryption of the hash.
|
||||
*/
|
||||
polarssl_printf( "\n . Generating the encrypted value" );
|
||||
mbedtls_printf( "\n . Generating the encrypted value" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = pk_encrypt( &pk, input, strlen( argv[2] ),
|
||||
if( ( ret = mbedtls_pk_encrypt( &pk, input, strlen( argv[2] ),
|
||||
buf, &olen, sizeof(buf),
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! pk_encrypt returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -133,33 +133,33 @@ int main( int argc, char *argv[] )
|
|||
if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( i = 0; i < olen; i++ )
|
||||
polarssl_fprintf( f, "%02X%s", buf[i],
|
||||
mbedtls_fprintf( f, "%02X%s", buf[i],
|
||||
( i + 1 ) % 16 == 0 ? "\r\n" : " " );
|
||||
|
||||
fclose( f );
|
||||
|
||||
polarssl_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
|
||||
|
||||
exit:
|
||||
ctr_drbg_free( &ctr_drbg );
|
||||
entropy_free( &entropy );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(POLARSSL_ERROR_C)
|
||||
polarssl_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
polarssl_printf( " ! Last error was: %s\n", buf );
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_ENTROPY_C &&
|
||||
POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -20,25 +20,25 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_snprintf snprintf
|
||||
#define polarssl_printf printf
|
||||
#define polarssl_snprintf snprintf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_ENTROPY_C) && \
|
||||
defined(POLARSSL_SHA256_C) && \
|
||||
defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_FS_IO) && \
|
||||
defined(POLARSSL_CTR_DRBG_C)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_SHA256_C) && \
|
||||
defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) && \
|
||||
defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
|
@ -54,16 +54,16 @@
|
|||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
|
||||
!defined(POLARSSL_SHA256_C) || \
|
||||
!defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO) || \
|
||||
!defined(POLARSSL_CTR_DRBG_C)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || \
|
||||
!defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
|
||||
"POLARSSL_SHA256_C and/or "
|
||||
"POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO and/or "
|
||||
"POLARSSL_CTR_DRBG_C not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_SHA256_C and/or "
|
||||
"MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -71,47 +71,47 @@ int main( int argc, char *argv[] )
|
|||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
pk_context pk;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char hash[20];
|
||||
unsigned char buf[POLARSSL_MPI_MAX_SIZE];
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
char filename[512];
|
||||
const char *pers = "pk_sign";
|
||||
const char *pers = "mbedtls_pk_sign";
|
||||
size_t olen = 0;
|
||||
|
||||
entropy_init( &entropy );
|
||||
pk_init( &pk );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
polarssl_printf( "usage: pk_sign <key_file> <filename>\n" );
|
||||
mbedtls_printf( "usage: mbedtls_pk_sign <key_file> <filename>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Seeding the random number generator..." );
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_init returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Reading private key from '%s'", argv[1] );
|
||||
mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not open '%s'\n", argv[1] );
|
||||
mbedtls_printf( " failed\n ! Could not open '%s'\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -119,61 +119,61 @@ int main( int argc, char *argv[] )
|
|||
* Compute the SHA-256 hash of the input file,
|
||||
* then calculate the signature of the hash.
|
||||
*/
|
||||
polarssl_printf( "\n . Generating the SHA-256 signature" );
|
||||
mbedtls_printf( "\n . Generating the SHA-256 signature" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
|
||||
if( ( ret = mbedtls_sha1_file( argv[2], hash ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
|
||||
mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = pk_sign( &pk, POLARSSL_MD_SHA256, hash, 0, buf, &olen,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! pk_sign returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_sign returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write the signature into <filename>-sig.txt
|
||||
*/
|
||||
polarssl_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
|
||||
mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
|
||||
|
||||
if( ( f = fopen( filename, "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not create %s\n\n", filename );
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( fwrite( buf, 1, olen, f ) != olen )
|
||||
{
|
||||
polarssl_printf( "failed\n ! fwrite failed\n\n" );
|
||||
mbedtls_printf( "failed\n ! fwrite failed\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
polarssl_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
|
||||
exit:
|
||||
pk_free( &pk );
|
||||
ctr_drbg_free( &ctr_drbg );
|
||||
entropy_free( &entropy );
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(POLARSSL_ERROR_C)
|
||||
polarssl_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
polarssl_printf( " ! Last error was: %s\n", buf );
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C &&
|
||||
POLARSSL_SHA256_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
|
||||
POLARSSL_CTR_DRBG_C */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -20,24 +20,24 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_snprintf snprintf
|
||||
#define polarssl_printf printf
|
||||
#define polarssl_snprintf snprintf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && \
|
||||
defined(POLARSSL_SHA256_C) && defined(POLARSSL_PK_PARSE_C) && \
|
||||
defined(POLARSSL_FS_IO)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && \
|
||||
defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_PK_PARSE_C) && \
|
||||
defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/pk.h"
|
||||
|
|
@ -51,14 +51,14 @@
|
|||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || \
|
||||
!defined(POLARSSL_SHA256_C) || !defined(POLARSSL_PK_PARSE_C) || \
|
||||
!defined(POLARSSL_FS_IO)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \
|
||||
!defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or "
|
||||
"POLARSSL_SHA256_C and/or POLARSSL_PK_PARSE_C and/or "
|
||||
"POLARSSL_FS_IO not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
|
||||
"MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or "
|
||||
"MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -67,30 +67,30 @@ int main( int argc, char *argv[] )
|
|||
FILE *f;
|
||||
int ret = 1;
|
||||
size_t i;
|
||||
pk_context pk;
|
||||
mbedtls_pk_context pk;
|
||||
unsigned char hash[20];
|
||||
unsigned char buf[POLARSSL_MPI_MAX_SIZE];
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
char filename[512];
|
||||
|
||||
pk_init( &pk );
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
polarssl_printf( "usage: pk_verify <key_file> <filename>\n" );
|
||||
mbedtls_printf( "usage: mbedtls_pk_verify <key_file> <filename>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Reading public key from '%s'", argv[1] );
|
||||
mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -98,11 +98,11 @@ int main( int argc, char *argv[] )
|
|||
* Extract the signature from the text file
|
||||
*/
|
||||
ret = 1;
|
||||
polarssl_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
|
||||
mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
{
|
||||
polarssl_printf( "\n ! Could not open %s\n\n", filename );
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -115,40 +115,40 @@ int main( int argc, char *argv[] )
|
|||
* Compute the SHA-256 hash of the input file and compare
|
||||
* it with the hash decrypted from the signature.
|
||||
*/
|
||||
polarssl_printf( "\n . Verifying the SHA-256 signature" );
|
||||
mbedtls_printf( "\n . Verifying the SHA-256 signature" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
|
||||
if( ( ret = mbedtls_sha1_file( argv[2], hash ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
|
||||
mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = pk_verify( &pk, POLARSSL_MD_SHA256, hash, 0,
|
||||
if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
|
||||
buf, i ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! pk_verify returned -0x%04x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_verify returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
|
||||
mbedtls_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
|
||||
|
||||
ret = 0;
|
||||
|
||||
exit:
|
||||
pk_free( &pk );
|
||||
mbedtls_pk_free( &pk );
|
||||
|
||||
#if defined(POLARSSL_ERROR_C)
|
||||
polarssl_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
polarssl_printf( " ! Last error was: %s\n", buf );
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_SHA256_C &&
|
||||
POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -20,22 +20,22 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) && \
|
||||
defined(POLARSSL_FS_IO) && defined(POLARSSL_ENTROPY_C) && \
|
||||
defined(POLARSSL_CTR_DRBG_C)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
|
@ -44,14 +44,14 @@
|
|||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
|
||||
!defined(POLARSSL_FS_IO) || !defined(POLARSSL_ENTROPY_C) || \
|
||||
!defined(POLARSSL_CTR_DRBG_C)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
|
||||
"POLARSSL_FS_IO and/or POLARSSL_ENTROPY_C and/or "
|
||||
"POLARSSL_CTR_DRBG_C not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
|
||||
"MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -60,9 +60,9 @@ int main( int argc, char *argv[] )
|
|||
FILE *f;
|
||||
int ret, c;
|
||||
size_t i;
|
||||
rsa_context rsa;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char result[1024];
|
||||
unsigned char buf[512];
|
||||
const char *pers = "rsa_decrypt";
|
||||
|
|
@ -73,53 +73,53 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( argc != 1 )
|
||||
{
|
||||
polarssl_printf( "usage: rsa_decrypt\n" );
|
||||
mbedtls_printf( "usage: rsa_decrypt\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Seeding the random number generator..." );
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Reading private key from rsa_priv.txt" );
|
||||
mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
polarssl_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
|
||||
if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
|
||||
fclose( f );
|
||||
|
||||
|
|
@ -130,7 +130,7 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
polarssl_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -144,39 +144,39 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( i != rsa.len )
|
||||
{
|
||||
polarssl_printf( "\n ! Invalid RSA signature format\n\n" );
|
||||
mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Decrypt the encrypted RSA data and print the result.
|
||||
*/
|
||||
polarssl_printf( "\n . Decrypting the encrypted data" );
|
||||
mbedtls_printf( "\n . Decrypting the encrypted data" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = rsa_pkcs1_decrypt( &rsa, ctr_drbg_random, &ctr_drbg,
|
||||
RSA_PRIVATE, &i, buf, result,
|
||||
if( ( ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg,
|
||||
MBEDTLS_RSA_PRIVATE, &i, buf, result,
|
||||
1024 ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . OK\n\n" );
|
||||
mbedtls_printf( "\n . OK\n\n" );
|
||||
|
||||
polarssl_printf( "The decrypted result is: '%s'\n\n", result );
|
||||
mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
|
||||
|
||||
ret = 0;
|
||||
|
||||
exit:
|
||||
ctr_drbg_free( &ctr_drbg );
|
||||
entropy_free( &entropy );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_FS_IO */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -20,23 +20,23 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_fprintf fprintf
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) && \
|
||||
defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_FS_IO) && \
|
||||
defined(POLARSSL_CTR_DRBG_C)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
|
||||
defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
|
@ -45,14 +45,14 @@
|
|||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
|
||||
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_FS_IO) || \
|
||||
!defined(POLARSSL_CTR_DRBG_C)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
|
||||
"POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO and/or "
|
||||
"POLARSSL_CTR_DRBG_C not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
|
||||
"MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -61,9 +61,9 @@ int main( int argc, char *argv[] )
|
|||
FILE *f;
|
||||
int ret;
|
||||
size_t i;
|
||||
rsa_context rsa;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char input[1024];
|
||||
unsigned char buf[512];
|
||||
const char *pers = "rsa_encrypt";
|
||||
|
|
@ -72,54 +72,54 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( argc != 2 )
|
||||
{
|
||||
polarssl_printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
|
||||
mbedtls_printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Seeding the random number generator..." );
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Reading public key from rsa_pub.txt" );
|
||||
mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
|
||||
if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
|
||||
fclose( f );
|
||||
|
||||
if( strlen( argv[1] ) > 100 )
|
||||
{
|
||||
polarssl_printf( " Input data larger than 100 characters.\n\n" );
|
||||
mbedtls_printf( " Input data larger than 100 characters.\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -128,14 +128,14 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* Calculate the RSA encryption of the hash.
|
||||
*/
|
||||
polarssl_printf( "\n . Generating the RSA encrypted value" );
|
||||
mbedtls_printf( "\n . Generating the RSA encrypted value" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = rsa_pkcs1_encrypt( &rsa, ctr_drbg_random, &ctr_drbg,
|
||||
RSA_PUBLIC, strlen( argv[1] ),
|
||||
if( ( ret = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg,
|
||||
MBEDTLS_RSA_PUBLIC, strlen( argv[1] ),
|
||||
input, buf ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! rsa_pkcs1_encrypt returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -145,28 +145,28 @@ int main( int argc, char *argv[] )
|
|||
if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( i = 0; i < rsa.len; i++ )
|
||||
polarssl_fprintf( f, "%02X%s", buf[i],
|
||||
mbedtls_fprintf( f, "%02X%s", buf[i],
|
||||
( i + 1 ) % 16 == 0 ? "\r\n" : " " );
|
||||
|
||||
fclose( f );
|
||||
|
||||
polarssl_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
|
||||
|
||||
exit:
|
||||
ctr_drbg_free( &ctr_drbg );
|
||||
entropy_free( &entropy );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_ENTROPY_C &&
|
||||
POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -20,22 +20,22 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_ENTROPY_C) && \
|
||||
defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME) && \
|
||||
defined(POLARSSL_FS_IO) && defined(POLARSSL_CTR_DRBG_C)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/bignum.h"
|
||||
|
|
@ -49,92 +49,92 @@
|
|||
#define KEY_SIZE 1024
|
||||
#define EXPONENT 65537
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
|
||||
!defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) || \
|
||||
!defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
|
||||
"POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
|
||||
"POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
|
||||
"MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
int main( void )
|
||||
{
|
||||
int ret;
|
||||
rsa_context rsa;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
FILE *fpub = NULL;
|
||||
FILE *fpriv = NULL;
|
||||
const char *pers = "rsa_genkey";
|
||||
|
||||
polarssl_printf( "\n . Seeding the random number generator..." );
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
entropy_init( &entropy );
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
|
||||
mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
|
||||
fflush( stdout );
|
||||
|
||||
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
|
||||
if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE,
|
||||
if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
|
||||
EXPONENT ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
|
||||
mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
polarssl_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
|
||||
mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
|
||||
( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
|
||||
mbedtls_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
polarssl_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
|
||||
mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
|
||||
( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
|
||||
( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
|
||||
( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
|
||||
( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
|
||||
( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
|
||||
( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
|
||||
( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
/*
|
||||
polarssl_printf( " ok\n . Generating the certificate..." );
|
||||
mbedtls_printf( " ok\n . Generating the certificate..." );
|
||||
|
||||
x509write_init_raw( &cert );
|
||||
x509write_add_pubkey( &cert, &rsa );
|
||||
|
|
@ -146,7 +146,7 @@ int main( void )
|
|||
x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
|
||||
x509write_free_raw( &cert );
|
||||
*/
|
||||
polarssl_printf( " ok\n\n" );
|
||||
mbedtls_printf( " ok\n\n" );
|
||||
|
||||
exit:
|
||||
|
||||
|
|
@ -156,16 +156,16 @@ exit:
|
|||
if( fpriv != NULL )
|
||||
fclose( fpriv );
|
||||
|
||||
rsa_free( &rsa );
|
||||
ctr_drbg_free( &ctr_drbg );
|
||||
entropy_free( &entropy );
|
||||
mbedtls_rsa_free( &rsa );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_RSA_C &&
|
||||
POLARSSL_GENPRIME && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
|
||||
MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -20,22 +20,22 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_fprintf fprintf
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) && \
|
||||
defined(POLARSSL_SHA256_C) && defined(POLARSSL_FS_IO)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||
defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
|
||||
|
|
@ -43,12 +43,12 @@
|
|||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
|
||||
!defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
|
||||
"POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
|
||||
"MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -57,58 +57,58 @@ int main( int argc, char *argv[] )
|
|||
FILE *f;
|
||||
int ret;
|
||||
size_t i;
|
||||
rsa_context rsa;
|
||||
mbedtls_rsa_context rsa;
|
||||
unsigned char hash[20];
|
||||
unsigned char buf[POLARSSL_MPI_MAX_SIZE];
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
|
||||
ret = 1;
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
polarssl_printf( "usage: rsa_sign <filename>\n" );
|
||||
mbedtls_printf( "usage: rsa_sign <filename>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Reading private key from rsa_priv.txt" );
|
||||
mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
|
||||
if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
|
||||
fclose( f );
|
||||
|
||||
polarssl_printf( "\n . Checking the private key" );
|
||||
mbedtls_printf( "\n . Checking the private key" );
|
||||
fflush( stdout );
|
||||
if( ( ret = rsa_check_privkey( &rsa ) ) != 0 )
|
||||
if( ( ret = mbedtls_rsa_check_privkey( &rsa ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! rsa_check_privkey failed with -0x%0x\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_check_privkey failed with -0x%0x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -116,19 +116,19 @@ int main( int argc, char *argv[] )
|
|||
* Compute the SHA-256 hash of the input file,
|
||||
* then calculate the RSA signature of the hash.
|
||||
*/
|
||||
polarssl_printf( "\n . Generating the RSA/SHA-256 signature" );
|
||||
mbedtls_printf( "\n . Generating the RSA/SHA-256 signature" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
|
||||
if( ( ret = mbedtls_sha1_file( argv[1], hash ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
|
||||
mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_SHA256,
|
||||
if( ( ret = mbedtls_rsa_pkcs1_sign( &rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256,
|
||||
20, hash, buf ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -140,26 +140,26 @@ int main( int argc, char *argv[] )
|
|||
if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not create %s\n\n", argv[1] );
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( i = 0; i < rsa.len; i++ )
|
||||
polarssl_fprintf( f, "%02X%s", buf[i],
|
||||
mbedtls_fprintf( f, "%02X%s", buf[i],
|
||||
( i + 1 ) % 16 == 0 ? "\r\n" : " " );
|
||||
|
||||
fclose( f );
|
||||
|
||||
polarssl_printf( "\n . Done (created \"%s\")\n\n", argv[1] );
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", argv[1] );
|
||||
|
||||
exit:
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA256_C &&
|
||||
POLARSSL_FS_IO */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -20,25 +20,25 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_snprintf snprintf
|
||||
#define polarssl_printf printf
|
||||
#define polarssl_snprintf snprintf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_ENTROPY_C) && \
|
||||
defined(POLARSSL_RSA_C) && defined(POLARSSL_SHA256_C) && \
|
||||
defined(POLARSSL_PK_PARSE_C) && defined(POLARSSL_FS_IO) && \
|
||||
defined(POLARSSL_CTR_DRBG_C)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) && \
|
||||
defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) && \
|
||||
defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/md.h"
|
||||
|
|
@ -54,16 +54,16 @@
|
|||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
|
||||
!defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA256_C) || \
|
||||
!defined(POLARSSL_PK_PARSE_C) || !defined(POLARSSL_FS_IO) || \
|
||||
!defined(POLARSSL_CTR_DRBG_C)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
!defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
|
||||
"POLARSSL_RSA_C and/or POLARSSL_SHA256_C and/or "
|
||||
"POLARSSL_PK_PARSE_C and/or POLARSSL_FS_IO and/or "
|
||||
"POLARSSL_CTR_DRBG_C not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
|
||||
"MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -71,114 +71,114 @@ int main( int argc, char *argv[] )
|
|||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
pk_context pk;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char hash[20];
|
||||
unsigned char buf[POLARSSL_MPI_MAX_SIZE];
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
char filename[512];
|
||||
const char *pers = "rsa_sign_pss";
|
||||
size_t olen = 0;
|
||||
|
||||
entropy_init( &entropy );
|
||||
pk_init( &pk );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
polarssl_printf( "usage: rsa_sign_pss <key_file> <filename>\n" );
|
||||
mbedtls_printf( "usage: rsa_sign_pss <key_file> <filename>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Seeding the random number generator..." );
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
|
||||
if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_init returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Reading private key from '%s'", argv[1] );
|
||||
mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
|
||||
polarssl_printf( " ! pk_parse_public_keyfile returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
|
||||
mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( !pk_can_do( &pk, POLARSSL_PK_RSA ) )
|
||||
if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Key is not an RSA key\n" );
|
||||
mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa_set_padding( pk_rsa( pk ), RSA_PKCS_V21, POLARSSL_MD_SHA256 );
|
||||
mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
|
||||
|
||||
/*
|
||||
* Compute the SHA-256 hash of the input file,
|
||||
* then calculate the RSA signature of the hash.
|
||||
*/
|
||||
polarssl_printf( "\n . Generating the RSA/SHA-256 signature" );
|
||||
mbedtls_printf( "\n . Generating the RSA/SHA-256 signature" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
|
||||
if( ( ret = mbedtls_sha1_file( argv[2], hash ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
|
||||
mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = pk_sign( &pk, POLARSSL_MD_SHA256, hash, 0, buf, &olen,
|
||||
ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! pk_sign returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_sign returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write the signature into <filename>-sig.txt
|
||||
*/
|
||||
polarssl_snprintf( filename, 512, "%s.sig", argv[2] );
|
||||
mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
|
||||
|
||||
if( ( f = fopen( filename, "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Could not create %s\n\n", filename );
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( fwrite( buf, 1, olen, f ) != olen )
|
||||
{
|
||||
polarssl_printf( "failed\n ! fwrite failed\n\n" );
|
||||
mbedtls_printf( "failed\n ! fwrite failed\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
polarssl_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
|
||||
exit:
|
||||
pk_free( &pk );
|
||||
ctr_drbg_free( &ctr_drbg );
|
||||
entropy_free( &entropy );
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_RSA_C &&
|
||||
POLARSSL_SHA256_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
|
||||
POLARSSL_CTR_DRBG_C */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
|
||||
MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -20,21 +20,21 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) && \
|
||||
defined(POLARSSL_SHA256_C) && defined(POLARSSL_FS_IO)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||
defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
|
||||
|
|
@ -42,12 +42,12 @@
|
|||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
|
||||
!defined(POLARSSL_SHA256_C) || !defined(POLARSSL_FS_IO)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
|
||||
"POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
|
||||
"MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -56,42 +56,42 @@ int main( int argc, char *argv[] )
|
|||
FILE *f;
|
||||
int ret, c;
|
||||
size_t i;
|
||||
rsa_context rsa;
|
||||
mbedtls_rsa_context rsa;
|
||||
unsigned char hash[20];
|
||||
unsigned char buf[POLARSSL_MPI_MAX_SIZE];
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
|
||||
ret = 1;
|
||||
if( argc != 2 )
|
||||
{
|
||||
polarssl_printf( "usage: rsa_verify <filename>\n" );
|
||||
mbedtls_printf( "usage: rsa_verify <filename>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Reading public key from rsa_pub.txt" );
|
||||
mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
polarssl_printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa_init( &rsa, RSA_PKCS_V15, 0 );
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
|
||||
if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
|
||||
( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3;
|
||||
|
||||
fclose( f );
|
||||
|
||||
|
|
@ -104,7 +104,7 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( f = fopen( argv[1], "rb" ) ) == NULL )
|
||||
{
|
||||
polarssl_printf( "\n ! Could not open %s\n\n", argv[1] );
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -118,7 +118,7 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( i != rsa.len )
|
||||
{
|
||||
polarssl_printf( "\n ! Invalid RSA signature format\n\n" );
|
||||
mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -126,34 +126,34 @@ int main( int argc, char *argv[] )
|
|||
* Compute the SHA-256 hash of the input file and compare
|
||||
* it with the hash decrypted from the RSA signature.
|
||||
*/
|
||||
polarssl_printf( "\n . Verifying the RSA/SHA-256 signature" );
|
||||
mbedtls_printf( "\n . Verifying the RSA/SHA-256 signature" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = sha1_file( argv[1], hash ) ) != 0 )
|
||||
if( ( ret = mbedtls_sha1_file( argv[1], hash ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
|
||||
mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC,
|
||||
POLARSSL_MD_SHA256, 20, hash, buf ) ) != 0 )
|
||||
if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
|
||||
MBEDTLS_MD_SHA256, 20, hash, buf ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! rsa_pkcs1_verify returned -0x%0x\n\n", -ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
|
||||
mbedtls_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
|
||||
|
||||
ret = 0;
|
||||
|
||||
exit:
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA256_C &&
|
||||
POLARSSL_FS_IO */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -20,24 +20,24 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_snprintf snprintf
|
||||
#define polarssl_printf printf
|
||||
#define polarssl_snprintf snprintf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_RSA_C) && \
|
||||
defined(POLARSSL_SHA256_C) && defined(POLARSSL_PK_PARSE_C) && \
|
||||
defined(POLARSSL_FS_IO)
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||
defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_PK_PARSE_C) && \
|
||||
defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/pem.h"
|
||||
#include "mbedtls/pk.h"
|
||||
|
|
@ -52,14 +52,14 @@
|
|||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
||||
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
|
||||
!defined(POLARSSL_SHA256_C) || !defined(POLARSSL_PK_PARSE_C) || \
|
||||
!defined(POLARSSL_FS_IO)
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \
|
||||
!defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
|
||||
"POLARSSL_SHA256_C and/or POLARSSL_PK_PARSE_C and/or "
|
||||
"POLARSSL_FS_IO not defined.\n");
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
|
||||
"MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or "
|
||||
"MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
|
@ -68,57 +68,57 @@ int main( int argc, char *argv[] )
|
|||
FILE *f;
|
||||
int ret = 1;
|
||||
size_t i;
|
||||
pk_context pk;
|
||||
mbedtls_pk_context pk;
|
||||
unsigned char hash[20];
|
||||
unsigned char buf[POLARSSL_MPI_MAX_SIZE];
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
char filename[512];
|
||||
|
||||
pk_init( &pk );
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
polarssl_printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
|
||||
mbedtls_printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . Reading public key from '%s'", argv[1] );
|
||||
mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
|
||||
if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
|
||||
polarssl_printf( " ! pk_parse_public_keyfile returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
|
||||
mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( !pk_can_do( &pk, POLARSSL_PK_RSA ) )
|
||||
if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
|
||||
{
|
||||
ret = 1;
|
||||
polarssl_printf( " failed\n ! Key is not an RSA key\n" );
|
||||
mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa_set_padding( pk_rsa( pk ), RSA_PKCS_V21, POLARSSL_MD_SHA256 );
|
||||
mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
|
||||
|
||||
/*
|
||||
* Extract the RSA signature from the text file
|
||||
*/
|
||||
ret = 1;
|
||||
polarssl_snprintf( filename, 512, "%s.sig", argv[2] );
|
||||
mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
{
|
||||
polarssl_printf( "\n ! Could not open %s\n\n", filename );
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
||||
i = fread( buf, 1, POLARSSL_MPI_MAX_SIZE, f );
|
||||
i = fread( buf, 1, MBEDTLS_MPI_MAX_SIZE, f );
|
||||
|
||||
fclose( f );
|
||||
|
||||
|
|
@ -126,35 +126,35 @@ int main( int argc, char *argv[] )
|
|||
* Compute the SHA-256 hash of the input file and compare
|
||||
* it with the hash decrypted from the RSA signature.
|
||||
*/
|
||||
polarssl_printf( "\n . Verifying the RSA/SHA-256 signature" );
|
||||
mbedtls_printf( "\n . Verifying the RSA/SHA-256 signature" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
|
||||
if( ( ret = mbedtls_sha1_file( argv[2], hash ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
|
||||
mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = pk_verify( &pk, POLARSSL_MD_SHA256, hash, 0,
|
||||
if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
|
||||
buf, i ) ) != 0 )
|
||||
{
|
||||
polarssl_printf( " failed\n ! pk_verify returned %d\n\n", ret );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_verify returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
polarssl_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
|
||||
mbedtls_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" );
|
||||
|
||||
ret = 0;
|
||||
|
||||
exit:
|
||||
pk_free( &pk );
|
||||
mbedtls_pk_free( &pk );
|
||||
|
||||
#if defined(_WIN32)
|
||||
polarssl_printf( " + Press Enter to exit this program.\n" );
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA256_C &&
|
||||
POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue