Commit ca2da515 authored by Paul Bakker's avatar Paul Bakker
Browse files

Merged ECDSA tests / enhancements and example into development

parents 2b217c36 bf3109fd
......@@ -844,7 +844,7 @@
* Module: library/ecdsa.c
* Caller:
*
* Requires: POLARSSL_ECP_C
* Requires: POLARSSL_ECP_C, POLARSSL_ASN1_WRITE_C, POLARSSL_ASN1_PARSE_C
*/
#define POLARSSL_ECDSA_C
......@@ -1371,7 +1371,10 @@
#error "POLARSSL_ECDH_C defined, but not all prerequisites"
#endif
#if defined(POLARSSL_ECDSA_C) && !defined(POLARSSL_ECP_C)
#if defined(POLARSSL_ECDSA_C) && \
( !defined(POLARSSL_ECP_C) || \
!defined(POLARSSL_ASN1_PARSE_C) || \
!defined(POLARSSL_ASN1_WRITE_C) )
#error "POLARSSL_ECDSA_C defined, but not all prerequisites"
#endif
......
......@@ -83,6 +83,63 @@ int ecdsa_verify( const ecp_group *grp,
const unsigned char *buf, size_t blen,
const ecp_point *Q, const mpi *r, const mpi *s);
/**
* \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
* \param hlen Length of hash
* \param sig Buffer that will hold the signature
* \param slen Length of the signature written
* \param f_rng RNG function
* \param p_rng RNG parameter
*
* \note The "sig" buffer must be at least as large as twice the
* size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
* curve is used).
*
* \return 0 if successful,
* or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
* POLARSSL_ERR_ASN1 error code
*/
int ecdsa_write_signature( ecdsa_context *ctx,
const unsigned char *hash, size_t hlen,
unsigned char *sig, size_t *slen,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
/**
* \brief Read and verify an ECDSA signature
*
* \param ctx ECDSA context
* \param hash Message hash
* \param hlen Size of hash
* \param sig Signature to read and verify
* \param slen Size of sig
*
* \return 0 if successful,
* POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid
* or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code
*/
int ecdsa_read_signature( ecdsa_context *ctx,
const unsigned char *hash, size_t hlen,
const unsigned char *sig, size_t slen );
/**
* \brief Generate an ECDSA keypair on the given curve
*
* \param ctx ECDSA context in which the keypair should be stored
* \param grp Group (elliptic curve) to use. One of the various
* POLARSSL_ECP_DP_XXX macros depending on configuration.
* \param f_rng RNG function
* \param p_rng RNG parameter
*
* \return 0 on success, or a POLARSSL_ERR_ECP code.
*/
int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
/**
* \brief Initialize context
*
......
......@@ -126,9 +126,10 @@ ecp_keypair;
#define POLARSSL_ECP_DP_SECP521R1 25
/**
* Maximum bit size of the groups (that is, of N)
* Maximum size of the groups (that is, of N and P)
*/
#define POLARSSL_ECP_MAX_N_BITS 521
#define POLARSSL_ECP_MAX_BITS 521
#define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
/*
* Maximum window size (actually, NAF width) used for point multipliation.
......@@ -218,6 +219,17 @@ int ecp_is_zero( ecp_point *pt );
*/
int ecp_copy( ecp_point *P, const ecp_point *Q );
/**
* \brief Copy the contents of a group object
*
* \param dst Destination group
* \param src Source group
*
* \return 0 if successful,
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int ecp_group_copy( ecp_group *dst, const ecp_group *src );
/**
* \brief Import a non-zero point from two ASCII strings
*
......
......@@ -34,6 +34,7 @@
#if defined(POLARSSL_ECDSA_C)
#include "polarssl/ecdsa.h"
#include "polarssl/asn1write.h"
/*
* Derive a suitable integer for group grp from a buffer of length len
......@@ -187,6 +188,102 @@ cleanup:
return( ret );
}
/*
* RFC 4492 page 20:
*
* Ecdsa-Sig-Value ::= SEQUENCE {
* r INTEGER,
* s INTEGER
* }
*
* Size is at most
* 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
* twice that + 1 (tag) + 2 (len) for the sequence
* (assuming ECP_MAX_BYTES is less than 126 for r and s,
* and less than 124 (total len <= 255) for the sequence)
*/
#if POLARSSL_ECP_MAX_BYTES > 124
#error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN"
#endif
#define MAX_SIG_LEN ( 3 + 2 * ( 2 + POLARSSL_ECP_MAX_BYTES ) )
/*
* Compute and write signature
*/
int ecdsa_write_signature( ecdsa_context *ctx,
const unsigned char *hash, size_t hlen,
unsigned char *sig, size_t *slen,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng )
{
int ret;
unsigned char buf[MAX_SIG_LEN];
unsigned char *p = buf + MAX_SIG_LEN - 1;
size_t len = 0;
if( ( ret = ecdsa_sign( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
hash, hlen, f_rng, p_rng ) ) != 0 )
{
return( ret );
}
ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->s ) );
ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->r ) );
ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) );
ASN1_CHK_ADD( len, asn1_write_tag( &p, buf,
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
memcpy( sig, p, len );
*slen = len;
return( 0 );
}
/*
* Read and check signature
*/
int ecdsa_read_signature( ecdsa_context *ctx,
const unsigned char *hash, size_t hlen,
const unsigned char *sig, size_t slen )
{
int ret;
unsigned char *p = (unsigned char *) sig;
const unsigned char *end = sig + slen;
size_t len;
if( ( ret = asn1_get_tag( &p, end, &len,
ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
{
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
}
if( p + len != end )
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
if( ( ret = asn1_get_mpi( &p, end, &ctx->r ) ) != 0 ||
( ret = asn1_get_mpi( &p, end, &ctx->s ) ) != 0 )
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
if( p != end )
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
return( ecdsa_verify( &ctx->grp, hash, hlen, &ctx->Q, &ctx->r, &ctx->s ) );
}
/*
* Generate key pair
*/
int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
{
return( ecp_use_known_dp( &ctx->grp, gid ) ||
ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
}
/*
* Initialize context
*/
......
......@@ -181,6 +181,14 @@ cleanup:
return( ret );
}
/*
* Copy the contents of a group object
*/
int ecp_group_copy( ecp_group *dst, const ecp_group *src )
{
return ecp_use_known_dp( dst, src->id );
}
/*
* Import a non-zero point from ASCII strings
*/
......@@ -1146,7 +1154,7 @@ cleanup:
* (that is: grp->nbits / w + 1)
* Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N.
*/
#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_N_BITS / 2 + 1 )
#define MAX_NAF_LEN ( POLARSSL_ECP_MAX_BITS / 2 + 1 )
/*
* Integer multiplication: R = m * P
......
......@@ -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
......
......@@ -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 $@
......
......@@ -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)
......
/*
* Example ECDSA program
*
* Copyright (C) 2013, Brainspark B.V.
*
* This file is part of PolarSSL (http://www.polarssl.org)
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
*
* 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 <string.h>
#include <stdio.h>
/*
* 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_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
{
printf( " failed\n ! ecp_group_copy 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
ecdsa_free( &ctx_verify );
ecdsa_free( &ctx_sign );
return( ret );
}
#endif /* POLARSSL_BIGNUM_C && POLARSSL_ECDSA_C &&
POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C &&
ECPARAMS */
......@@ -22,3 +22,17 @@ ecdsa_prim_test_vectors:POLARSSL_ECP_DP_SECP384R1:"0BEB646634BA87735D77AE4809A0E
ECDSA primitive rfc 4754 p521
ecdsa_prim_test_vectors:POLARSSL_ECP_DP_SECP521R1:"0065FDA3409451DCAB0A0EAD45495112A3D813C17BFD34BDF8C1209D7DF5849120597779060A7FF9D704ADF78B570FFAD6F062E95C7E0C5D5481C5B153B48B375FA1":"0151518F1AF0F563517EDD5485190DF95A4BF57B5CBA4CF2A9A3F6474725A35F7AFE0A6DDEB8BEDBCD6A197E592D40188901CECD650699C9B5E456AEA5ADD19052A8":"006F3B142EA1BFFF7E2837AD44C9E4FF6D2D34C73184BBAD90026DD5E6E85317D9DF45CAD7803C6C20035B2F3FF63AFF4E1BA64D1C077577DA3F4286C58F0AEAE643":"00C1C2B305419F5A41344D7E4359933D734096F556197A9B244342B8B62F46F9373778F9DE6B6497B1EF825FF24F42F9B4A4BD7382CFC3378A540B1B7F0C1B956C2F":"DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F":"0154FD3836AF92D0DCA57DD5341D3053988534FDE8318FC6AAAAB68E2E6F4339B19F2F281A7E0B22C269D93CF8794A9278880ED7DBB8D9362CAEACEE544320552251":"017705A7030290D1CEB605A9A1BB03FF9CDD521E87A696EC926C8C10C8362DF4975367101F67D1CF9BCCBF2F3D239534FA509E70AAC851AE01AAC68D62F866472660"
ECDSA write-read random #1
ecdsa_write_read_random:POLARSSL_ECP_DP_SECP192R1
ECDSA write-read random #2
ecdsa_write_read_random:POLARSSL_ECP_DP_SECP224R1
ECDSA write-read random #3
ecdsa_write_read_random:POLARSSL_ECP_DP_SECP256R1
ECDSA write-read random #4
ecdsa_write_read_random:POLARSSL_ECP_DP_SECP384R1
ECDSA write-read random #5
ecdsa_write_read_random:POLARSSL_ECP_DP_SECP521R1
......@@ -76,3 +76,61 @@ void ecdsa_prim_test_vectors( int id, char *d_str, char *xQ_str, char *yQ_str,
mpi_free( &r_check ); mpi_free( &s_check );
}
/* END_CASE */
/* BEGIN_CASE */
void ecdsa_write_read_random( int id )
{
ecdsa_context ctx;
rnd_pseudo_info rnd_info;
unsigned char hash[66];
unsigned char sig[200];
size_t sig_len, i;
ecdsa_init( &ctx );
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
memset( hash, 0, sizeof( hash ) );
memset( sig, 0x2a, sizeof( sig ) );
/* prepare material for signature */
TEST_ASSERT( rnd_pseudo_rand( &rnd_info, hash, sizeof( hash ) ) == 0 );
/* generate signing key */
TEST_ASSERT( ecdsa_genkey( &ctx, id, &rnd_pseudo_rand, &rnd_info ) == 0 );
/* generate and write signature, then read and verify it */
TEST_ASSERT( ecdsa_write_signature( &ctx, hash, sizeof( hash ),
sig, &sig_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ),
sig, sig_len ) == 0 );
/* check we didn't write past the announced length */
for( i = sig_len; i < sizeof( sig ); i++ )
TEST_ASSERT( sig[i] == 0x2a );
/* try verification with invalid length */
TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ),
sig, sig_len - 1 ) != 0 );
TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ),
sig, sig_len + 1 ) != 0 );
/* try invalid sequence tag */
sig[0]++;
TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ),
sig, sig_len ) != 0 );
sig[0]--;
/* try modifying r */
sig[10]++;
TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ),
sig, sig_len ) != 0 );
sig[10]--;
/* try modifying s */
sig[sig_len - 1]++;
TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ),
sig, sig_len ) != 0 );
sig[sig_len - 1]--;
ecdsa_free( &ctx );
}
/* END_CASE */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment