Allow R and A to point to same mpi in mpi_div_mpi
(cherry picked from commit f02c5642d0f19281e7c30d849bf8cd94703a9bd5 and
from commit 50546921ac8250d1884c41fd9dc7a645007d4103)
diff --git a/library/bignum.c b/library/bignum.c
index acaba6a..079b58e 100644
--- a/library/bignum.c
+++ b/library/bignum.c
@@ -1196,9 +1196,9 @@
if( R != NULL )
{
mpi_shift_r( &X, k );
+ X.s = A->s;
mpi_copy( R, &X );
- R->s = A->s;
if( mpi_cmp_int( R, 0 ) == 0 )
R->s = 1;
}
@@ -1400,19 +1400,6 @@
return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
/*
- * Compensate for negative A (and correct at the end)
- */
- neg = ( A->s == -1 );
-
- mpi_init( &Apos );
- if( neg )
- {
- MPI_CHK( mpi_copy( &Apos, A ) );
- Apos.s = 1;
- A = &Apos;
- }
-
- /*
* Init temps and window size
*/
mpi_montg_init( &mm, N );
@@ -1433,6 +1420,19 @@
MPI_CHK( mpi_grow( &T, j * 2 ) );
/*
+ * Compensate for negative A (and correct at the end)
+ */
+ neg = ( A->s == -1 );
+
+ mpi_init( &Apos );
+ if( neg )
+ {
+ MPI_CHK( mpi_copy( &Apos, A ) );
+ Apos.s = 1;
+ A = &Apos;
+ }
+
+ /*
* If 1st call, pre-compute R^2 mod N
*/
if( _RR == NULL || _RR->p == NULL )