1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-07-29 11:41:15 +03:00

Add mpi_safe_cond_assign()

This commit is contained in:
Manuel Pégourié-Gonnard
2013-11-21 16:56:39 +01:00
parent 44aab79022
commit 71c2c21601
4 changed files with 73 additions and 0 deletions

View File

@ -204,6 +204,31 @@ void mpi_swap( mpi *X, mpi *Y )
memcpy( Y, &T, sizeof( mpi ) );
}
/*
* Conditionally assign X = Y, without leaking information
*/
int mpi_safe_cond_assign( mpi *X, mpi *Y, unsigned char assign )
{
int ret = 0;
size_t i;
if( assign * ( 1 - assign ) != 0 )
return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
/* Make sure both MPIs have the same size */
if( X->n > Y->n )
MPI_CHK( mpi_grow( Y, X->n ) );
if( Y->n > X->n )
MPI_CHK( mpi_grow( X, Y->n ) );
/* Do the conditional assign safely */
for( i = 0; i < X->n; i++ )
X->p[i] = X->p[i] * (1 - assign) + Y->p[i] * assign;
cleanup:
return( ret );
}
/*
* Set value from integer
*/