From 2e24ca74b024aa56e085045a757444d380ee4e05 Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Wed, 18 Sep 2013 15:21:27 +0200 Subject: [PATCH] Updated key_app.c and key_app_writer.c for EC key printing --- programs/pkey/key_app.c | 26 ++++++++++++-------------- programs/pkey/key_app_writer.c | 28 ++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/programs/pkey/key_app.c b/programs/pkey/key_app.c index de5f51383..d4c30d22b 100644 --- a/programs/pkey/key_app.c +++ b/programs/pkey/key_app.c @@ -92,7 +92,7 @@ int main( int argc, char *argv[] ) * Set to sane values */ pk_init( &pk ); - memset( buf, 0, 1024 ); + memset( buf, 0, sizeof(buf) ); if( argc == 0 ) { @@ -150,7 +150,7 @@ int main( int argc, char *argv[] ) printf( " failed\n ! fopen returned NULL\n" ); goto exit; } - fgets( buf, 1024, f ); + fgets( buf, sizeof(buf), f ); fclose( f ); i = strlen( buf ); @@ -169,8 +169,8 @@ int main( int argc, char *argv[] ) if( ret != 0 ) { - polarssl_strerror( ret, buf, 1024 ); - printf( " failed\n ! pk_parse_keyfile returned %d - %s\n\n", ret, buf ); + polarssl_strerror( ret, buf, sizeof(buf) ); + printf( " failed\n ! pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf ); goto exit; } @@ -179,11 +179,11 @@ int main( int argc, char *argv[] ) /* * 1.2 Print the key */ + printf( " . Key information ...\n" ); #if defined(POLARSSL_RSA_C) - if( pk_can_do( &pk, POLARSSL_PK_RSA ) ) + if( pk_get_type( &pk ) == POLARSSL_PK_RSA ) { rsa_context *rsa = pk_rsa( pk ); - printf( " . Key information ...\n" ); mpi_write_file( "N: ", &rsa->N, 16, NULL ); mpi_write_file( "E: ", &rsa->E, 16, NULL ); mpi_write_file( "D: ", &rsa->D, 16, NULL ); @@ -196,10 +196,9 @@ int main( int argc, char *argv[] ) else #endif #if defined(POLARSSL_ECP_C) - if( pk_can_do( &pk, POLARSSL_PK_ECKEY ) ) + if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY ) { ecp_keypair *ecp = pk_ec( pk ); - printf( " . Key information ...\n" ); 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 ); @@ -224,28 +223,27 @@ int main( int argc, char *argv[] ) if( ret != 0 ) { - polarssl_strerror( ret, buf, 1024 ); - printf( " failed\n ! pk_parse_public_keyfile returned %d - %s\n\n", ret, buf ); + polarssl_strerror( ret, buf, sizeof(buf) ); + printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x - %s\n\n", -ret, buf ); goto exit; } printf( " ok\n" ); + printf( " . Key information ...\n" ); #if defined(POLARSSL_RSA_C) - if( pk_can_do( &pk, POLARSSL_PK_RSA ) ) + if( pk_get_type( &pk ) == POLARSSL_PK_RSA ) { rsa_context *rsa = pk_rsa( pk ); - printf( " . Key information ...\n" ); mpi_write_file( "N: ", &rsa->N, 16, NULL ); mpi_write_file( "E: ", &rsa->E, 16, NULL ); } else #endif #if defined(POLARSSL_ECP_C) - if( pk_can_do( &pk, POLARSSL_PK_ECKEY ) ) + if( pk_get_type( &pk ) == POLARSSL_PK_ECKEY ) { ecp_keypair *ecp = pk_ec( pk ); - printf( " . Key information ...\n" ); 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 ); diff --git a/programs/pkey/key_app_writer.c b/programs/pkey/key_app_writer.c index 3257635e7..672ebf1a7 100644 --- a/programs/pkey/key_app_writer.c +++ b/programs/pkey/key_app_writer.c @@ -33,6 +33,7 @@ #include "polarssl/config.h" +#include "polarssl/error.h" #include "polarssl/pk.h" #include "polarssl/error.h" @@ -254,7 +255,8 @@ int main( int argc, char *argv[] ) if( ret != 0 ) { - printf( " failed\n ! pk_parse_key returned %d", ret ); + polarssl_strerror( ret, (char *) buf, sizeof(buf) ); + printf( " failed\n ! pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf ); goto exit; } @@ -279,6 +281,17 @@ int main( int argc, char *argv[] ) mpi_write_file( "QP: ", &rsa->QP, 16, NULL ); } else +#endif +#if defined(POLARSSL_ECP_C) + if( pk_get_type( &key ) == POLARSSL_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 ); + } + else #endif printf("key type not supported yet\n"); @@ -295,7 +308,8 @@ int main( int argc, char *argv[] ) if( ret != 0 ) { - printf( " failed\n ! pk_parse_public_key returned %d", ret ); + polarssl_strerror( ret, (char *) buf, sizeof(buf) ); + printf( " failed\n ! pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf ); goto exit; } @@ -314,6 +328,16 @@ int main( int argc, char *argv[] ) mpi_write_file( "E: ", &rsa->E, 16, NULL ); } else +#endif +#if defined(POLARSSL_ECP_C) + if( pk_get_type( &key ) == POLARSSL_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 ); + } + else #endif printf("key type not supported yet\n"); }