2012-10-31 09:26:55 +01:00
|
|
|
/*
|
2013-12-02 15:49:09 +01:00
|
|
|
* Elliptic curves over GF(p): generic functions
|
2012-10-31 09:26:55 +01:00
|
|
|
*
|
2013-01-16 17:00:43 +01:00
|
|
|
* Copyright (C) 2006-2013, Brainspark B.V.
|
2012-10-31 09:26:55 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* References:
|
|
|
|
*
|
2012-11-02 09:40:25 +01:00
|
|
|
* SEC1 http://www.secg.org/index.php?action=secg,docs_secg
|
2012-11-08 17:40:51 +01:00
|
|
|
* GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
|
2012-11-10 00:27:12 +01:00
|
|
|
* FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
|
2013-02-09 17:03:58 +01:00
|
|
|
* RFC 4492 for the related TLS structures and constants
|
2013-09-02 16:26:04 +02:00
|
|
|
*
|
2013-12-04 18:14:55 +01:00
|
|
|
* [M255] http://cr.yp.to/ecdh/curve25519-20060209.pdf
|
|
|
|
*
|
2013-09-02 16:26:04 +02:00
|
|
|
* [2] CORON, Jean-Sébastien. Resistance against differential power analysis
|
|
|
|
* for elliptic curve cryptosystems. In : Cryptographic Hardware and
|
|
|
|
* Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
|
|
|
|
* <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
|
2013-11-16 15:50:12 +01:00
|
|
|
*
|
|
|
|
* [3] HEDABOU, Mustapha, PINEL, Pierre, et BÉNÉTEAU, Lucien. A comb method to
|
|
|
|
* render ECC resistant against Side Channel Attacks. IACR Cryptology
|
|
|
|
* ePrint Archive, 2004, vol. 2004, p. 342.
|
|
|
|
* <http://eprint.iacr.org/2004/342.pdf>
|
2012-10-31 09:26:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "polarssl/config.h"
|
|
|
|
|
|
|
|
#if defined(POLARSSL_ECP_C)
|
|
|
|
|
|
|
|
#include "polarssl/ecp.h"
|
2013-07-03 13:37:05 +02:00
|
|
|
|
|
|
|
#if defined(POLARSSL_MEMORY_C)
|
|
|
|
#include "polarssl/memory.h"
|
|
|
|
#else
|
|
|
|
#define polarssl_malloc malloc
|
|
|
|
#define polarssl_free free
|
|
|
|
#endif
|
|
|
|
|
2012-11-21 16:00:55 +01:00
|
|
|
#include <stdlib.h>
|
2012-10-31 09:26:55 +01:00
|
|
|
|
2013-11-30 15:10:14 +01:00
|
|
|
#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
|
|
|
#define strcasecmp _stricmp
|
|
|
|
#endif
|
|
|
|
|
2013-10-28 18:53:08 +01:00
|
|
|
#if defined(_MSC_VER) && !defined(inline)
|
|
|
|
#define inline _inline
|
|
|
|
#else
|
|
|
|
#if defined(__ARMCC_VERSION) && !defined(inline)
|
|
|
|
#define inline __inline
|
|
|
|
#endif /* __ARMCC_VERSION */
|
|
|
|
#endif /*_MSC_VER */
|
|
|
|
|
2012-11-13 20:57:00 +01:00
|
|
|
#if defined(POLARSSL_SELF_TEST)
|
|
|
|
/*
|
2013-11-21 20:23:55 +01:00
|
|
|
* Counts of point addition and doubling, and field multiplications.
|
2013-09-02 16:26:04 +02:00
|
|
|
* Used to test resistance of point multiplication to simple timing attacks.
|
2012-11-13 20:57:00 +01:00
|
|
|
*/
|
2013-12-01 16:51:27 +01:00
|
|
|
static unsigned long add_count, dbl_count, mul_count;
|
2012-11-13 20:57:00 +01:00
|
|
|
#endif
|
|
|
|
|
2013-12-04 23:15:46 +01:00
|
|
|
#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) || \
|
|
|
|
defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
|
|
|
|
defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
|
|
|
|
defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) || \
|
|
|
|
defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) || \
|
|
|
|
defined(POLARSSL_ECP_DP_BP256R1_ENABLED) || \
|
|
|
|
defined(POLARSSL_ECP_DP_BP384R1_ENABLED) || \
|
|
|
|
defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
|
|
|
|
#define POLARSSL_ECP_SHORT_WEIERSTRASS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(POLARSSL_ECP_DP_M221_ENABLED) || \
|
|
|
|
defined(POLARSSL_ECP_DP_M255_ENABLED) || \
|
|
|
|
defined(POLARSSL_ECP_DP_M383_ENABLED) || \
|
|
|
|
defined(POLARSSL_ECP_DP_M511_ENABLED)
|
|
|
|
#define POLARSSL_ECP_MONTGOMERY
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curve types: internal for now, might be exposed later
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
POLARSSL_ECP_TYPE_NONE = 0,
|
|
|
|
POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
|
|
|
|
POLARSSL_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
|
|
|
|
} ecp_curve_type;
|
|
|
|
|
2013-09-16 17:30:04 +02:00
|
|
|
/*
|
|
|
|
* List of supported curves:
|
|
|
|
* - internal ID
|
2013-10-07 19:40:41 +02:00
|
|
|
* - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
|
2013-09-16 17:30:04 +02:00
|
|
|
* - size in bits
|
2013-10-07 19:40:41 +02:00
|
|
|
* - readable name
|
2013-09-16 17:30:04 +02:00
|
|
|
*/
|
2013-12-01 16:51:27 +01:00
|
|
|
static const ecp_curve_info ecp_supported_curves[] =
|
2013-09-16 17:30:04 +02:00
|
|
|
{
|
2013-10-07 19:40:41 +02:00
|
|
|
#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
|
2013-11-30 15:10:14 +01:00
|
|
|
{ POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
|
2013-10-07 19:40:41 +02:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
|
2013-11-30 15:10:14 +01:00
|
|
|
{ POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
|
2013-10-07 19:40:41 +02:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
|
2013-11-30 15:10:14 +01:00
|
|
|
{ POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
|
2013-10-07 19:40:41 +02:00
|
|
|
#endif
|
2013-09-16 17:30:04 +02:00
|
|
|
#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
|
2013-10-07 19:40:41 +02:00
|
|
|
{ POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
|
2013-09-16 17:30:04 +02:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
|
2013-10-07 19:40:41 +02:00
|
|
|
{ POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
|
2013-09-16 17:30:04 +02:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
|
2013-10-07 19:40:41 +02:00
|
|
|
{ POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
|
2013-09-16 17:30:04 +02:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
|
2013-10-07 19:40:41 +02:00
|
|
|
{ POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
|
2013-09-16 17:30:04 +02:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
|
2013-10-07 19:40:41 +02:00
|
|
|
{ POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
|
2013-09-16 17:30:04 +02:00
|
|
|
#endif
|
2013-10-07 19:40:41 +02:00
|
|
|
{ POLARSSL_ECP_DP_NONE, 0, 0, NULL },
|
2013-09-16 17:30:04 +02:00
|
|
|
};
|
|
|
|
|
2013-09-18 15:31:24 +02:00
|
|
|
/*
|
|
|
|
* List of supported curves and associated info
|
|
|
|
*/
|
|
|
|
const ecp_curve_info *ecp_curve_list( void )
|
|
|
|
{
|
|
|
|
return ecp_supported_curves;
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
|
|
|
* Get the curve info for the internal identifer
|
|
|
|
*/
|
|
|
|
const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
|
|
|
|
{
|
|
|
|
const ecp_curve_info *curve_info;
|
|
|
|
|
|
|
|
for( curve_info = ecp_curve_list();
|
|
|
|
curve_info->grp_id != POLARSSL_ECP_DP_NONE;
|
|
|
|
curve_info++ )
|
|
|
|
{
|
|
|
|
if( curve_info->grp_id == grp_id )
|
|
|
|
return( curve_info );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the curve info from the TLS identifier
|
|
|
|
*/
|
|
|
|
const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
|
|
|
|
{
|
|
|
|
const ecp_curve_info *curve_info;
|
|
|
|
|
|
|
|
for( curve_info = ecp_curve_list();
|
|
|
|
curve_info->grp_id != POLARSSL_ECP_DP_NONE;
|
|
|
|
curve_info++ )
|
|
|
|
{
|
|
|
|
if( curve_info->tls_id == tls_id )
|
|
|
|
return( curve_info );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2013-11-30 15:10:14 +01:00
|
|
|
/*
|
|
|
|
* Get the curve info from the name
|
|
|
|
*/
|
|
|
|
const ecp_curve_info *ecp_curve_info_from_name( const char *name )
|
|
|
|
{
|
|
|
|
const ecp_curve_info *curve_info;
|
|
|
|
|
|
|
|
for( curve_info = ecp_curve_list();
|
|
|
|
curve_info->grp_id != POLARSSL_ECP_DP_NONE;
|
|
|
|
curve_info++ )
|
|
|
|
{
|
|
|
|
if( strcasecmp( curve_info->name, name ) == 0 )
|
|
|
|
return( curve_info );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2013-12-04 11:08:01 +01:00
|
|
|
/*
|
2013-12-04 23:15:46 +01:00
|
|
|
* Get the type of a curve
|
2013-12-04 11:08:01 +01:00
|
|
|
*/
|
2013-12-04 23:15:46 +01:00
|
|
|
static inline ecp_curve_type ecp_get_type( const ecp_group *grp )
|
2013-12-04 11:08:01 +01:00
|
|
|
{
|
2013-12-04 23:15:46 +01:00
|
|
|
if( grp->G.X.p == NULL )
|
|
|
|
return( POLARSSL_ECP_TYPE_NONE );
|
|
|
|
|
|
|
|
if( grp->G.Y.p == NULL )
|
|
|
|
return( POLARSSL_ECP_TYPE_MONTGOMERY );
|
|
|
|
else
|
|
|
|
return( POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS );
|
2013-12-04 11:08:01 +01:00
|
|
|
}
|
|
|
|
|
2012-11-02 18:14:40 +01:00
|
|
|
/*
|
2012-11-05 17:27:54 +01:00
|
|
|
* Initialize (the components of) a point
|
2012-11-02 18:14:40 +01:00
|
|
|
*/
|
|
|
|
void ecp_point_init( ecp_point *pt )
|
|
|
|
{
|
|
|
|
if( pt == NULL )
|
|
|
|
return;
|
|
|
|
|
2012-11-05 17:27:54 +01:00
|
|
|
mpi_init( &pt->X );
|
|
|
|
mpi_init( &pt->Y );
|
2012-11-19 20:16:28 +01:00
|
|
|
mpi_init( &pt->Z );
|
2012-11-05 17:27:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize (the components of) a group
|
|
|
|
*/
|
|
|
|
void ecp_group_init( ecp_group *grp )
|
|
|
|
{
|
|
|
|
if( grp == NULL )
|
|
|
|
return;
|
|
|
|
|
2013-09-16 18:56:28 +02:00
|
|
|
memset( grp, 0, sizeof( ecp_group ) );
|
2012-11-02 18:14:40 +01:00
|
|
|
}
|
|
|
|
|
2013-07-01 13:40:52 +02:00
|
|
|
/*
|
|
|
|
* Initialize (the components of) a key pair
|
|
|
|
*/
|
|
|
|
void ecp_keypair_init( ecp_keypair *key )
|
|
|
|
{
|
|
|
|
if ( key == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
ecp_group_init( &key->grp );
|
|
|
|
mpi_init( &key->d );
|
|
|
|
ecp_point_init( &key->Q );
|
|
|
|
}
|
|
|
|
|
2012-10-31 19:24:21 +01:00
|
|
|
/*
|
|
|
|
* Unallocate (the components of) a point
|
|
|
|
*/
|
|
|
|
void ecp_point_free( ecp_point *pt )
|
|
|
|
{
|
|
|
|
if( pt == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
mpi_free( &( pt->X ) );
|
|
|
|
mpi_free( &( pt->Y ) );
|
2012-11-19 20:16:28 +01:00
|
|
|
mpi_free( &( pt->Z ) );
|
2012-10-31 19:24:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unallocate (the components of) a group
|
|
|
|
*/
|
|
|
|
void ecp_group_free( ecp_group *grp )
|
|
|
|
{
|
2013-09-17 19:13:10 +02:00
|
|
|
size_t i;
|
|
|
|
|
2012-10-31 19:24:21 +01:00
|
|
|
if( grp == NULL )
|
|
|
|
return;
|
|
|
|
|
2013-12-06 12:51:50 +01:00
|
|
|
if( grp->h != 1 )
|
|
|
|
{
|
|
|
|
mpi_free( &grp->P );
|
|
|
|
mpi_free( &grp->A );
|
|
|
|
mpi_free( &grp->B );
|
|
|
|
ecp_point_free( &grp->G );
|
|
|
|
mpi_free( &grp->N );
|
|
|
|
}
|
2013-09-16 18:56:28 +02:00
|
|
|
|
2013-09-17 19:13:10 +02:00
|
|
|
if( grp->T != NULL )
|
|
|
|
{
|
|
|
|
for( i = 0; i < grp->T_size; i++ )
|
|
|
|
ecp_point_free( &grp->T[i] );
|
|
|
|
polarssl_free( grp->T );
|
|
|
|
}
|
|
|
|
|
2013-09-16 18:56:28 +02:00
|
|
|
memset( grp, 0, sizeof( ecp_group ) );
|
2012-10-31 19:24:21 +01:00
|
|
|
}
|
2012-10-31 09:26:55 +01:00
|
|
|
|
2013-07-01 13:40:52 +02:00
|
|
|
/*
|
|
|
|
* Unallocate (the components of) a key pair
|
|
|
|
*/
|
|
|
|
void ecp_keypair_free( ecp_keypair *key )
|
|
|
|
{
|
|
|
|
if ( key == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
ecp_group_free( &key->grp );
|
|
|
|
mpi_free( &key->d );
|
|
|
|
ecp_point_free( &key->Q );
|
|
|
|
}
|
|
|
|
|
2012-11-02 18:14:40 +01:00
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Copy the contents of a point
|
2012-11-02 09:40:25 +01:00
|
|
|
*/
|
|
|
|
int ecp_copy( ecp_point *P, const ecp_point *Q )
|
|
|
|
{
|
2012-11-10 14:44:24 +01:00
|
|
|
int ret;
|
2012-11-02 09:40:25 +01:00
|
|
|
|
|
|
|
MPI_CHK( mpi_copy( &P->X, &Q->X ) );
|
|
|
|
MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
|
2012-11-19 20:16:28 +01:00
|
|
|
MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
|
2012-11-02 09:40:25 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
2012-10-31 19:37:54 +01:00
|
|
|
|
2013-08-12 15:44:31 +02:00
|
|
|
/*
|
|
|
|
* 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 );
|
|
|
|
}
|
|
|
|
|
2012-11-05 13:13:44 +01:00
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Set point to zero
|
2012-11-05 13:13:44 +01:00
|
|
|
*/
|
2013-10-23 20:19:57 +02:00
|
|
|
int ecp_set_zero( ecp_point *pt )
|
2012-11-05 13:13:44 +01:00
|
|
|
{
|
2012-11-10 14:44:24 +01:00
|
|
|
int ret;
|
2012-11-05 13:13:44 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
MPI_CHK( mpi_lset( &pt->X , 1 ) );
|
|
|
|
MPI_CHK( mpi_lset( &pt->Y , 1 ) );
|
|
|
|
MPI_CHK( mpi_lset( &pt->Z , 0 ) );
|
2012-11-05 13:13:44 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Tell if a point is zero
|
2012-11-05 13:13:44 +01:00
|
|
|
*/
|
2013-10-23 20:19:57 +02:00
|
|
|
int ecp_is_zero( ecp_point *pt )
|
2012-11-05 13:13:44 +01:00
|
|
|
{
|
2013-10-23 20:19:57 +02:00
|
|
|
return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
|
2013-10-08 12:04:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Import a non-zero point from ASCII strings
|
2013-10-08 12:04:56 +02:00
|
|
|
*/
|
2013-10-23 20:19:57 +02:00
|
|
|
int ecp_point_read_string( ecp_point *P, int radix,
|
|
|
|
const char *x, const char *y )
|
2013-10-08 12:04:56 +02:00
|
|
|
{
|
2013-10-10 15:55:39 +02:00
|
|
|
int ret;
|
2013-10-08 12:04:56 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
MPI_CHK( mpi_read_string( &P->X, radix, x ) );
|
|
|
|
MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
|
|
|
|
MPI_CHK( mpi_lset( &P->Z, 1 ) );
|
2013-10-10 15:55:39 +02:00
|
|
|
|
|
|
|
cleanup:
|
2012-11-05 13:13:44 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-11-24 14:10:14 +01:00
|
|
|
/*
|
2012-11-24 15:19:55 +01:00
|
|
|
* Export a point into unsigned binary data (SEC1 2.3.3)
|
2012-11-24 14:10:14 +01:00
|
|
|
*/
|
2013-02-10 10:58:48 +01:00
|
|
|
int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
|
2013-02-10 12:22:46 +01:00
|
|
|
int format, size_t *olen,
|
2013-02-10 10:58:48 +01:00
|
|
|
unsigned char *buf, size_t buflen )
|
2012-11-24 14:10:14 +01:00
|
|
|
{
|
2013-04-08 13:40:17 +02:00
|
|
|
int ret = 0;
|
2012-11-24 14:10:14 +01:00
|
|
|
size_t plen;
|
|
|
|
|
2012-11-24 15:19:55 +01:00
|
|
|
if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
|
|
|
|
format != POLARSSL_ECP_PF_COMPRESSED )
|
2013-02-09 19:00:07 +01:00
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
2012-11-24 15:19:55 +01:00
|
|
|
|
2012-11-24 14:10:14 +01:00
|
|
|
/*
|
2012-11-24 15:19:55 +01:00
|
|
|
* Common case: P == 0
|
2012-11-24 14:10:14 +01:00
|
|
|
*/
|
|
|
|
if( mpi_cmp_int( &P->Z, 0 ) == 0 )
|
|
|
|
{
|
|
|
|
if( buflen < 1 )
|
2013-02-09 19:00:07 +01:00
|
|
|
return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
|
2012-11-24 14:10:14 +01:00
|
|
|
|
|
|
|
buf[0] = 0x00;
|
|
|
|
*olen = 1;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
plen = mpi_size( &grp->P );
|
|
|
|
|
2012-11-24 15:19:55 +01:00
|
|
|
if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
|
|
|
|
{
|
|
|
|
*olen = 2 * plen + 1;
|
|
|
|
|
|
|
|
if( buflen < *olen )
|
2013-02-09 19:00:07 +01:00
|
|
|
return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
|
2012-11-24 14:10:14 +01:00
|
|
|
|
2012-11-24 15:19:55 +01:00
|
|
|
buf[0] = 0x04;
|
|
|
|
MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
|
|
|
|
MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
|
|
|
|
}
|
|
|
|
else if( format == POLARSSL_ECP_PF_COMPRESSED )
|
|
|
|
{
|
|
|
|
*olen = plen + 1;
|
|
|
|
|
|
|
|
if( buflen < *olen )
|
2013-02-09 19:00:07 +01:00
|
|
|
return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
|
2012-11-24 15:19:55 +01:00
|
|
|
|
|
|
|
buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
|
|
|
|
MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
|
|
|
|
}
|
2012-11-24 14:10:14 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-11-24 16:19:42 +01:00
|
|
|
/*
|
|
|
|
* Import a point from unsigned binary data (SEC1 2.3.4)
|
|
|
|
*/
|
2013-02-10 10:58:48 +01:00
|
|
|
int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
|
|
|
|
const unsigned char *buf, size_t ilen ) {
|
2012-11-24 16:19:42 +01:00
|
|
|
int ret;
|
|
|
|
size_t plen;
|
|
|
|
|
|
|
|
if( ilen == 1 && buf[0] == 0x00 )
|
2013-02-10 10:53:04 +01:00
|
|
|
return( ecp_set_zero( pt ) );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2013-02-10 10:53:04 +01:00
|
|
|
plen = mpi_size( &grp->P );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
|
|
|
if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
|
2013-02-10 10:53:04 +01:00
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2013-02-10 10:53:04 +01:00
|
|
|
MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
|
|
|
|
MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
|
|
|
|
MPI_CHK( mpi_lset( &pt->Z, 1 ) );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-02-09 19:00:07 +01:00
|
|
|
/*
|
|
|
|
* Import a point from a TLS ECPoint record (RFC 4492)
|
|
|
|
* struct {
|
|
|
|
* opaque point <1..2^8-1>;
|
|
|
|
* } ECPoint;
|
|
|
|
*/
|
|
|
|
int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
|
2013-02-10 13:38:29 +01:00
|
|
|
const unsigned char **buf, size_t buf_len )
|
2013-02-09 19:00:07 +01:00
|
|
|
{
|
|
|
|
unsigned char data_len;
|
2013-02-10 13:38:29 +01:00
|
|
|
const unsigned char *buf_start;
|
2013-02-09 19:00:07 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We must have at least two bytes (1 for length, at least of for data)
|
|
|
|
*/
|
|
|
|
if( buf_len < 2 )
|
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
|
|
|
|
2013-02-10 13:38:29 +01:00
|
|
|
data_len = *(*buf)++;
|
2013-02-09 19:00:07 +01:00
|
|
|
if( data_len < 1 || data_len > buf_len - 1 )
|
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
|
|
|
|
2013-02-10 13:38:29 +01:00
|
|
|
/*
|
|
|
|
* Save buffer start for read_binary and update buf
|
|
|
|
*/
|
|
|
|
buf_start = *buf;
|
|
|
|
*buf += data_len;
|
|
|
|
|
|
|
|
return ecp_point_read_binary( grp, pt, buf_start, data_len );
|
2013-02-09 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Export a point as a TLS ECPoint record (RFC 4492)
|
|
|
|
* struct {
|
|
|
|
* opaque point <1..2^8-1>;
|
|
|
|
* } ECPoint;
|
|
|
|
*/
|
|
|
|
int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
|
2013-02-10 12:22:46 +01:00
|
|
|
int format, size_t *olen,
|
|
|
|
unsigned char *buf, size_t blen )
|
2013-02-09 19:00:07 +01:00
|
|
|
{
|
2013-02-10 12:22:46 +01:00
|
|
|
int ret;
|
|
|
|
|
2013-02-09 19:00:07 +01:00
|
|
|
/*
|
2013-02-10 12:22:46 +01:00
|
|
|
* buffer length must be at least one, for our length byte
|
2013-02-09 19:00:07 +01:00
|
|
|
*/
|
2013-02-10 12:22:46 +01:00
|
|
|
if( blen < 1 )
|
2013-02-09 19:00:07 +01:00
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
|
|
|
|
2013-02-10 12:22:46 +01:00
|
|
|
if( ( ret = ecp_point_write_binary( grp, pt, format,
|
|
|
|
olen, buf + 1, blen - 1) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write length to the first byte and update total length
|
|
|
|
*/
|
2013-10-11 18:58:55 +02:00
|
|
|
buf[0] = (unsigned char) *olen;
|
2013-02-10 12:22:46 +01:00
|
|
|
++*olen;
|
|
|
|
|
|
|
|
return 0;
|
2013-02-09 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
2012-11-10 00:27:12 +01:00
|
|
|
/*
|
2013-12-02 17:20:39 +01:00
|
|
|
* Import an ECP group from ASCII strings, case A == -3
|
2012-11-10 00:27:12 +01:00
|
|
|
*/
|
2013-12-02 17:20:39 +01:00
|
|
|
int ecp_group_read_string( ecp_group *grp, int radix,
|
|
|
|
const char *p, const char *b,
|
2013-10-23 20:19:57 +02:00
|
|
|
const char *gx, const char *gy, const char *n)
|
2012-11-10 00:27:12 +01:00
|
|
|
{
|
2012-11-10 14:44:24 +01:00
|
|
|
int ret;
|
2012-11-10 00:27:12 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
|
|
|
|
MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
|
|
|
|
MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
|
|
|
|
MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
|
2012-11-10 00:27:12 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
grp->pbits = mpi_msb( &grp->P );
|
|
|
|
grp->nbits = mpi_msb( &grp->N );
|
2012-11-10 00:27:12 +01:00
|
|
|
|
|
|
|
cleanup:
|
2013-10-23 20:19:57 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
ecp_group_free( grp );
|
2013-10-22 21:03:16 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
return( ret );
|
2013-10-22 21:03:16 +02:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
|
|
|
* Set a group from an ECParameters record (RFC 4492)
|
|
|
|
*/
|
|
|
|
int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
|
|
|
|
{
|
|
|
|
uint16_t tls_id;
|
|
|
|
const ecp_curve_info *curve_info;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We expect at least three bytes (see below)
|
|
|
|
*/
|
|
|
|
if( len < 3 )
|
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First byte is curve_type; only named_curve is handled
|
|
|
|
*/
|
|
|
|
if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
|
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Next two bytes are the namedcurve value
|
|
|
|
*/
|
|
|
|
tls_id = *(*buf)++;
|
|
|
|
tls_id <<= 8;
|
|
|
|
tls_id |= *(*buf)++;
|
|
|
|
|
|
|
|
if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
|
|
|
|
return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
|
|
|
|
|
|
|
|
return ecp_use_known_dp( grp, curve_info->grp_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the ECParameters record corresponding to a group (RFC 4492)
|
|
|
|
*/
|
|
|
|
int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
|
|
|
|
unsigned char *buf, size_t blen )
|
|
|
|
{
|
|
|
|
const ecp_curve_info *curve_info;
|
|
|
|
|
|
|
|
if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
|
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are going to write 3 bytes (see below)
|
|
|
|
*/
|
|
|
|
*olen = 3;
|
|
|
|
if( blen < *olen )
|
|
|
|
return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First byte is curve_type, always named_curve
|
|
|
|
*/
|
|
|
|
*buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Next two bytes are the namedcurve value
|
|
|
|
*/
|
|
|
|
buf[0] = curve_info->tls_id >> 8;
|
|
|
|
buf[1] = curve_info->tls_id & 0xFF;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
|
|
|
|
* See the documentation of struct ecp_group.
|
|
|
|
*
|
|
|
|
* This function is in the critial loop for ecp_mul, so pay attention to perf.
|
|
|
|
*/
|
|
|
|
static int ecp_modp( mpi *N, const ecp_group *grp )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( grp->modp == NULL )
|
|
|
|
return( mpi_mod_mpi( N, N, &grp->P ) );
|
|
|
|
|
|
|
|
/* N->s < 0 is a much faster test, which fails only if N is 0 */
|
|
|
|
if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
|
|
|
|
mpi_msb( N ) > 2 * grp->pbits )
|
|
|
|
{
|
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
MPI_CHK( grp->modp( N ) );
|
|
|
|
|
|
|
|
/* N->s < 0 is a much faster test, which fails only if N is 0 */
|
|
|
|
while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
|
|
|
|
MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
|
|
|
|
|
|
|
|
while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
|
|
|
|
/* we known P, N and the result are positive */
|
|
|
|
MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fast mod-p functions expect their argument to be in the 0..p^2 range.
|
|
|
|
*
|
|
|
|
* In order to guarantee that, we need to ensure that operands of
|
|
|
|
* mpi_mul_mpi are in the 0..p range. So, after each operation we will
|
|
|
|
* bring the result back to this range.
|
|
|
|
*
|
|
|
|
* The following macros are shortcuts for doing that.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
|
|
|
|
*/
|
2013-11-21 20:23:55 +01:00
|
|
|
#if defined(POLARSSL_SELF_TEST)
|
|
|
|
#define INC_MUL_COUNT mul_count++;
|
|
|
|
#else
|
|
|
|
#define INC_MUL_COUNT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
|
|
|
|
while( 0 )
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reduce a mpi mod p in-place, to use after mpi_sub_mpi
|
|
|
|
* N->s < 0 is a very fast test, which fails only if N is 0
|
|
|
|
*/
|
|
|
|
#define MOD_SUB( N ) \
|
|
|
|
while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
|
|
|
|
MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
|
|
|
|
* We known P, N and the result are positive, so sub_abs is correct, and
|
|
|
|
* a bit faster.
|
|
|
|
*/
|
|
|
|
#define MOD_ADD( N ) \
|
|
|
|
while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
|
|
|
|
MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
|
|
|
|
|
2013-12-04 23:15:46 +01:00
|
|
|
#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
|
|
|
|
/*
|
|
|
|
* For curves in short Weierstrass form, we do all the internal operations in
|
|
|
|
* Jacobian coordinates.
|
|
|
|
*
|
|
|
|
* For multiplication, we'll use a comb method with coutermeasueres against
|
|
|
|
* SPA, hence timing attacks.
|
|
|
|
*/
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
|
|
|
* Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
|
2013-11-20 22:57:38 +01:00
|
|
|
* Cost: 1N := 1I + 3M + 1S
|
2013-10-23 20:19:57 +02:00
|
|
|
*/
|
2013-12-02 19:44:41 +01:00
|
|
|
static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mpi Zi, ZZi;
|
|
|
|
|
|
|
|
if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
mpi_init( &Zi ); mpi_init( &ZZi );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X = X / Z^2 mod p
|
|
|
|
*/
|
|
|
|
MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Y = Y / Z^3 mod p
|
|
|
|
*/
|
|
|
|
MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Z = 1
|
|
|
|
*/
|
|
|
|
MPI_CHK( mpi_lset( &pt->Z, 1 ) );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
mpi_free( &Zi ); mpi_free( &ZZi );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2012-11-07 20:24:05 +01:00
|
|
|
/*
|
2013-11-16 15:50:12 +01:00
|
|
|
* Normalize jacobian coordinates of an array of (pointers to) points,
|
2013-10-23 20:19:57 +02:00
|
|
|
* using Montgomery's trick to perform only one inversion mod P.
|
|
|
|
* (See for example Cohen's "A Course in Computational Algebraic Number
|
|
|
|
* Theory", Algorithm 10.3.4.)
|
|
|
|
*
|
|
|
|
* Warning: fails (returning an error) if one of the points is zero!
|
2013-12-05 10:26:01 +01:00
|
|
|
* This should never happen, see choice of w in ecp_mul_comb().
|
2013-11-20 22:57:38 +01:00
|
|
|
*
|
|
|
|
* Cost: 1N(t) := 1I + (6t - 3)M + 1S
|
2012-11-07 20:24:05 +01:00
|
|
|
*/
|
2013-12-02 19:44:41 +01:00
|
|
|
static int ecp_normalize_jac_many( const ecp_group *grp,
|
2013-12-04 23:15:46 +01:00
|
|
|
ecp_point *T[], size_t t_len )
|
2012-11-07 20:24:05 +01:00
|
|
|
{
|
2013-10-23 20:19:57 +02:00
|
|
|
int ret;
|
|
|
|
size_t i;
|
|
|
|
mpi *c, u, Zi, ZZi;
|
2013-02-10 12:06:19 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
if( t_len < 2 )
|
2013-12-02 19:44:41 +01:00
|
|
|
return( ecp_normalize_jac( grp, *T ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
|
|
|
if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
|
|
|
|
return( POLARSSL_ERR_ECP_MALLOC_FAILED );
|
|
|
|
|
|
|
|
mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
|
|
|
|
for( i = 0; i < t_len; i++ )
|
|
|
|
mpi_init( &c[i] );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* c[i] = Z_0 * ... * Z_i
|
|
|
|
*/
|
2013-11-16 15:50:12 +01:00
|
|
|
MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
for( i = 1; i < t_len; i++ )
|
2012-11-07 20:24:05 +01:00
|
|
|
{
|
2013-11-16 15:50:12 +01:00
|
|
|
MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
MOD_MUL( c[i] );
|
|
|
|
}
|
2012-11-10 14:23:17 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
|
|
|
* u = 1 / (Z_0 * ... * Z_n) mod P
|
|
|
|
*/
|
|
|
|
MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
|
2012-11-10 14:23:17 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
for( i = t_len - 1; ; i-- )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Zi = 1 / Z_i mod p
|
|
|
|
* u = 1 / (Z_0 * ... * Z_i) mod P
|
|
|
|
*/
|
|
|
|
if( i == 0 ) {
|
|
|
|
MPI_CHK( mpi_copy( &Zi, &u ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-16 15:50:12 +01:00
|
|
|
MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
2012-11-10 14:23:17 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
|
|
|
* proceed as in normalize()
|
|
|
|
*/
|
2013-11-16 15:50:12 +01:00
|
|
|
MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
|
2013-12-30 17:31:56 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Post-precessing: reclaim some memory by shrinking coordinates
|
|
|
|
* - not storing Z (always 1)
|
|
|
|
* - shrinking other coordinates, but still keeping the same number of
|
|
|
|
* limbs as P, as otherwise it will too likely be regrown too fast.
|
|
|
|
*/
|
2013-12-30 19:33:33 +01:00
|
|
|
MPI_CHK( mpi_shrink( &T[i]->X, grp->P.n ) );
|
|
|
|
MPI_CHK( mpi_shrink( &T[i]->Y, grp->P.n ) );
|
2013-12-30 17:31:56 +01:00
|
|
|
mpi_free( &T[i]->Z );
|
2012-11-10 14:23:17 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
if( i == 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
|
|
|
|
for( i = 0; i < t_len; i++ )
|
|
|
|
mpi_free( &c[i] );
|
|
|
|
polarssl_free( c );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-11-21 17:47:12 +01:00
|
|
|
/*
|
|
|
|
* Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
|
|
|
|
* "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
|
|
|
|
*/
|
2013-12-02 19:44:41 +01:00
|
|
|
static int ecp_safe_invert_jac( const ecp_group *grp,
|
2013-11-21 17:47:12 +01:00
|
|
|
ecp_point *Q,
|
|
|
|
unsigned char inv )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char nonzero;
|
|
|
|
mpi mQY;
|
|
|
|
|
|
|
|
mpi_init( &mQY );
|
|
|
|
|
|
|
|
/* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
|
|
|
|
MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
|
|
|
|
nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
|
|
|
|
MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
mpi_free( &mQY );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
|
|
|
* Point doubling R = 2 P, Jacobian coordinates
|
|
|
|
*
|
|
|
|
* http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
|
|
|
|
* with heavy variable renaming, some reordering and one minor modification
|
|
|
|
* (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
|
|
|
|
* in order to use a lot less intermediate variables (6 vs 25).
|
2013-11-20 22:57:38 +01:00
|
|
|
*
|
|
|
|
* Cost: 1D := 2M + 8S
|
2013-10-23 20:19:57 +02:00
|
|
|
*/
|
|
|
|
static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
|
|
|
|
const ecp_point *P )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mpi T1, T2, T3, X3, Y3, Z3;
|
|
|
|
|
|
|
|
#if defined(POLARSSL_SELF_TEST)
|
|
|
|
dbl_count++;
|
2013-10-23 16:11:52 +02:00
|
|
|
#endif
|
2012-11-07 20:24:05 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
|
|
|
|
mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
|
2013-10-08 12:04:56 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
|
|
|
|
MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
|
|
|
|
MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
|
|
|
|
MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
|
2013-12-06 12:41:30 +01:00
|
|
|
|
|
|
|
/* Special case for A = -3 */
|
|
|
|
if( grp->A.p == NULL )
|
|
|
|
{
|
|
|
|
MPI_CHK( mpi_mul_int( &X3, &X3, 3 ) );
|
|
|
|
X3.s = -1; /* mpi_mul_int doesn't handle negative numbers */
|
|
|
|
MOD_SUB( X3 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
|
|
|
|
MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
|
|
|
|
MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
|
2013-10-08 12:04:56 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
MPI_CHK( mpi_copy( &R->X, &X3 ) );
|
|
|
|
MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
|
|
|
|
MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
|
2013-10-08 12:04:56 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
cleanup:
|
|
|
|
mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
|
|
|
|
mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
|
|
|
|
|
|
|
|
return( ret );
|
2013-02-09 17:03:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-21 18:20:43 +01:00
|
|
|
* Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
|
2013-10-23 20:19:57 +02:00
|
|
|
*
|
|
|
|
* The coordinates of Q must be normalized (= affine),
|
|
|
|
* but those of P don't need to. R is not normalized.
|
|
|
|
*
|
2013-11-21 19:19:54 +01:00
|
|
|
* Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
|
2013-12-05 10:26:01 +01:00
|
|
|
* None of these cases can happen as intermediate step in ecp_mul_comb():
|
2013-11-21 19:19:54 +01:00
|
|
|
* - at each step, P, Q and R are multiples of the base point, the factor
|
|
|
|
* being less than its order, so none of them is zero;
|
|
|
|
* - Q is an odd multiple of the base point, P an even multiple,
|
|
|
|
* due to the choice of precomputed points in the modified comb method.
|
|
|
|
* So branches for these cases do not leak secret information.
|
|
|
|
*
|
2013-12-30 16:04:55 +01:00
|
|
|
* We accept Q->Z being unset (saving memory in tables) as meaning 1.
|
|
|
|
*
|
2013-11-20 22:57:38 +01:00
|
|
|
* Cost: 1A := 8M + 3S
|
2013-02-09 17:03:58 +01:00
|
|
|
*/
|
2013-10-23 20:19:57 +02:00
|
|
|
static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
|
2013-11-21 18:20:43 +01:00
|
|
|
const ecp_point *P, const ecp_point *Q )
|
2013-02-09 17:03:58 +01:00
|
|
|
{
|
2013-10-23 20:19:57 +02:00
|
|
|
int ret;
|
|
|
|
mpi T1, T2, T3, T4, X, Y, Z;
|
|
|
|
|
|
|
|
#if defined(POLARSSL_SELF_TEST)
|
|
|
|
add_count++;
|
|
|
|
#endif
|
2013-02-09 17:03:58 +01:00
|
|
|
|
|
|
|
/*
|
2013-11-21 19:19:54 +01:00
|
|
|
* Trivial cases: P == 0 or Q == 0 (case 1)
|
2013-02-09 17:03:58 +01:00
|
|
|
*/
|
2013-10-23 20:19:57 +02:00
|
|
|
if( mpi_cmp_int( &P->Z, 0 ) == 0 )
|
2013-11-21 18:20:43 +01:00
|
|
|
return( ecp_copy( R, Q ) );
|
2013-10-23 20:19:57 +02:00
|
|
|
|
2013-12-30 16:04:55 +01:00
|
|
|
if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 0 ) == 0 )
|
2013-11-21 18:20:43 +01:00
|
|
|
return( ecp_copy( R, P ) );
|
2013-02-09 17:03:58 +01:00
|
|
|
|
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Make sure Q coordinates are normalized
|
2013-02-09 17:03:58 +01:00
|
|
|
*/
|
2013-12-30 16:04:55 +01:00
|
|
|
if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 1 ) != 0 )
|
2013-02-09 17:03:58 +01:00
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
|
|
|
|
mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
|
|
|
|
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
|
2013-02-10 12:06:19 +01:00
|
|
|
|
2013-11-21 19:19:54 +01:00
|
|
|
/* Special cases (2) and (3) */
|
2013-10-23 20:19:57 +02:00
|
|
|
if( mpi_cmp_int( &T1, 0 ) == 0 )
|
|
|
|
{
|
|
|
|
if( mpi_cmp_int( &T2, 0 ) == 0 )
|
|
|
|
{
|
|
|
|
ret = ecp_double_jac( grp, R, P );
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = ecp_set_zero( R );
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 18:14:50 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
|
|
|
|
MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
|
2013-09-16 16:19:53 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
MPI_CHK( mpi_copy( &R->X, &X ) );
|
|
|
|
MPI_CHK( mpi_copy( &R->Y, &Y ) );
|
|
|
|
MPI_CHK( mpi_copy( &R->Z, &Z ) );
|
2013-02-10 12:06:19 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
cleanup:
|
2013-02-10 12:06:19 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
|
|
|
|
mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
|
2013-02-10 12:06:19 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
return( ret );
|
2012-11-07 20:24:05 +01:00
|
|
|
}
|
2012-11-05 17:34:55 +01:00
|
|
|
|
2013-09-16 17:30:04 +02:00
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Addition: R = P + Q, result's coordinates normalized
|
2013-09-16 17:30:04 +02:00
|
|
|
*/
|
2013-10-23 20:19:57 +02:00
|
|
|
int ecp_add( const ecp_group *grp, ecp_point *R,
|
|
|
|
const ecp_point *P, const ecp_point *Q )
|
2013-09-16 16:19:53 +02:00
|
|
|
{
|
2013-10-23 20:19:57 +02:00
|
|
|
int ret;
|
2013-09-16 16:19:53 +02:00
|
|
|
|
2013-12-04 23:15:46 +01:00
|
|
|
if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
|
2013-12-04 20:52:04 +01:00
|
|
|
return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
|
|
|
|
|
2013-11-21 18:20:43 +01:00
|
|
|
MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
|
2013-12-02 19:44:41 +01:00
|
|
|
MPI_CHK( ecp_normalize_jac( grp, R ) );
|
2013-09-16 17:30:04 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
cleanup:
|
|
|
|
return( ret );
|
2013-09-16 16:19:53 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 17:30:04 +02:00
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Subtraction: R = P - Q, result's coordinates normalized
|
2013-09-16 17:30:04 +02:00
|
|
|
*/
|
2013-10-23 20:19:57 +02:00
|
|
|
int ecp_sub( const ecp_group *grp, ecp_point *R,
|
|
|
|
const ecp_point *P, const ecp_point *Q )
|
2013-09-16 16:19:53 +02:00
|
|
|
{
|
2013-10-23 20:19:57 +02:00
|
|
|
int ret;
|
2013-11-21 18:20:43 +01:00
|
|
|
ecp_point mQ;
|
|
|
|
|
|
|
|
ecp_point_init( &mQ );
|
2013-09-16 16:19:53 +02:00
|
|
|
|
2013-12-04 23:15:46 +01:00
|
|
|
if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
|
2013-12-04 20:52:04 +01:00
|
|
|
return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
|
|
|
|
|
2013-11-21 18:20:43 +01:00
|
|
|
/* mQ = - Q */
|
2013-12-30 19:33:33 +01:00
|
|
|
MPI_CHK( ecp_copy( &mQ, Q ) );
|
2013-11-21 18:20:43 +01:00
|
|
|
if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
|
|
|
|
MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
|
|
|
|
|
|
|
|
MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
|
2013-12-02 19:44:41 +01:00
|
|
|
MPI_CHK( ecp_normalize_jac( grp, R ) );
|
2013-09-16 17:30:04 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
cleanup:
|
2013-11-21 18:20:43 +01:00
|
|
|
ecp_point_free( &mQ );
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
return( ret );
|
2013-09-16 16:19:53 +02:00
|
|
|
}
|
2013-09-16 17:30:04 +02:00
|
|
|
|
2012-11-02 18:14:40 +01:00
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* Randomize jacobian coordinates:
|
|
|
|
* (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
|
2013-12-02 19:44:41 +01:00
|
|
|
* This is sort of the reverse operation of ecp_normalize_jac().
|
2013-11-21 10:53:59 +01:00
|
|
|
*
|
|
|
|
* This countermeasure was first suggested in [2].
|
2013-10-09 16:09:46 +02:00
|
|
|
*/
|
2013-12-03 13:28:21 +01:00
|
|
|
static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
|
2013-10-23 20:19:57 +02:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
2013-10-09 16:09:46 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2013-10-23 20:19:57 +02:00
|
|
|
mpi l, ll;
|
|
|
|
size_t p_size = (grp->pbits + 7) / 8;
|
|
|
|
int count = 0;
|
2013-10-10 12:44:27 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
mpi_init( &l ); mpi_init( &ll );
|
2013-10-10 15:55:39 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/* Generate l such that 1 < l < p */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
mpi_fill_random( &l, p_size, f_rng, p_rng );
|
2013-10-10 12:44:27 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
|
|
|
|
mpi_shift_r( &l, 1 );
|
2013-10-09 16:09:46 +02:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
if( count++ > 10 )
|
|
|
|
return( POLARSSL_ERR_ECP_RANDOM_FAILED );
|
|
|
|
}
|
|
|
|
while( mpi_cmp_int( &l, 1 ) <= 0 );
|
|
|
|
|
|
|
|
/* Z = l * Z */
|
|
|
|
MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
|
|
|
|
|
|
|
|
/* X = l^2 * X */
|
|
|
|
MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
|
|
|
|
|
|
|
|
/* Y = l^3 * Y */
|
|
|
|
MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
|
2013-10-09 16:09:46 +02:00
|
|
|
|
|
|
|
cleanup:
|
2013-10-23 20:19:57 +02:00
|
|
|
mpi_free( &l ); mpi_free( &ll );
|
2013-10-09 16:09:46 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-11-16 15:50:12 +01:00
|
|
|
/*
|
2013-11-20 18:39:55 +01:00
|
|
|
* Check and define parameters used by the comb method (see below for details)
|
|
|
|
*/
|
|
|
|
#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
|
|
|
|
#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* d = ceil( n / w ) */
|
|
|
|
#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
|
|
|
|
|
|
|
|
/* number of precomputed points */
|
|
|
|
#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the representation of m that will be used with our comb method.
|
2013-11-16 15:50:12 +01:00
|
|
|
*
|
|
|
|
* The basic comb method is described in GECC 3.44 for example. We use a
|
2013-11-20 14:47:19 +01:00
|
|
|
* modified version that provides resistance to SPA by avoiding zero
|
|
|
|
* digits in the representation as in [3]. We modify the method further by
|
|
|
|
* requiring that all K_i be odd, which has the small cost that our
|
2013-11-20 18:39:55 +01:00
|
|
|
* representation uses one more K_i, due to carries.
|
2013-11-20 14:47:19 +01:00
|
|
|
*
|
|
|
|
* Also, for the sake of compactness, only the seven low-order bits of x[i]
|
|
|
|
* are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
|
|
|
|
* the paper): it is set if and only if if s_i == -1;
|
2013-11-16 15:50:12 +01:00
|
|
|
*
|
|
|
|
* Calling conventions:
|
2013-11-20 14:47:19 +01:00
|
|
|
* - x is an array of size d + 1
|
2013-11-20 18:39:55 +01:00
|
|
|
* - w is the size, ie number of teeth, of the comb, and must be between
|
|
|
|
* 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
|
2013-11-20 14:47:19 +01:00
|
|
|
* - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
|
|
|
|
* (the result will be incorrect if these assumptions are not satisfied)
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2013-11-20 14:47:19 +01:00
|
|
|
static void ecp_comb_fixed( unsigned char x[], size_t d,
|
2013-11-16 15:50:12 +01:00
|
|
|
unsigned char w, const mpi *m )
|
|
|
|
{
|
|
|
|
size_t i, j;
|
2013-11-20 14:47:19 +01:00
|
|
|
unsigned char c, cc, adjust;
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-20 14:47:19 +01:00
|
|
|
memset( x, 0, d+1 );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-21 09:50:00 +01:00
|
|
|
/* First get the classical comb values (except for x_d = 0) */
|
|
|
|
for( i = 0; i < d; i++ )
|
2013-11-16 15:50:12 +01:00
|
|
|
for( j = 0; j < w; j++ )
|
|
|
|
x[i] |= mpi_get_bit( m, i + d * j ) << j;
|
|
|
|
|
2013-11-21 09:50:00 +01:00
|
|
|
/* Now make sure x_1 .. x_d are odd */
|
|
|
|
c = 0;
|
|
|
|
for( i = 1; i <= d; i++ )
|
|
|
|
{
|
2013-11-20 14:47:19 +01:00
|
|
|
/* Add carry and update it */
|
|
|
|
cc = x[i] & c;
|
|
|
|
x[i] = x[i] ^ c;
|
|
|
|
c = cc;
|
|
|
|
|
2013-11-21 09:50:00 +01:00
|
|
|
/* Adjust if needed, avoiding branches */
|
2013-11-20 14:47:19 +01:00
|
|
|
adjust = 1 - ( x[i] & 0x01 );
|
|
|
|
c |= x[i] & ( x[i-1] * adjust );
|
|
|
|
x[i] = x[i] ^ ( x[i-1] * adjust );
|
|
|
|
x[i-1] |= adjust << 7;
|
2013-11-16 15:50:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Precompute points for the comb method
|
|
|
|
*
|
2013-11-20 14:47:19 +01:00
|
|
|
* If i = i_{w-1} ... i_1 is the binary representation of i, then
|
|
|
|
* T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P
|
2013-11-16 15:50:12 +01:00
|
|
|
*
|
2013-11-20 22:57:38 +01:00
|
|
|
* T must be able to hold 2^{w - 1} elements
|
|
|
|
*
|
|
|
|
* Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
|
|
|
static int ecp_precompute_comb( const ecp_group *grp,
|
|
|
|
ecp_point T[], const ecp_point *P,
|
|
|
|
unsigned char w, size_t d )
|
|
|
|
{
|
|
|
|
int ret;
|
2013-11-20 18:39:55 +01:00
|
|
|
unsigned char i, k;
|
|
|
|
size_t j;
|
|
|
|
ecp_point *cur, *TT[COMB_MAX_PRE - 1];
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
/*
|
2013-11-20 14:47:19 +01:00
|
|
|
* Set T[0] = P and
|
|
|
|
* T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
|
|
|
MPI_CHK( ecp_copy( &T[0], P ) );
|
|
|
|
|
2013-11-20 14:47:19 +01:00
|
|
|
k = 0;
|
|
|
|
for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
2013-11-20 14:47:19 +01:00
|
|
|
cur = T + i;
|
|
|
|
MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
for( j = 0; j < d; j++ )
|
|
|
|
MPI_CHK( ecp_double_jac( grp, cur, cur ) );
|
2013-11-20 14:47:19 +01:00
|
|
|
|
|
|
|
TT[k++] = cur;
|
2013-11-16 15:50:12 +01:00
|
|
|
}
|
|
|
|
|
2013-12-30 19:33:33 +01:00
|
|
|
MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the remaining ones using the minimal number of additions
|
2013-11-20 14:47:19 +01:00
|
|
|
* Be careful to update T[2^l] only after using it!
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2013-11-20 14:47:19 +01:00
|
|
|
k = 0;
|
|
|
|
for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
2013-11-20 14:47:19 +01:00
|
|
|
j = i;
|
|
|
|
while( j-- )
|
|
|
|
{
|
2013-12-30 19:33:33 +01:00
|
|
|
MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
|
2013-11-20 14:47:19 +01:00
|
|
|
TT[k++] = &T[i + j];
|
|
|
|
}
|
2013-11-16 15:50:12 +01:00
|
|
|
}
|
|
|
|
|
2013-12-30 19:33:33 +01:00
|
|
|
MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
|
2013-11-21 10:08:50 +01:00
|
|
|
|
2013-11-16 15:50:12 +01:00
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-20 14:47:19 +01:00
|
|
|
* Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
|
|
|
static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
|
2013-11-25 18:28:53 +01:00
|
|
|
const ecp_point T[], unsigned char t_len,
|
2013-11-21 20:00:38 +01:00
|
|
|
unsigned char i )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2013-11-21 20:00:38 +01:00
|
|
|
unsigned char ii, j;
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-21 20:00:38 +01:00
|
|
|
/* Ignore the "sign" bit and scale down */
|
|
|
|
ii = ( i & 0x7Fu ) >> 1;
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-21 20:00:38 +01:00
|
|
|
/* Read the whole table to thwart cache-based timing attacks */
|
|
|
|
for( j = 0; j < t_len; j++ )
|
|
|
|
{
|
|
|
|
MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
|
|
|
|
MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
|
|
|
|
}
|
|
|
|
|
2013-11-21 17:47:12 +01:00
|
|
|
/* Safely invert result if i is "negative" */
|
2013-12-02 19:44:41 +01:00
|
|
|
MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Core multiplication algorithm for the (modified) comb method.
|
|
|
|
* This part is actually common with the basic comb method (GECC 3.44)
|
2013-11-20 22:57:38 +01:00
|
|
|
*
|
|
|
|
* Cost: d A + d D + 1 R
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
|
|
|
static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
|
2013-11-25 18:28:53 +01:00
|
|
|
const ecp_point T[], unsigned char t_len,
|
2013-11-20 20:07:26 +01:00
|
|
|
const unsigned char x[], size_t d,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ecp_point Txi;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
ecp_point_init( &Txi );
|
|
|
|
|
2013-11-20 20:07:26 +01:00
|
|
|
/* Start with a non-zero point and randomize its coordinates */
|
2013-11-20 14:47:19 +01:00
|
|
|
i = d;
|
2013-11-21 20:00:38 +01:00
|
|
|
MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
|
2013-12-30 16:04:55 +01:00
|
|
|
MPI_CHK( mpi_lset( &R->Z, 1 ) );
|
2013-11-20 20:07:26 +01:00
|
|
|
if( f_rng != 0 )
|
2013-12-03 13:28:21 +01:00
|
|
|
MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
while( i-- != 0 )
|
|
|
|
{
|
|
|
|
MPI_CHK( ecp_double_jac( grp, R, R ) );
|
2013-11-21 20:00:38 +01:00
|
|
|
MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
|
2013-11-21 18:20:43 +01:00
|
|
|
MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
ecp_point_free( &Txi );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-12-04 11:49:20 +01:00
|
|
|
* Multiplication using the comb method,
|
|
|
|
* for curves in short Weierstrass form
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2013-12-04 11:49:20 +01:00
|
|
|
static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
|
|
|
|
const mpi *m, const ecp_point *P,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2013-11-21 20:00:38 +01:00
|
|
|
unsigned char w, m_is_odd, p_eq_g, pre_len, i;
|
|
|
|
size_t d;
|
2013-11-20 18:39:55 +01:00
|
|
|
unsigned char k[COMB_MAX_D + 1];
|
2013-11-21 19:19:54 +01:00
|
|
|
ecp_point *T;
|
|
|
|
mpi M, mm;
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-21 09:28:03 +01:00
|
|
|
mpi_init( &M );
|
2013-11-21 19:19:54 +01:00
|
|
|
mpi_init( &mm );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-12-04 11:49:20 +01:00
|
|
|
/* we need N to be odd to trnaform m in an odd number, check now */
|
|
|
|
if( mpi_get_bit( &grp->N, 0 ) != 1 )
|
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
|
|
|
|
2013-11-20 22:57:38 +01:00
|
|
|
/*
|
|
|
|
* Minimize the number of multiplications, that is minimize
|
2013-11-21 18:33:36 +01:00
|
|
|
* 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
|
2013-11-20 22:57:38 +01:00
|
|
|
* (see costs of the various parts, with 1S = 1M)
|
|
|
|
*/
|
|
|
|
w = grp->nbits >= 384 ? 5 : 4;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If P == G, pre-compute a bit more, since this may be re-used later.
|
2013-12-30 18:41:16 +01:00
|
|
|
* Just adding one avoids upping the cost of the first mul too much,
|
|
|
|
* and the memory cost too.
|
2013-11-20 22:57:38 +01:00
|
|
|
*/
|
2013-12-30 18:41:16 +01:00
|
|
|
#if POLARSSL_ECP_FIXED_POINT_OPTIM == 1
|
2013-12-04 11:49:20 +01:00
|
|
|
p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
|
|
|
|
mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
|
2013-11-20 22:57:38 +01:00
|
|
|
if( p_eq_g )
|
|
|
|
w++;
|
2013-12-30 18:41:16 +01:00
|
|
|
#else
|
|
|
|
p_eq_g = 0;
|
|
|
|
#endif
|
2013-11-20 22:57:38 +01:00
|
|
|
|
|
|
|
/*
|
2013-11-21 18:33:36 +01:00
|
|
|
* Make sure w is within bounds.
|
2013-11-20 22:57:38 +01:00
|
|
|
* (The last test is useful only for very small curves in the test suite.)
|
|
|
|
*/
|
|
|
|
if( w > POLARSSL_ECP_WINDOW_SIZE )
|
|
|
|
w = POLARSSL_ECP_WINDOW_SIZE;
|
2013-11-21 18:33:36 +01:00
|
|
|
if( w >= grp->nbits )
|
2013-11-20 22:57:38 +01:00
|
|
|
w = 2;
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2013-11-20 22:57:38 +01:00
|
|
|
/* Other sizes that depend on w */
|
2013-11-20 18:39:55 +01:00
|
|
|
pre_len = 1U << ( w - 1 );
|
2013-11-16 15:50:12 +01:00
|
|
|
d = ( grp->nbits + w - 1 ) / w;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare precomputed points: if P == G we want to
|
2013-11-21 09:50:00 +01:00
|
|
|
* use grp->T if already initialized, or initialize it.
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2013-11-21 19:19:54 +01:00
|
|
|
T = p_eq_g ? grp->T : NULL;
|
2013-11-21 09:50:00 +01:00
|
|
|
|
|
|
|
if( T == NULL )
|
2013-11-16 15:50:12 +01:00
|
|
|
{
|
|
|
|
T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
|
|
|
|
if( T == NULL )
|
|
|
|
{
|
|
|
|
ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < pre_len; i++ )
|
|
|
|
ecp_point_init( &T[i] );
|
|
|
|
|
|
|
|
MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
|
|
|
|
|
|
|
|
if( p_eq_g )
|
|
|
|
{
|
|
|
|
grp->T = T;
|
|
|
|
grp->T_size = pre_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-21 19:19:54 +01:00
|
|
|
* Make sure M is odd (M = m or M = N - m, since N is odd)
|
|
|
|
* using the fact that m * P = - (N - m) * P
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
|
|
|
m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
|
|
|
|
MPI_CHK( mpi_copy( &M, m ) );
|
2013-11-21 19:19:54 +01:00
|
|
|
MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
|
|
|
|
MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
/*
|
2013-11-21 19:19:54 +01:00
|
|
|
* Go for comb multiplication, R = M * P
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
|
|
|
ecp_comb_fixed( k, d, w, &M );
|
2013-11-21 20:00:38 +01:00
|
|
|
MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
|
|
|
/*
|
2013-11-21 19:19:54 +01:00
|
|
|
* Now get m * P from M * P and normalize it
|
2013-11-16 15:50:12 +01:00
|
|
|
*/
|
2013-12-02 19:44:41 +01:00
|
|
|
MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
|
|
|
|
MPI_CHK( ecp_normalize_jac( grp, R ) );
|
2013-11-16 15:50:12 +01:00
|
|
|
|
2012-11-19 21:23:27 +01:00
|
|
|
cleanup:
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
if( T != NULL && ! p_eq_g )
|
|
|
|
{
|
|
|
|
for( i = 0; i < pre_len; i++ )
|
|
|
|
ecp_point_free( &T[i] );
|
|
|
|
polarssl_free( T );
|
|
|
|
}
|
2012-11-19 21:23:27 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
mpi_free( &M );
|
2013-11-21 19:19:54 +01:00
|
|
|
mpi_free( &mm );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
ecp_point_free( R );
|
2012-11-02 18:14:40 +01:00
|
|
|
|
2013-12-03 12:02:28 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-12-04 23:15:46 +01:00
|
|
|
#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
|
|
|
|
|
|
|
|
#if defined(POLARSSL_ECP_MONTGOMERY)
|
|
|
|
/*
|
|
|
|
* For Montgomery curves, we do all the internal arithmetic in projective
|
|
|
|
* coordinates. Import/export of points uses only the x coordinates, which is
|
|
|
|
* internaly represented as X / Z.
|
|
|
|
*
|
|
|
|
* For scalar multiplication, we'll use a Montgomery ladder.
|
|
|
|
*/
|
|
|
|
|
2013-12-03 12:02:28 +01:00
|
|
|
/*
|
|
|
|
* Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
|
|
|
|
* Cost: 1M + 1I
|
|
|
|
*/
|
|
|
|
static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
|
|
|
|
MPI_CHK( mpi_lset( &P->Z, 1 ) );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-12-03 13:28:21 +01:00
|
|
|
/*
|
|
|
|
* Randomize projective x/z coordinates:
|
|
|
|
* (X, Z) -> (l X, l Z) for random l
|
|
|
|
* This is sort of the reverse operation of ecp_normalize_mxz().
|
|
|
|
*
|
|
|
|
* This countermeasure was first suggested in [2].
|
|
|
|
* Cost: 2M
|
|
|
|
*/
|
|
|
|
static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mpi l;
|
|
|
|
size_t p_size = (grp->pbits + 7) / 8;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
mpi_init( &l );
|
|
|
|
|
|
|
|
/* Generate l such that 1 < l < p */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
mpi_fill_random( &l, p_size, f_rng, p_rng );
|
|
|
|
|
|
|
|
while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
|
|
|
|
mpi_shift_r( &l, 1 );
|
|
|
|
|
|
|
|
if( count++ > 10 )
|
|
|
|
return( POLARSSL_ERR_ECP_RANDOM_FAILED );
|
|
|
|
}
|
|
|
|
while( mpi_cmp_int( &l, 1 ) <= 0 );
|
|
|
|
|
|
|
|
MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
mpi_free( &l );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2013-12-03 12:02:28 +01:00
|
|
|
/*
|
|
|
|
* Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
|
|
|
|
* for Montgomery curves in x/z coordinates.
|
|
|
|
*
|
|
|
|
* http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
|
|
|
|
* with
|
|
|
|
* d = X1
|
|
|
|
* P = (X2, Z2)
|
|
|
|
* Q = (X3, Z3)
|
|
|
|
* R = (X4, Z4)
|
|
|
|
* S = (X5, Z5)
|
|
|
|
* and eliminating temporary variables tO, ..., t4.
|
|
|
|
*
|
|
|
|
* Cost: 5M + 4S
|
|
|
|
*/
|
|
|
|
static int ecp_double_add_mxz( const ecp_group *grp,
|
|
|
|
ecp_point *R, ecp_point *S,
|
|
|
|
const ecp_point *P, const ecp_point *Q,
|
|
|
|
const mpi *d )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mpi A, AA, B, BB, E, C, D, DA, CB;
|
|
|
|
|
|
|
|
mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
|
|
|
|
mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
|
|
|
|
mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
|
|
|
|
|
|
|
|
MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
|
|
|
|
MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
|
|
|
|
MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
|
|
|
|
MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
|
|
|
|
MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
|
|
|
|
mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
|
|
|
|
mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-12-04 11:49:20 +01:00
|
|
|
* Multiplication with Montgomery ladder in x/z coordinates,
|
|
|
|
* for curves in Montgomery form
|
2013-12-03 12:02:28 +01:00
|
|
|
*/
|
2013-12-04 11:49:20 +01:00
|
|
|
static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
|
|
|
|
const mpi *m, const ecp_point *P,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
2013-12-03 12:02:28 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t i;
|
2013-12-04 21:54:36 +01:00
|
|
|
unsigned char b;
|
2013-12-03 12:02:28 +01:00
|
|
|
ecp_point RP;
|
|
|
|
mpi PX;
|
|
|
|
|
|
|
|
ecp_point_init( &RP ); mpi_init( &PX );
|
|
|
|
|
2013-12-03 13:28:21 +01:00
|
|
|
/* Save PX and read from P before writing to R, in case P == R */
|
2013-12-03 12:02:28 +01:00
|
|
|
mpi_copy( &PX, &P->X );
|
|
|
|
MPI_CHK( ecp_copy( &RP, P ) );
|
2013-12-04 18:39:17 +01:00
|
|
|
|
|
|
|
/* Set R to zero in modified x/z coordinates */
|
|
|
|
MPI_CHK( mpi_lset( &R->X, 1 ) );
|
|
|
|
MPI_CHK( mpi_lset( &R->Z, 0 ) );
|
|
|
|
mpi_free( &R->Y );
|
2013-12-03 12:02:28 +01:00
|
|
|
|
2013-12-05 10:48:42 +01:00
|
|
|
/* RP.X might be sligtly larger than P, so reduce it */
|
|
|
|
MOD_ADD( RP.X );
|
|
|
|
|
2013-12-03 13:28:21 +01:00
|
|
|
/* Randomize coordinates of the starting point */
|
2013-12-04 18:39:17 +01:00
|
|
|
if( f_rng != NULL )
|
|
|
|
MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
|
2013-12-03 12:02:28 +01:00
|
|
|
|
2013-12-04 21:54:36 +01:00
|
|
|
/* Loop invariant: R = result so far, RP = R + P */
|
2013-12-04 18:39:17 +01:00
|
|
|
i = mpi_msb( m ); /* one past the (zero-based) most significant bit */
|
2013-12-03 12:02:28 +01:00
|
|
|
while( i-- > 0 )
|
|
|
|
{
|
2013-12-04 21:54:36 +01:00
|
|
|
b = mpi_get_bit( m, i );
|
|
|
|
/*
|
|
|
|
* if (b) R = 2R + P else R = 2R,
|
|
|
|
* which is:
|
|
|
|
* if (b) double_add( RP, R, RP, R )
|
|
|
|
* else double_add( R, RP, R, RP )
|
|
|
|
* but using safe conditional swaps to avoid leaks
|
|
|
|
*/
|
|
|
|
MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
|
|
|
|
MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
|
|
|
|
MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
|
|
|
|
MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
|
|
|
|
MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
|
2013-12-03 12:02:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MPI_CHK( ecp_normalize_mxz( grp, R ) );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
ecp_point_free( &RP ); mpi_free( &PX );
|
|
|
|
|
2012-11-08 23:21:46 +01:00
|
|
|
return( ret );
|
2012-11-02 18:14:40 +01:00
|
|
|
}
|
|
|
|
|
2013-12-04 23:15:46 +01:00
|
|
|
#endif /* POLARSSL_ECP_MONTGOMERY */
|
|
|
|
|
2013-12-04 11:49:20 +01:00
|
|
|
/*
|
|
|
|
* Multiplication R = m * P
|
|
|
|
*/
|
|
|
|
int ecp_mul( ecp_group *grp, ecp_point *R,
|
|
|
|
const mpi *m, const ecp_point *P,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Common sanity checks */
|
|
|
|
if( mpi_cmp_int( &P->Z, 1 ) != 0 )
|
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
|
|
|
|
( ret = ecp_check_pubkey( grp, P ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
2013-12-04 23:15:46 +01:00
|
|
|
#if defined(POLARSSL_ECP_MONTGOMERY)
|
|
|
|
if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
|
2013-12-04 11:49:20 +01:00
|
|
|
return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
|
2013-12-04 23:15:46 +01:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
|
|
|
|
if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
|
2013-12-04 11:49:20 +01:00
|
|
|
return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
|
2013-12-04 23:15:46 +01:00
|
|
|
#endif
|
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
2013-12-04 11:49:20 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 10:06:06 +01:00
|
|
|
#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
|
2012-11-17 19:54:20 +01:00
|
|
|
/*
|
2013-12-05 10:06:06 +01:00
|
|
|
* Check that an affine point is valid as a public key,
|
|
|
|
* short weierstrass curves (SEC1 3.2.3.1)
|
2012-11-17 19:54:20 +01:00
|
|
|
*/
|
2013-12-05 10:06:06 +01:00
|
|
|
static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt )
|
2012-11-17 19:54:20 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2013-10-23 20:19:57 +02:00
|
|
|
mpi YY, RHS;
|
2012-11-17 19:54:20 +01:00
|
|
|
|
2013-12-04 11:08:01 +01:00
|
|
|
/* pt coordinates must be normalized for our checks */
|
2013-10-23 20:19:57 +02:00
|
|
|
if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
|
|
|
|
mpi_cmp_int( &pt->Y, 0 ) < 0 ||
|
|
|
|
mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
|
|
|
|
mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
|
|
|
|
return( POLARSSL_ERR_ECP_INVALID_KEY );
|
2012-11-17 19:54:20 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
mpi_init( &YY ); mpi_init( &RHS );
|
2012-11-17 19:54:20 +01:00
|
|
|
|
|
|
|
/*
|
2013-10-23 20:19:57 +02:00
|
|
|
* YY = Y^2
|
|
|
|
* RHS = X (X^2 + A) + B = X^3 + A X + B
|
2012-11-17 19:54:20 +01:00
|
|
|
*/
|
2013-10-23 20:19:57 +02:00
|
|
|
MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
|
|
|
|
MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
|
2013-12-06 12:41:30 +01:00
|
|
|
|
|
|
|
/* Special case for A = -3 */
|
|
|
|
if( grp->A.p == NULL )
|
|
|
|
{
|
|
|
|
MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
|
|
|
|
MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
|
|
|
|
|
|
|
|
if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
|
|
|
|
ret = POLARSSL_ERR_ECP_INVALID_KEY;
|
2012-11-17 19:54:20 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
mpi_free( &YY ); mpi_free( &RHS );
|
2012-11-17 19:54:20 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2013-12-05 10:06:06 +01:00
|
|
|
#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(POLARSSL_ECP_MONTGOMERY)
|
|
|
|
/*
|
|
|
|
* Check validity of a public key for Montgomery curves with x-only schemes
|
|
|
|
*/
|
|
|
|
static int ecp_check_pubkey_mx( const ecp_group *grp, const ecp_point *pt )
|
|
|
|
{
|
|
|
|
/* [M255 p. 5] Just check X is the correct number of bytes */
|
|
|
|
if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
|
|
|
|
return( POLARSSL_ERR_ECP_INVALID_KEY );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* POLARSSL_ECP_MONTGOMERY */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that a point is valid as a public key
|
|
|
|
*/
|
|
|
|
int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
|
|
|
|
{
|
|
|
|
/* Must use affine coordinates */
|
|
|
|
if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
|
|
|
|
return( POLARSSL_ERR_ECP_INVALID_KEY );
|
|
|
|
|
|
|
|
#if defined(POLARSSL_ECP_MONTGOMERY)
|
|
|
|
if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
|
|
|
|
return( ecp_check_pubkey_mx( grp, pt ) );
|
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
|
|
|
|
if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
|
|
|
|
return( ecp_check_pubkey_sw( grp, pt ) );
|
|
|
|
#endif
|
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
|
|
|
}
|
2012-11-17 19:54:20 +01:00
|
|
|
|
2012-11-21 10:00:45 +01:00
|
|
|
/*
|
2013-12-04 11:08:01 +01:00
|
|
|
* Check that an mpi is valid as a private key
|
2012-11-21 10:00:45 +01:00
|
|
|
*/
|
2013-10-23 20:19:57 +02:00
|
|
|
int ecp_check_privkey( const ecp_group *grp, const mpi *d )
|
2012-11-21 10:00:45 +01:00
|
|
|
{
|
2013-12-05 10:06:06 +01:00
|
|
|
#if defined(POLARSSL_ECP_MONTGOMERY)
|
2013-12-04 23:15:46 +01:00
|
|
|
if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
|
2013-12-04 11:08:01 +01:00
|
|
|
{
|
2013-12-04 18:14:55 +01:00
|
|
|
/* see [M255] page 5 */
|
2013-12-04 11:08:01 +01:00
|
|
|
if( mpi_get_bit( d, 0 ) != 0 ||
|
|
|
|
mpi_get_bit( d, 1 ) != 0 ||
|
|
|
|
mpi_get_bit( d, 2 ) != 0 ||
|
|
|
|
mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
|
|
|
|
return( POLARSSL_ERR_ECP_INVALID_KEY );
|
2013-12-05 10:06:06 +01:00
|
|
|
else
|
|
|
|
return( 0 );
|
2013-12-04 11:08:01 +01:00
|
|
|
}
|
2013-12-05 10:06:06 +01:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
|
|
|
|
if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
|
2013-12-04 11:08:01 +01:00
|
|
|
{
|
|
|
|
/* see SEC1 3.2 */
|
|
|
|
if( mpi_cmp_int( d, 1 ) < 0 ||
|
|
|
|
mpi_cmp_mpi( d, &grp->N ) >= 0 )
|
|
|
|
return( POLARSSL_ERR_ECP_INVALID_KEY );
|
2013-12-05 10:06:06 +01:00
|
|
|
else
|
|
|
|
return( 0 );
|
2013-12-04 11:08:01 +01:00
|
|
|
}
|
2013-12-05 10:06:06 +01:00
|
|
|
#endif
|
2012-11-21 10:00:45 +01:00
|
|
|
|
2013-12-05 10:06:06 +01:00
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
2012-11-21 10:00:45 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
/*
|
2013-12-04 18:14:55 +01:00
|
|
|
* Generate a keypair
|
2013-10-23 20:19:57 +02:00
|
|
|
*/
|
|
|
|
int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
|
|
|
{
|
|
|
|
size_t n_size = (grp->nbits + 7) / 8;
|
2012-11-21 16:00:55 +01:00
|
|
|
|
2013-12-05 10:06:06 +01:00
|
|
|
#if defined(POLARSSL_ECP_MONTGOMERY)
|
2013-12-04 23:15:46 +01:00
|
|
|
if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
|
2013-10-23 20:19:57 +02:00
|
|
|
{
|
2013-12-04 18:14:55 +01:00
|
|
|
/* [M225] page 5 */
|
|
|
|
size_t b;
|
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
mpi_fill_random( d, n_size, f_rng, p_rng );
|
2012-11-21 10:00:45 +01:00
|
|
|
|
2013-12-04 18:14:55 +01:00
|
|
|
/* Make sure the most significant bit is nbits */
|
|
|
|
b = mpi_msb( d ) - 1; /* mpi_msb is one-based */
|
|
|
|
if( b > grp->nbits )
|
|
|
|
mpi_shift_r( d, b - grp->nbits );
|
|
|
|
else
|
|
|
|
mpi_set_bit( d, grp->nbits, 1 );
|
2012-11-21 10:00:45 +01:00
|
|
|
|
2013-12-04 18:14:55 +01:00
|
|
|
/* Make sure the last three bits are unset */
|
|
|
|
mpi_set_bit( d, 0, 0 );
|
|
|
|
mpi_set_bit( d, 1, 0 );
|
|
|
|
mpi_set_bit( d, 2, 0 );
|
|
|
|
}
|
|
|
|
else
|
2013-12-05 10:06:06 +01:00
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
|
|
|
|
if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
|
2013-12-04 18:14:55 +01:00
|
|
|
{
|
|
|
|
/* SEC1 3.2.1: Generate d such that 1 <= n < N */
|
2013-12-05 10:06:06 +01:00
|
|
|
int count = 0;
|
2013-12-04 18:14:55 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
mpi_fill_random( d, n_size, f_rng, p_rng );
|
|
|
|
|
|
|
|
while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
|
|
|
|
mpi_shift_r( d, 1 );
|
|
|
|
|
|
|
|
if( count++ > 10 )
|
|
|
|
return( POLARSSL_ERR_ECP_RANDOM_FAILED );
|
|
|
|
}
|
|
|
|
while( mpi_cmp_int( d, 1 ) < 0 );
|
2013-10-23 20:19:57 +02:00
|
|
|
}
|
2013-12-05 10:06:06 +01:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
2012-11-21 10:00:45 +01:00
|
|
|
|
2013-10-23 20:19:57 +02:00
|
|
|
return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
|
|
|
|
}
|
|
|
|
|
2013-11-30 14:13:16 +01:00
|
|
|
/*
|
|
|
|
* Generate a keypair, prettier wrapper
|
|
|
|
*/
|
|
|
|
int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
|
|
|
|
}
|
|
|
|
|
2012-10-31 09:26:55 +01:00
|
|
|
#if defined(POLARSSL_SELF_TEST)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
|
|
|
int ecp_self_test( int verbose )
|
|
|
|
{
|
2012-11-13 20:57:00 +01:00
|
|
|
int ret;
|
|
|
|
size_t i;
|
|
|
|
ecp_group grp;
|
2013-09-17 19:13:10 +02:00
|
|
|
ecp_point R, P;
|
2012-11-13 20:57:00 +01:00
|
|
|
mpi m;
|
2013-11-21 20:23:55 +01:00
|
|
|
unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
|
2013-10-10 15:40:49 +02:00
|
|
|
/* exponents especially adapted for secp192r1 */
|
2013-06-25 16:25:17 +02:00
|
|
|
const char *exponents[] =
|
2012-11-13 20:57:00 +01:00
|
|
|
{
|
2012-11-21 13:00:58 +01:00
|
|
|
"000000000000000000000000000000000000000000000001", /* one */
|
2013-11-21 09:28:03 +01:00
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
|
2012-11-21 13:00:58 +01:00
|
|
|
"5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
|
2013-11-21 09:28:03 +01:00
|
|
|
"400000000000000000000000000000000000000000000000", /* one and zeros */
|
|
|
|
"7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
|
|
|
|
"555555555555555555555555555555555555555555555555", /* 101010... */
|
2012-11-13 20:57:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
ecp_group_init( &grp );
|
|
|
|
ecp_point_init( &R );
|
2013-09-17 19:13:10 +02:00
|
|
|
ecp_point_init( &P );
|
2012-11-13 20:57:00 +01:00
|
|
|
mpi_init( &m );
|
|
|
|
|
2013-10-10 15:40:49 +02:00
|
|
|
/* Use secp192r1 if available, or any available curve */
|
2013-06-29 23:26:34 +02:00
|
|
|
#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
|
2012-11-13 20:57:00 +01:00
|
|
|
MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
|
2013-06-29 23:26:34 +02:00
|
|
|
#else
|
2013-10-10 15:40:49 +02:00
|
|
|
MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
|
|
|
|
#endif
|
2012-11-13 20:57:00 +01:00
|
|
|
|
|
|
|
if( verbose != 0 )
|
2013-09-17 19:13:10 +02:00
|
|
|
printf( " ECP test #1 (constant op_count, base point G): " );
|
|
|
|
|
|
|
|
/* Do a dummy multiplication first to trigger precomputation */
|
|
|
|
MPI_CHK( mpi_lset( &m, 2 ) );
|
|
|
|
MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
|
|
|
add_count = 0;
|
|
|
|
dbl_count = 0;
|
2013-11-21 20:23:55 +01:00
|
|
|
mul_count = 0;
|
2012-11-13 20:57:00 +01:00
|
|
|
MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
|
2013-09-02 14:29:09 +02:00
|
|
|
MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
|
|
|
for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
|
|
|
|
{
|
|
|
|
add_c_prev = add_count;
|
|
|
|
dbl_c_prev = dbl_count;
|
2013-11-21 20:23:55 +01:00
|
|
|
mul_c_prev = mul_count;
|
2012-11-13 20:57:00 +01:00
|
|
|
add_count = 0;
|
|
|
|
dbl_count = 0;
|
2013-11-21 20:23:55 +01:00
|
|
|
mul_count = 0;
|
2012-11-13 20:57:00 +01:00
|
|
|
|
|
|
|
MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
|
2013-09-02 14:29:09 +02:00
|
|
|
MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
2013-11-21 20:23:55 +01:00
|
|
|
if( add_count != add_c_prev ||
|
|
|
|
dbl_count != dbl_c_prev ||
|
|
|
|
mul_count != mul_c_prev )
|
2012-11-13 20:57:00 +01:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2013-12-30 19:04:47 +01:00
|
|
|
printf( "failed (%u)\n", (unsigned int) i );
|
2012-11-13 20:57:00 +01:00
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
|
|
|
printf( "passed\n" );
|
|
|
|
|
2013-09-17 19:13:10 +02:00
|
|
|
if( verbose != 0 )
|
|
|
|
printf( " ECP test #2 (constant op_count, other point): " );
|
|
|
|
/* We computed P = 2G last time, use it */
|
|
|
|
|
|
|
|
add_count = 0;
|
|
|
|
dbl_count = 0;
|
2013-11-21 20:23:55 +01:00
|
|
|
mul_count = 0;
|
2013-09-17 19:13:10 +02:00
|
|
|
MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
|
|
|
|
MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
|
|
|
|
|
|
|
|
for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
|
|
|
|
{
|
|
|
|
add_c_prev = add_count;
|
|
|
|
dbl_c_prev = dbl_count;
|
2013-11-21 20:23:55 +01:00
|
|
|
mul_c_prev = mul_count;
|
2013-09-17 19:13:10 +02:00
|
|
|
add_count = 0;
|
|
|
|
dbl_count = 0;
|
2013-11-21 20:23:55 +01:00
|
|
|
mul_count = 0;
|
2013-09-17 19:13:10 +02:00
|
|
|
|
|
|
|
MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
|
|
|
|
MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
|
|
|
|
|
2013-11-21 20:23:55 +01:00
|
|
|
if( add_count != add_c_prev ||
|
|
|
|
dbl_count != dbl_c_prev ||
|
|
|
|
mul_count != mul_c_prev )
|
2013-09-17 19:13:10 +02:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2013-12-30 19:04:47 +01:00
|
|
|
printf( "failed (%u)\n", (unsigned int) i );
|
2013-09-17 19:13:10 +02:00
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
|
|
|
printf( "passed\n" );
|
|
|
|
|
2012-11-13 20:57:00 +01:00
|
|
|
cleanup:
|
|
|
|
|
|
|
|
if( ret < 0 && verbose != 0 )
|
|
|
|
printf( "Unexpected error, return code = %08X\n", ret );
|
|
|
|
|
|
|
|
ecp_group_free( &grp );
|
|
|
|
ecp_point_free( &R );
|
2013-09-17 19:13:10 +02:00
|
|
|
ecp_point_free( &P );
|
2012-11-13 20:57:00 +01:00
|
|
|
mpi_free( &m );
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
|
|
|
printf( "\n" );
|
|
|
|
|
|
|
|
return( ret );
|
2012-10-31 09:26:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|