From aa431613b3b4e4926bd082ff2999df2dfcf7977b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Fri, 9 Aug 2013 17:10:27 +0200 Subject: [PATCH] Add ecdsa example program --- include/polarssl/ecdsa.h | 3 +- programs/.gitignore | 1 + programs/Makefile | 4 + programs/pkey/CMakeLists.txt | 3 + programs/pkey/ecdsa.c | 196 +++++++++++++++++++++++++++++++++++ 5 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 programs/pkey/ecdsa.c diff --git a/include/polarssl/ecdsa.h b/include/polarssl/ecdsa.h index cd1987e319..d61e82c2d2 100644 --- a/include/polarssl/ecdsa.h +++ b/include/polarssl/ecdsa.h @@ -84,7 +84,8 @@ int ecdsa_verify( const ecp_group *grp, const ecp_point *Q, const mpi *r, const mpi *s); /** - * \brief Compute ECDSA signature and write it to buffer + * \brief Compute ECDSA signature and write it to buffer, + * serialized as defined in RFC 4492 page 20. * * \param ctx ECDSA context * \param hash Message hash diff --git a/programs/.gitignore b/programs/.gitignore index 9a69532471..e9f4e54eff 100644 --- a/programs/.gitignore +++ b/programs/.gitignore @@ -9,6 +9,7 @@ hash/sha2sum pkey/dh_client pkey/dh_genprime pkey/dh_server +pkey/ecdsa pkey/key_app pkey/key_app_writer pkey/mpi_demo diff --git a/programs/Makefile b/programs/Makefile index 388b029e2b..1c2b5fc807 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -91,6 +91,10 @@ pkey/dh_server: pkey/dh_server.c ../library/libpolarssl.a echo " CC pkey/dh_server.c" $(CC) $(CFLAGS) $(OFLAGS) pkey/dh_server.c $(LDFLAGS) -o $@ +pkey/ecdsa: pkey/ecdsa.c ../library/libpolarssl.a + echo " CC pkey/ecdsa.c" + $(CC) $(CFLAGS) $(OFLAGS) pkey/ecdsa.c $(LDFLAGS) -o $@ + pkey/key_app: pkey/key_app.c ../library/libpolarssl.a echo " CC pkey/key_app.c" $(CC) $(CFLAGS) $(OFLAGS) pkey/key_app.c $(LDFLAGS) -o $@ diff --git a/programs/pkey/CMakeLists.txt b/programs/pkey/CMakeLists.txt index c8551a6233..015505b65a 100644 --- a/programs/pkey/CMakeLists.txt +++ b/programs/pkey/CMakeLists.txt @@ -7,6 +7,9 @@ target_link_libraries(dh_genprime polarssl) add_executable(dh_server dh_server.c) target_link_libraries(dh_server polarssl) +add_executable(ecdsa ecdsa.c) +target_link_libraries(ecdsa polarssl) + add_executable(key_app key_app.c) target_link_libraries(key_app polarssl) diff --git a/programs/pkey/ecdsa.c b/programs/pkey/ecdsa.c new file mode 100644 index 0000000000..06f1123099 --- /dev/null +++ b/programs/pkey/ecdsa.c @@ -0,0 +1,196 @@ +/* + * Example ECDSA program + * + * Copyright (C) 2013, Brainspark B.V. + * + * This file is part of PolarSSL (http://www.polarssl.org) + * Lead Maintainer: Paul Bakker + * + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "polarssl/config.h" + +#include "polarssl/entropy.h" +#include "polarssl/ctr_drbg.h" +#include "polarssl/ecdsa.h" + +#include +#include + +/* + * Uncomment to force use of a specific curve +#define ECPARAMS POLARSSL_ECP_DP_SECP256R1 + */ + +#if !defined(ECPARAMS) +#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) +#define ECPARAMS POLARSSL_ECP_DP_SECP192R1 +#elif defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) +#define ECPARAMS POLARSSL_ECP_DP_SECP224R1 +#elif defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) +#define ECPARAMS POLARSSL_ECP_DP_SECP256R1 +#elif defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) +#define ECPARAMS POLARSSL_ECP_DP_SECP384R1 +#elif defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) +#define ECPARAMS POLARSSL_ECP_DP_SECP521R1 +#endif +#endif /* !defined(ECPARAMS) */ + +#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ECDSA_C) || \ + !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \ + !defined(ECPARAMS) +int main( int argc, char *argv[] ) +{ + ((void) argc); + ((void) argv); + + printf("POLARSSL_BIGNUM_C and/or POLARSSL_ECDSA_C and/or " + "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C not defined," + "and/or not EC domain parameter available\n" ); + return( 0 ); +} +#else +int main( int argc, char *argv[] ) +{ + int ret; + ecdsa_context ctx_sign, ctx_verify; + entropy_context entropy; + 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 ); + + memset(sig, 0, sizeof( sig ) ); + ret = 1; + + if( argc != 1 ) + { + printf( "usage: ecdsa\n" ); + +#if defined(_WIN32) + printf( "\n" ); +#endif + + goto exit; + } + + /* + * Generate a key pair for signing + */ + printf( "\n . Seeding the random number generator..." ); + fflush( stdout ); + + entropy_init( &entropy ); + if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, + (const unsigned char *) pers, + strlen( pers ) ) ) != 0 ) + { + printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); + goto exit; + } + + printf( " ok\n . Generating key pair..." ); + fflush( stdout ); + + if( ( ret = ecdsa_genkey( &ctx_sign, ECPARAMS, + ctr_drbg_random, &ctr_drbg ) ) != 0 ) + { + printf( " failed\n ! ecdsa_genkey returned %d\n", ret ); + goto exit; + } + + printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits ); + + /* + * Sign some message hash + */ + printf( " . Signing message..." ); + fflush( stdout ); + + if( ( ret = ecdsa_write_signature( &ctx_sign, + hash, sizeof( hash ), + sig, &sig_len, + ctr_drbg_random, &ctr_drbg ) ) != 0 ) + { + printf( " failed\n ! ecdsa_genkey returned %d\n", ret ); + goto exit; + } + printf( " ok\n" ); + + /* + * Signature is serialized as defined by RFC 4492 p. 20, + * but one can also access 'r' and 's' directly from the context + */ +#ifdef POLARSSL_FS_IO + mpi_write_file( " r = ", &ctx_sign.r, 16, NULL ); + mpi_write_file( " s = ", &ctx_sign.s, 16, NULL ); +#endif + + /* + * Transfer public information to verifying context + */ + printf( " . Preparing verification context..." ); + fflush( stdout ); + + if( ( ret = ecp_use_known_dp( &ctx_verify.grp, ctx_sign.grp.id ) ) != 0 ) + { + printf( " failed\n ! ecp_use_known_dp returned %d\n", ret ); + goto exit; + } + + if( ( ret = ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 ) + { + printf( " failed\n ! ecp_copy returned %d\n", ret ); + goto exit; + } + + ret = 0; + + /* + * Verify signature + */ + printf( " ok\n . Verifying signature..." ); + fflush( stdout ); + + if( ( ret = ecdsa_read_signature( &ctx_verify, + hash, sizeof( hash ), + sig, sig_len ) ) != 0 ) + { + printf( " failed\n ! ecdsa_read_signature returned %d\n", ret ); + goto exit; + } + + printf( " ok\n" ); + +exit: + +#if defined(_WIN32) + printf( " + Press Enter to exit this program.\n" ); + fflush( stdout ); getchar(); +#endif + + return( ret ); +} +#endif /* POLARSSL_BIGNUM_C && POLARSSL_ECDSA_C && + POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C && + ECPARAMS */