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

Add mpi_shrink()

This commit is contained in:
Manuel Pégourié-Gonnard
2013-11-21 10:39:37 +01:00
parent e282012219
commit 5868163e07
4 changed files with 90 additions and 0 deletions

View File

@ -119,6 +119,45 @@ int mpi_grow( mpi *X, size_t nblimbs )
return( 0 );
}
/*
* Resize down as much as possible,
* while keeping at least the specified number of limbs
*/
int mpi_shrink( mpi *X, size_t nblimbs )
{
t_uint *p;
size_t i;
/* Actually resize up in this case */
if( X->n <= nblimbs )
return( mpi_grow( X, nblimbs ) );
for( i = X->n - 1; i > 0; i-- )
if( X->p[i] != 0 )
break;
i++;
if( i < nblimbs )
i = nblimbs;
if( ( p = (t_uint *) polarssl_malloc( i * ciL ) ) == NULL )
return( POLARSSL_ERR_MPI_MALLOC_FAILED );
memset( p, 0, i * ciL );
if( X->p != NULL )
{
memcpy( p, X->p, i * ciL );
memset( X->p, 0, X->n * ciL );
polarssl_free( X->p );
}
X->n = i;
X->p = p;
return( 0 );
}
/*
* Copy the contents of Y into X
*/