mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-02-16 00:40:52 +00:00
Adapt key_app example program to new RSA interface
This commit is contained in:
parent
83aad1fa86
commit
54ebf9971d
@ -84,17 +84,23 @@ struct options
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
mbedtls_pk_context pk;
|
||||
char buf[1024];
|
||||
int i;
|
||||
char *p, *q;
|
||||
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
|
||||
|
||||
/*
|
||||
* Set to sane values
|
||||
*/
|
||||
mbedtls_pk_init( &pk );
|
||||
memset( buf, 0, sizeof(buf) );
|
||||
|
||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
||||
|
||||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
@ -189,14 +195,22 @@ int main( int argc, char *argv[] )
|
||||
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
|
||||
{
|
||||
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 );
|
||||
|
||||
if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
|
||||
( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
|
||||
mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
|
||||
mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
|
||||
mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
@ -239,8 +253,15 @@ int main( int argc, char *argv[] )
|
||||
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
|
||||
{
|
||||
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 );
|
||||
|
||||
if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
|
||||
NULL, &E ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
|
||||
mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
@ -265,11 +286,17 @@ int main( int argc, char *argv[] )
|
||||
exit:
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
mbedtls_strerror( ret, buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_strerror( ret, buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
}
|
||||
#endif
|
||||
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
|
||||
mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
|
Loading…
x
Reference in New Issue
Block a user