diff --git a/ChangeLog b/ChangeLog index 8c576a6ac..539256b86 100644 --- a/ChangeLog +++ b/ChangeLog @@ -24,6 +24,8 @@ Changes parity bits, to prevent mistakes in copying data. (Closes ticket #33) * Loads of minimal changes to better support WINCE as a build target (Credits go to Marco Lizza) + * Added POLARSSL_MPI_WINDOW_SIZE definition to allow easier time to memory + trade-off Bugfix * Fixed faulty HMAC-MD2 implementation. Found by dibac. (Closes diff --git a/include/polarssl/bignum.h b/include/polarssl/bignum.h index 793840670..1e46d126b 100644 --- a/include/polarssl/bignum.h +++ b/include/polarssl/bignum.h @@ -45,6 +45,17 @@ */ #define POLARSSL_MPI_MAX_LIMBS 10000 +/* + * Maximum window size used for modular exponentiation. Default: 6 + * Minimum value: 1. Maximum value: 6. + * + * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used + * for the sliding window calculation. (So 64 by default) + * + * Reduction in size, reduces speed. + */ +#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */ + /* * Define the base integer type, architecture-wise */ diff --git a/library/bignum.c b/library/bignum.c index c00697eb6..d4035b62b 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1377,7 +1377,7 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) size_t i, j, nblimbs; size_t bufsize, nbits; t_uint ei, mm, state; - mpi RR, T, W[64]; + mpi RR, T, W[ 2 << POLARSSL_MPI_WINDOW_SIZE ]; if( mpi_cmp_int( N, 0 ) < 0 || ( N->p[0] & 1 ) == 0 ) return( POLARSSL_ERR_MPI_BAD_INPUT_DATA ); @@ -1394,6 +1394,9 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 : ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1; + if( wsize > POLARSSL_MPI_WINDOW_SIZE ) + wsize = POLARSSL_MPI_WINDOW_SIZE; + j = N->n + 1; MPI_CHK( mpi_grow( X, j ) ); MPI_CHK( mpi_grow( &W[1], j ) );