mirror of
https://git.suyu.dev/suyu/mbedtls.git
synced 2025-12-30 11:16:25 +01:00
Merge remote-tracking branch 'public/pr/532' into development
This commit is contained in:
commit
fad547072a
37 changed files with 461 additions and 323 deletions
|
|
@ -29,9 +29,12 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
|
||||
|
|
@ -71,7 +74,8 @@ int main( void )
|
|||
{
|
||||
FILE *f;
|
||||
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t n, buflen;
|
||||
mbedtls_net_context server_fd;
|
||||
|
||||
|
|
@ -115,7 +119,6 @@ int main( void )
|
|||
|
||||
if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
|
|
@ -191,7 +194,6 @@ int main( void )
|
|||
|
||||
if( dhm.len < 64 || dhm.len > 512 )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Invalid DHM modulus size\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -207,7 +209,6 @@ int main( void )
|
|||
|
||||
if( ( n = (size_t) ( end - p ) ) != rsa.len )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Invalid RSA signature size\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -286,6 +287,8 @@ int main( void )
|
|||
buf[16] = '\0';
|
||||
mbedtls_printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_net_free( &server_fd );
|
||||
|
|
@ -301,7 +304,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
|
|
|
|||
|
|
@ -30,9 +30,11 @@
|
|||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#endif
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
|
||||
|
|
@ -69,6 +71,7 @@ int main( void )
|
|||
int main( int argc, char **argv )
|
||||
{
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_mpi G, P, Q;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
|
|
@ -86,7 +89,7 @@ int main( int argc, char **argv )
|
|||
{
|
||||
usage:
|
||||
mbedtls_printf( USAGE );
|
||||
return( 1 );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
for( i = 1; i < argc; i++ )
|
||||
|
|
@ -164,7 +167,6 @@ int main( int argc, char **argv )
|
|||
|
||||
if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -180,6 +182,8 @@ int main( int argc, char **argv )
|
|||
mbedtls_printf( " ok\n\n" );
|
||||
fclose( fout );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
|
|
@ -191,7 +195,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */
|
||||
|
|
|
|||
|
|
@ -29,9 +29,12 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
|
||||
|
|
@ -71,7 +74,8 @@ int main( void )
|
|||
{
|
||||
FILE *f;
|
||||
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t n, buflen;
|
||||
mbedtls_net_context listen_fd, client_fd;
|
||||
|
||||
|
|
@ -121,7 +125,6 @@ int main( void )
|
|||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
|
|
@ -164,7 +167,6 @@ int main( void )
|
|||
|
||||
if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not open dh_prime.txt\n" \
|
||||
" ! Please run dh_genprime first\n\n" );
|
||||
goto exit;
|
||||
|
|
@ -304,6 +306,8 @@ int main( void )
|
|||
|
||||
mbedtls_printf( "\n\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
|
|
@ -323,7 +327,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
|
|
|
|||
|
|
@ -29,8 +29,11 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_ECDH_C) || \
|
||||
!defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
|
||||
|
|
@ -51,7 +54,8 @@ int main( void )
|
|||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_ecdh_context ctx_cli, ctx_srv;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
|
|
@ -218,6 +222,7 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
|
|
@ -231,7 +236,7 @@ exit:
|
|||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
return( ret != 0 );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
|
||||
MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -29,8 +29,11 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
|
||||
|
|
@ -98,7 +101,8 @@ static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key )
|
|||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_ecdsa_context ctx_sign, ctx_verify;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
|
|
@ -115,7 +119,6 @@ int main( int argc, char *argv[] )
|
|||
|
||||
memset( sig, 0, sizeof( sig ) );
|
||||
memset( message, 0x25, sizeof( message ) );
|
||||
ret = 1;
|
||||
|
||||
if( argc != 1 )
|
||||
{
|
||||
|
|
@ -213,8 +216,6 @@ int main( int argc, char *argv[] )
|
|||
goto exit;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
/*
|
||||
* Verify signature
|
||||
*/
|
||||
|
|
@ -231,6 +232,8 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
|
@ -243,7 +246,7 @@ exit:
|
|||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
|
||||
ECPARAMS */
|
||||
|
|
|
|||
|
|
@ -29,8 +29,11 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
|
||||
|
|
@ -186,7 +189,8 @@ static int write_private_key( mbedtls_pk_context *key, const char *output_file )
|
|||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_pk_context key;
|
||||
char buf[1024];
|
||||
int i;
|
||||
|
|
@ -214,7 +218,6 @@ int main( int argc, char *argv[] )
|
|||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
ret = 1;
|
||||
mbedtls_printf( USAGE );
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
mbedtls_printf( " available ec_curve values:\n" );
|
||||
|
|
@ -222,7 +225,7 @@ int main( int argc, char *argv[] )
|
|||
mbedtls_printf( " %s (default)\n", curve_info->name );
|
||||
while( ( ++curve_info )->name != NULL )
|
||||
mbedtls_printf( " %s\n", curve_info->name );
|
||||
#endif
|
||||
#endif /* MBEDTLS_ECP_C */
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -411,9 +414,11 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
if( ret != 0 && ret != 1)
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
mbedtls_strerror( ret, buf, sizeof( buf ) );
|
||||
|
|
@ -436,7 +441,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
|
||||
* MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -29,8 +29,11 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && \
|
||||
defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
|
||||
|
|
@ -83,7 +86,8 @@ struct options
|
|||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
char buf[1024];
|
||||
int i;
|
||||
char *p, *q;
|
||||
|
|
@ -283,10 +287,12 @@ int main( int argc, char *argv[] )
|
|||
else
|
||||
goto usage;
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
|
|
@ -303,6 +309,6 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -29,8 +29,11 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/error.h"
|
||||
|
|
@ -189,7 +192,8 @@ static int write_private_key( mbedtls_pk_context *key, const char *output_file )
|
|||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
char buf[1024];
|
||||
int i;
|
||||
char *p, *q;
|
||||
|
|
@ -210,7 +214,6 @@ int main( int argc, char *argv[] )
|
|||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
ret = 1;
|
||||
mbedtls_printf( USAGE );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -403,9 +406,11 @@ int main( int argc, char *argv[] )
|
|||
write_private_key( &key, opt.output_file );
|
||||
}
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
if( ret != 0 && ret != 1)
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
mbedtls_strerror( ret, buf, sizeof( buf ) );
|
||||
|
|
@ -426,6 +431,6 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -29,8 +29,11 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/bignum.h"
|
||||
|
|
@ -47,7 +50,8 @@ int main( void )
|
|||
#else
|
||||
int main( void )
|
||||
{
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_mpi E, P, Q, N, H, D, X, Y, Z;
|
||||
|
||||
mbedtls_mpi_init( &E ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &N );
|
||||
|
|
@ -88,15 +92,16 @@ int main( void )
|
|||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL ) );
|
||||
mbedtls_printf( "\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
cleanup:
|
||||
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 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_printf( "\nAn error occurred.\n" );
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
|
@ -104,6 +109,6 @@ cleanup:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -29,8 +29,11 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
|
|
@ -59,7 +62,8 @@ int main( void )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret, c;
|
||||
int ret = 1, c;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i, olen = 0;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
|
|
@ -71,7 +75,6 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
memset(result, 0, sizeof( result ) );
|
||||
ret = 1;
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
|
|
@ -110,8 +113,6 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* Extract the RSA encrypted value from the text file
|
||||
*/
|
||||
ret = 1;
|
||||
|
||||
if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
|
||||
|
|
@ -143,14 +144,14 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
|
||||
|
||||
ret = 0;
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
|
|
@ -162,7 +163,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -29,9 +29,12 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
|
||||
|
|
@ -59,7 +62,8 @@ int main( void )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i, olen = 0;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
|
|
@ -68,7 +72,6 @@ int main( int argc, char *argv[] )
|
|||
unsigned char buf[512];
|
||||
const char *pers = "mbedtls_pk_encrypt";
|
||||
|
||||
ret = 1;
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
|
||||
if( argc != 3 )
|
||||
|
|
@ -132,7 +135,6 @@ int main( int argc, char *argv[] )
|
|||
*/
|
||||
if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -145,12 +147,14 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
|
|
@ -162,7 +166,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -30,9 +30,11 @@
|
|||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
|
||||
|
|
@ -61,6 +63,7 @@ int main( int argc, char *argv[] )
|
|||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
|
|
@ -134,14 +137,12 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( f = fopen( filename, "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( fwrite( buf, 1, olen, f ) != olen )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( "failed\n ! fwrite failed\n\n" );
|
||||
fclose( f );
|
||||
goto exit;
|
||||
|
|
@ -151,13 +152,15 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
|
|
@ -169,7 +172,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret ? EXIT_FAILURE : EXIT_SUCCESS );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
|
|
|
|||
|
|
@ -29,9 +29,12 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \
|
||||
|
|
@ -56,6 +59,7 @@ int main( int argc, char *argv[] )
|
|||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_pk_context pk;
|
||||
unsigned char hash[32];
|
||||
|
|
@ -87,7 +91,6 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* Extract the signature from the file
|
||||
*/
|
||||
ret = 1;
|
||||
mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
|
|
@ -125,13 +128,13 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
|
||||
|
||||
ret = 0;
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free( &pk );
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( ret != 0 )
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
|
|
@ -143,7 +146,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -30,11 +30,11 @@
|
|||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
|
|
@ -61,7 +61,9 @@ int main( void )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int return_val, exit_val, c;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
int c;
|
||||
size_t i;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
|
||||
|
|
@ -73,7 +75,6 @@ int main( int argc, char *argv[] )
|
|||
((void) argv);
|
||||
|
||||
memset(result, 0, sizeof( result ) );
|
||||
exit_val = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
if( argc != 1 )
|
||||
{
|
||||
|
|
@ -83,7 +84,7 @@ int main( int argc, char *argv[] )
|
|||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
mbedtls_exit( exit_code );
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
|
|
@ -96,14 +97,13 @@ int main( int argc, char *argv[] )
|
|||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
||||
|
||||
return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
&entropy, (const unsigned char *) pers,
|
||||
strlen( pers ) );
|
||||
if( return_val != 0 )
|
||||
if( ret != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -112,40 +112,38 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( return_val = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
fclose( f );
|
||||
goto exit;
|
||||
}
|
||||
fclose( f );
|
||||
|
||||
if( ( return_val = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
|
||||
if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( return_val = mbedtls_rsa_complete( &rsa ) ) != 0 )
|
||||
if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_complete returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -154,7 +152,6 @@ int main( int argc, char *argv[] )
|
|||
*/
|
||||
if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -169,7 +166,6 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( i != rsa.len )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -180,14 +176,13 @@ int main( int argc, char *argv[] )
|
|||
mbedtls_printf( "\n . Decrypting the encrypted data" );
|
||||
fflush( stdout );
|
||||
|
||||
return_val = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
|
||||
ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
|
||||
&ctr_drbg, MBEDTLS_RSA_PRIVATE, &i,
|
||||
buf, result, 1024 );
|
||||
if( return_val != 0 )
|
||||
if( ret != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -195,6 +190,8 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
|
@ -208,6 +205,6 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_val );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -30,12 +30,12 @@
|
|||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
|
||||
|
|
@ -61,7 +61,8 @@ int main( void )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int return_val, exit_val;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_entropy_context entropy;
|
||||
|
|
@ -71,8 +72,6 @@ int main( int argc, char *argv[] )
|
|||
const char *pers = "rsa_encrypt";
|
||||
mbedtls_mpi N, E;
|
||||
|
||||
exit_val = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
mbedtls_printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
|
||||
|
|
@ -81,7 +80,7 @@ int main( int argc, char *argv[] )
|
|||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
mbedtls_exit( exit_code );
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
|
|
@ -92,14 +91,13 @@ int main( int argc, char *argv[] )
|
|||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
|
||||
return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
&entropy, (const unsigned char *) pers,
|
||||
strlen( pers ) );
|
||||
if( return_val != 0 )
|
||||
ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
&entropy, (const unsigned char *) pers,
|
||||
strlen( pers ) );
|
||||
if( ret != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -108,35 +106,30 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( return_val = mbedtls_mpi_read_file( &N, 16, f ) ) != 0 ||
|
||||
( return_val = mbedtls_mpi_read_file( &E, 16, f ) ) != 0 )
|
||||
if( ( ret = mbedtls_mpi_read_file( &N, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &E, 16, f ) ) != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
fclose( f );
|
||||
goto exit;
|
||||
}
|
||||
fclose( f );
|
||||
|
||||
if( ( return_val = mbedtls_rsa_import( &rsa, &N, NULL,
|
||||
NULL, NULL, &E ) ) != 0 )
|
||||
if( ( ret = mbedtls_rsa_import( &rsa, &N, NULL, NULL, NULL, &E ) ) != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( strlen( argv[1] ) > 100 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " Input data larger than 100 characters.\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -149,14 +142,13 @@ int main( int argc, char *argv[] )
|
|||
mbedtls_printf( "\n . Generating the RSA encrypted value" );
|
||||
fflush( stdout );
|
||||
|
||||
return_val = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random,
|
||||
&ctr_drbg, MBEDTLS_RSA_PUBLIC,
|
||||
strlen( argv[1] ), input, buf );
|
||||
if( return_val != 0 )
|
||||
ret = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random,
|
||||
&ctr_drbg, MBEDTLS_RSA_PUBLIC,
|
||||
strlen( argv[1] ), input, buf );
|
||||
if( ret != 0 )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
|
||||
return_val );
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -165,7 +157,6 @@ int main( int argc, char *argv[] )
|
|||
*/
|
||||
if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -178,6 +169,8 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
|
|
@ -189,7 +182,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_val );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -29,8 +29,11 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
|
||||
|
|
@ -61,7 +64,8 @@ int main( void )
|
|||
#else
|
||||
int main( void )
|
||||
{
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
|
|
@ -105,14 +109,12 @@ int main( void )
|
|||
( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -129,7 +131,6 @@ int main( void )
|
|||
if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
|
@ -160,6 +161,8 @@ int main( void )
|
|||
*/
|
||||
mbedtls_printf( " ok\n\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
if( fpub != NULL )
|
||||
|
|
@ -180,7 +183,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
|
||||
MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
||||
|
|
|
|||
|
|
@ -29,10 +29,13 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
|
||||
|
|
@ -55,7 +58,8 @@ int main( void )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_rsa_context rsa;
|
||||
unsigned char hash[32];
|
||||
|
|
@ -69,8 +73,6 @@ int main( int argc, char *argv[] )
|
|||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
||||
|
||||
ret = 1;
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
mbedtls_printf( "usage: rsa_sign <filename>\n" );
|
||||
|
|
@ -87,7 +89,6 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
|
|
@ -159,7 +160,6 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( f = fopen( filename, "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -172,6 +172,8 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_rsa_free( &rsa );
|
||||
|
|
@ -184,7 +186,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -29,9 +29,12 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
|
|
@ -61,6 +64,7 @@ int main( int argc, char *argv[] )
|
|||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
|
|
@ -101,7 +105,6 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
{
|
||||
ret = 1;
|
||||
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;
|
||||
|
|
@ -109,7 +112,6 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -145,7 +147,6 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( ( f = fopen( filename, "wb+" ) ) == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -161,6 +162,8 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
|
|
@ -171,7 +174,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
|
||||
MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
|
|
|
|||
|
|
@ -29,9 +29,12 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
|
||||
|
|
@ -54,7 +57,8 @@ int main( void )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret, c;
|
||||
int ret = 1, c;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_rsa_context rsa;
|
||||
unsigned char hash[32];
|
||||
|
|
@ -62,7 +66,6 @@ int main( int argc, char *argv[] )
|
|||
char filename[512];
|
||||
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
ret = 1;
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
|
|
@ -100,7 +103,6 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* Extract the RSA signature from the text file
|
||||
*/
|
||||
ret = 1;
|
||||
mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
|
|
@ -146,7 +148,7 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
|
||||
|
||||
ret = 0;
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
|
|
@ -157,7 +159,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_FS_IO */
|
||||
|
|
|
|||
|
|
@ -29,9 +29,12 @@
|
|||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
|
|
@ -60,6 +63,7 @@ int main( int argc, char *argv[] )
|
|||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_pk_context pk;
|
||||
unsigned char hash[32];
|
||||
|
|
@ -91,7 +95,6 @@ int main( int argc, char *argv[] )
|
|||
|
||||
if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
|
||||
{
|
||||
ret = 1;
|
||||
mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
|
@ -101,7 +104,6 @@ int main( int argc, char *argv[] )
|
|||
/*
|
||||
* Extract the RSA signature from the file
|
||||
*/
|
||||
ret = 1;
|
||||
mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
|
|
@ -139,7 +141,7 @@ int main( int argc, char *argv[] )
|
|||
|
||||
mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
|
||||
|
||||
ret = 0;
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free( &pk );
|
||||
|
|
@ -149,7 +151,7 @@ exit:
|
|||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( ret );
|
||||
return( exit_code );
|
||||
}
|
||||
#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