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

- Added mpi_get_bit() and mpi_set_bit() individual bit setter/getter functions.

This commit is contained in:
Paul Bakker
2011-05-18 15:47:11 +00:00
parent 831a755d9e
commit 2f5947e1f6
5 changed files with 120 additions and 0 deletions

View File

@ -170,6 +170,44 @@ cleanup:
return( ret );
}
/*
* Get a specific bit
*/
int mpi_get_bit( mpi *X, size_t pos )
{
if( X->n * biL <= pos )
return( 0 );
return ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01;
}
/*
* Set a bit to a specific value of 0 or 1
*/
int mpi_set_bit( mpi *X, size_t pos, unsigned char val )
{
int ret = 0;
size_t off = pos / biL;
size_t idx = pos % biL;
if( val != 0 && val != 1 )
return POLARSSL_ERR_MPI_BAD_INPUT_DATA;
if( X->n * biL <= pos )
{
if( val == 0 )
return ( 0 );
MPI_CHK( mpi_grow( X, off + 1 ) );
}
X->p[off] = ( X->p[off] & ~( 0x01 << idx ) ) | ( val << idx );
cleanup:
return( ret );
}
/*
* Return the number of least significant bits
*/