Fixes error and exit paths in rsa sample programs
This commit is contained in:
parent
f8758b8bdc
commit
6b46c62d77
2 changed files with 72 additions and 49 deletions
|
@ -30,6 +30,9 @@
|
||||||
#else
|
#else
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define mbedtls_printf printf
|
#define mbedtls_printf printf
|
||||||
|
#define mbedtls_exit exit
|
||||||
|
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||||
|
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||||
|
@ -39,8 +42,8 @@
|
||||||
#include "mbedtls/entropy.h"
|
#include "mbedtls/entropy.h"
|
||||||
#include "mbedtls/ctr_drbg.h"
|
#include "mbedtls/ctr_drbg.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||||
|
@ -57,7 +60,7 @@ int main( void )
|
||||||
int main( int argc, char *argv[] )
|
int main( int argc, char *argv[] )
|
||||||
{
|
{
|
||||||
FILE *f;
|
FILE *f;
|
||||||
int ret, c;
|
int return_val, exit_val, c;
|
||||||
size_t i;
|
size_t i;
|
||||||
mbedtls_rsa_context rsa;
|
mbedtls_rsa_context rsa;
|
||||||
mbedtls_entropy_context entropy;
|
mbedtls_entropy_context entropy;
|
||||||
|
@ -68,10 +71,7 @@ int main( int argc, char *argv[] )
|
||||||
((void) argv);
|
((void) argv);
|
||||||
|
|
||||||
memset(result, 0, sizeof( result ) );
|
memset(result, 0, sizeof( result ) );
|
||||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
exit_val = MBEDTLS_EXIT_SUCCESS;
|
||||||
mbedtls_entropy_init( &entropy );
|
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
|
||||||
ret = 1;
|
|
||||||
|
|
||||||
if( argc != 1 )
|
if( argc != 1 )
|
||||||
{
|
{
|
||||||
|
@ -81,17 +81,23 @@ int main( int argc, char *argv[] )
|
||||||
mbedtls_printf( "\n" );
|
mbedtls_printf( "\n" );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
goto exit;
|
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||||
fflush( stdout );
|
fflush( stdout );
|
||||||
|
|
||||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||||
(const unsigned char *) pers,
|
mbedtls_entropy_init( &entropy );
|
||||||
strlen( pers ) ) ) != 0 )
|
|
||||||
|
return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||||
|
&entropy, (const unsigned char *) pers,
|
||||||
|
strlen( pers ) );
|
||||||
|
if( return_val != 0 )
|
||||||
{
|
{
|
||||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
|
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||||
|
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
|
||||||
|
return_val );
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,21 +106,24 @@ int main( int argc, char *argv[] )
|
||||||
|
|
||||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||||
{
|
{
|
||||||
|
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||||
mbedtls_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" );
|
" ! Please run rsa_genkey first\n\n" );
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( ( ret = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
|
if( ( return_val = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
|
||||||
( ret = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
|
( return_val = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
|
||||||
( ret = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
|
( return_val = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
|
||||||
( ret = mbedtls_mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
|
( return_val = mbedtls_mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
|
||||||
( ret = mbedtls_mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
|
( return_val = mbedtls_mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
|
||||||
( ret = mbedtls_mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
|
( return_val = mbedtls_mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
|
||||||
( ret = mbedtls_mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
|
( return_val = mbedtls_mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
|
||||||
( ret = mbedtls_mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
|
( return_val = mbedtls_mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
|
||||||
{
|
{
|
||||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
|
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||||
|
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
|
||||||
|
return_val );
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,10 +134,9 @@ int main( int argc, char *argv[] )
|
||||||
/*
|
/*
|
||||||
* Extract the RSA encrypted value from the text file
|
* Extract the RSA encrypted value from the text file
|
||||||
*/
|
*/
|
||||||
ret = 1;
|
|
||||||
|
|
||||||
if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
|
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" );
|
mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
@ -143,6 +151,7 @@ int main( int argc, char *argv[] )
|
||||||
|
|
||||||
if( i != rsa.len )
|
if( i != rsa.len )
|
||||||
{
|
{
|
||||||
|
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||||
mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
|
mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
@ -153,11 +162,14 @@ int main( int argc, char *argv[] )
|
||||||
mbedtls_printf( "\n . Decrypting the encrypted data" );
|
mbedtls_printf( "\n . Decrypting the encrypted data" );
|
||||||
fflush( stdout );
|
fflush( stdout );
|
||||||
|
|
||||||
if( ( ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg,
|
return_val = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
|
||||||
MBEDTLS_RSA_PRIVATE, &i, buf, result,
|
&ctr_drbg, MBEDTLS_RSA_PRIVATE, &i,
|
||||||
1024 ) ) != 0 )
|
buf, result, 1024 );
|
||||||
|
if( return_val != 0 )
|
||||||
{
|
{
|
||||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n", ret );
|
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||||
|
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
|
||||||
|
return_val );
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,8 +177,6 @@ int main( int argc, char *argv[] )
|
||||||
|
|
||||||
mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
|
mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
|
||||||
|
|
||||||
ret = 0;
|
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||||
mbedtls_entropy_free( &entropy );
|
mbedtls_entropy_free( &entropy );
|
||||||
|
@ -177,6 +187,7 @@ exit:
|
||||||
fflush( stdout ); getchar();
|
fflush( stdout ); getchar();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return( ret );
|
return( exit_val );
|
||||||
}
|
}
|
||||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */
|
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,9 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define mbedtls_fprintf fprintf
|
#define mbedtls_fprintf fprintf
|
||||||
#define mbedtls_printf printf
|
#define mbedtls_printf printf
|
||||||
|
#define mbedtls_exit exit
|
||||||
|
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||||
|
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||||
|
@ -40,7 +43,6 @@
|
||||||
#include "mbedtls/entropy.h"
|
#include "mbedtls/entropy.h"
|
||||||
#include "mbedtls/ctr_drbg.h"
|
#include "mbedtls/ctr_drbg.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -58,7 +60,7 @@ int main( void )
|
||||||
int main( int argc, char *argv[] )
|
int main( int argc, char *argv[] )
|
||||||
{
|
{
|
||||||
FILE *f;
|
FILE *f;
|
||||||
int ret;
|
int return_val, exit_val;
|
||||||
size_t i;
|
size_t i;
|
||||||
mbedtls_rsa_context rsa;
|
mbedtls_rsa_context rsa;
|
||||||
mbedtls_entropy_context entropy;
|
mbedtls_entropy_context entropy;
|
||||||
|
@ -67,9 +69,7 @@ int main( int argc, char *argv[] )
|
||||||
unsigned char buf[512];
|
unsigned char buf[512];
|
||||||
const char *pers = "rsa_encrypt";
|
const char *pers = "rsa_encrypt";
|
||||||
|
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
exit_val = MBEDTLS_EXIT_SUCCESS;
|
||||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
|
||||||
ret = 1;
|
|
||||||
|
|
||||||
if( argc != 2 )
|
if( argc != 2 )
|
||||||
{
|
{
|
||||||
|
@ -79,18 +79,24 @@ int main( int argc, char *argv[] )
|
||||||
mbedtls_printf( "\n" );
|
mbedtls_printf( "\n" );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
goto exit;
|
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||||
fflush( stdout );
|
fflush( stdout );
|
||||||
|
|
||||||
|
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||||
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||||
mbedtls_entropy_init( &entropy );
|
mbedtls_entropy_init( &entropy );
|
||||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
|
||||||
(const unsigned char *) pers,
|
return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||||
strlen( pers ) ) ) != 0 )
|
&entropy, (const unsigned char *) pers,
|
||||||
|
strlen( pers ) );
|
||||||
|
if( return_val != 0 )
|
||||||
{
|
{
|
||||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
|
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||||
|
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
|
||||||
|
return_val );
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,16 +105,18 @@ int main( int argc, char *argv[] )
|
||||||
|
|
||||||
if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
|
if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
|
||||||
{
|
{
|
||||||
ret = 1;
|
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||||
mbedtls_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" );
|
" ! Please run rsa_genkey first\n\n" );
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
|
if( ( return_val = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
|
||||||
( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
|
( return_val = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
|
||||||
{
|
{
|
||||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
|
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||||
|
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
|
||||||
|
return_val );
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,6 +126,7 @@ int main( int argc, char *argv[] )
|
||||||
|
|
||||||
if( strlen( argv[1] ) > 100 )
|
if( strlen( argv[1] ) > 100 )
|
||||||
{
|
{
|
||||||
|
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||||
mbedtls_printf( " Input data larger than 100 characters.\n\n" );
|
mbedtls_printf( " Input data larger than 100 characters.\n\n" );
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
@ -130,11 +139,14 @@ int main( int argc, char *argv[] )
|
||||||
mbedtls_printf( "\n . Generating the RSA encrypted value" );
|
mbedtls_printf( "\n . Generating the RSA encrypted value" );
|
||||||
fflush( stdout );
|
fflush( stdout );
|
||||||
|
|
||||||
if( ( ret = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg,
|
return_val = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random,
|
||||||
MBEDTLS_RSA_PUBLIC, strlen( argv[1] ),
|
&ctr_drbg, MBEDTLS_RSA_PUBLIC,
|
||||||
input, buf ) ) != 0 )
|
strlen( argv[1] ), input, buf );
|
||||||
|
if( return_val != 0 )
|
||||||
{
|
{
|
||||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n", ret );
|
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||||
|
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
|
||||||
|
return_val );
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,7 +155,7 @@ int main( int argc, char *argv[] )
|
||||||
*/
|
*/
|
||||||
if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
|
if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
|
||||||
{
|
{
|
||||||
ret = 1;
|
exit_val = MBEDTLS_EXIT_FAILURE;
|
||||||
mbedtls_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;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
@ -166,7 +178,7 @@ exit:
|
||||||
fflush( stdout ); getchar();
|
fflush( stdout ); getchar();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return( ret );
|
return( exit_val );
|
||||||
}
|
}
|
||||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
|
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
|
||||||
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
||||||
|
|
Loading…
Reference in a new issue