Improved on the previous fix and added a test case to cover both types

of carries.
This commit is contained in:
Janos Follath 2015-10-30 17:43:11 +01:00 committed by Manuel Pégourié-Gonnard
parent 2b806fad7b
commit 2db440d2f1
2 changed files with 13 additions and 14 deletions

View File

@ -889,22 +889,11 @@ int mpi_add_abs( mpi *X, const mpi *A, const mpi *B )
{ {
int ret; int ret;
size_t i, j; size_t i, j;
mpi_uint *o, *p, c; mpi_uint *o, *p, c, tmp;
mpi TB;
if( X == B ) if( X == B )
{ {
B = A; A = X; const mpi *T = A; A = X; B = T;
if( B == A )
{
// Making a temporary copy instead of shifting by one to deny
// the possibility of corresponding side-channel attacks.
mpi_init( &TB );
MPI_CHK( mpi_copy( &TB, B ) );
B = &TB;
}
} }
if( X != A ) if( X != A )
@ -923,10 +912,14 @@ int mpi_add_abs( mpi *X, const mpi *A, const mpi *B )
o = B->p; p = X->p; c = 0; o = B->p; p = X->p; c = 0;
/*
* tmp is used because it might happen that p == o
*/
for( i = 0; i < j; i++, o++, p++ ) for( i = 0; i < j; i++, o++, p++ )
{ {
tmp= *o;
*p += c; c = ( *p < c ); *p += c; c = ( *p < c );
*p += *o; c += ( *p < *o ); *p += tmp; c += ( *p < tmp );
} }
while( c != 0 ) while( c != 0 )
@ -941,10 +934,13 @@ int mpi_add_abs( mpi *X, const mpi *A, const mpi *B )
} }
cleanup: cleanup:
<<<<<<< HEAD
if( &TB == B ) if( &TB == B )
{ {
mpi_free( &TB ); mpi_free( &TB );
} }
=======
>>>>>>> 6c9226809370... Improved on the previous fix and added a test case to cover both types
return( ret ); return( ret );
} }

View File

@ -301,6 +301,9 @@ mpi_add_mpi_inplace:10:"12345678":10:"24691356"
Test mpi_add_mpi inplace #2 Test mpi_add_mpi inplace #2
mpi_add_mpi_inplace:10:"643808006803554439230129854961492699151386107534013432918073439524138264842370630061369715394739134090922937332590384720397133335969549256322620979036686633213903952966175107096769180017646161851573147596390153":10:"1287616013607108878460259709922985398302772215068026865836146879048276529684741260122739430789478268181845874665180769440794266671939098512645241958073373266427807905932350214193538360035292323703146295192780306" mpi_add_mpi_inplace:10:"643808006803554439230129854961492699151386107534013432918073439524138264842370630061369715394739134090922937332590384720397133335969549256322620979036686633213903952966175107096769180017646161851573147596390153":10:"1287616013607108878460259709922985398302772215068026865836146879048276529684741260122739430789478268181845874665180769440794266671939098512645241958073373266427807905932350214193538360035292323703146295192780306"
Test mpi_add_mpi inplace #3
mpi_add_mpi_inplace:16:"ffffffffffffffffffffffffffffffff":16:"01fffffffffffffffffffffffffffffffe"
Test mpi_add_int #1 Test mpi_add_int #1
mpi_add_int:10:"2039568783564019774057658669290345772801939933143482630947726464532830627227012776329":9871232:10:"2039568783564019774057658669290345772801939933143482630947726464532830627227022647561" mpi_add_int:10:"2039568783564019774057658669290345772801939933143482630947726464532830627227012776329":9871232:10:"2039568783564019774057658669290345772801939933143482630947726464532830627227022647561"