diff --git a/library/ecp.c b/library/ecp.c index 0e4be10ba..8e162e7e2 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -527,7 +527,6 @@ int ecp_group_read_string( ecp_group *grp, int radix, int ret; MPI_CHK( mpi_read_string( &grp->P, radix, p ) ); - MPI_CHK( mpi_add_int( &grp->A, &grp->P, -3 ) ); 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 ) ); @@ -868,7 +867,17 @@ static int ecp_double_jac( const ecp_group *grp, ecp_point *R, 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 ); - MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 ); + + /* 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 ); + 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 ); @@ -1633,7 +1642,17 @@ static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt ) */ 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 ); - MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS ); + + /* 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 ); + } + MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS ); MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS ); diff --git a/library/ecp_curves.c b/library/ecp_curves.c index f7b5eb94e..f29b68792 100644 --- a/library/ecp_curves.c +++ b/library/ecp_curves.c @@ -450,6 +450,17 @@ static inline void ecp_mpi_load( mpi *X, const t_uint *p, size_t len ) X->p = (t_uint *) p; } +/* + * Set an MPI to static value 1 + */ +static inline void ecp_mpi_set1( mpi *X ) +{ + static t_uint one[] = { 1 }; + X->s = 1; + X->n = 1; + X->p = one; +} + /* * Make group available from embedded constants */ @@ -461,28 +472,20 @@ static int ecp_group_load( ecp_group *grp, const t_uint *gy, size_t gylen, const t_uint *n, size_t nlen) { - int ret; - ecp_mpi_load( &grp->P, p, plen ); if( a != NULL ) ecp_mpi_load( &grp->A, a, alen ); - else - MPI_CHK( mpi_sub_int( &grp->A, &grp->P, 3 ) ); ecp_mpi_load( &grp->B, b, blen ); ecp_mpi_load( &grp->N, n, nlen ); ecp_mpi_load( &grp->G.X, gx, gxlen ); ecp_mpi_load( &grp->G.Y, gy, gylen ); - MPI_CHK( mpi_lset( &grp->G.Z, 1 ) ); + ecp_mpi_set1( &grp->G.Z ); grp->pbits = mpi_msb( &grp->P ); grp->nbits = mpi_msb( &grp->N ); -cleanup: - if( ret != 0 ) - ecp_group_free( grp ); - - return( ret ); + return( 0 ); } #if defined(POLARSSL_ECP_NIST_OPTIM)