Commit 287781a9 authored by Paul Bakker's avatar Paul Bakker
Browse files

- Added mpi_fill_random() for centralized filling of big numbers with random...

 -  Added mpi_fill_random() for centralized filling of big numbers with random data (Fixed ticket #10)
parent 98675496
......@@ -6,6 +6,8 @@ Features
for the RSAES-OAEP and RSASSA-PSS operations.
* Reading of Public Key files incorporated into default x509
functionality as well.
* Added mpi_fill_random() for centralized filling of big numbers
with random data (Fixed ticket #10)
Changes
* Debug print of MPI now removes leading zero octets and
......
......@@ -466,6 +466,19 @@ int mpi_mod_int( t_int *r, const mpi *A, int b );
*/
int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
/**
* \brief Fill an MPI X with size bytes of random
*
* \param X Destination MPI
* \param size Size in bytes
* \param f_rng RNG function
* \param p_rng RNG parameter
*
* \return 0 if successful,
* 1 if memory allocation failed
*/
int mpi_fill_random( mpi *X, int size, int (*f_rng)(void *), void *p_rng );
/**
* \brief Greatest common divisor: G = gcd(A, B)
*
......
......@@ -1559,6 +1559,22 @@ cleanup:
return( ret );
}
int mpi_fill_random( mpi *X, int size, int (*f_rng)(void *), void *p_rng )
{
int ret, k;
unsigned char *p;
MPI_CHK( mpi_grow( X, size ) );
MPI_CHK( mpi_lset( X, 0 ) );
p = (unsigned char *) X->p;
for( k = 0; k < X->n * ciL; k++ )
*p++ = (unsigned char) f_rng( p_rng );
cleanup:
return( ret );
}
#if defined(POLARSSL_GENPRIME)
/*
......@@ -1686,7 +1702,6 @@ int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng )
{
int ret, i, j, n, s, xs;
mpi W, R, T, A, RR;
unsigned char *p;
if( mpi_cmp_int( X, 0 ) == 0 ||
mpi_cmp_int( X, 1 ) == 0 )
......@@ -1740,11 +1755,7 @@ int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng )
/*
* pick a random A, 1 < A < |X| - 1
*/
MPI_CHK( mpi_grow( &A, X->n ) );
p = (unsigned char *) A.p;
for( j = 0; j < A.n * ciL; j++ )
*p++ = (unsigned char) f_rng( p_rng );
mpi_fill_random( &A, X->n, f_rng, p_rng );
if( mpi_cmp_mpi( &A, &W ) >= 0 )
{
......@@ -1804,7 +1815,6 @@ int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
int (*f_rng)(void *), void *p_rng )
{
int ret, k, n;
unsigned char *p;
mpi Y;
if( nbits < 3 )
......@@ -1814,12 +1824,7 @@ int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
n = BITS_TO_LIMBS( nbits );
MPI_CHK( mpi_grow( X, n ) );
MPI_CHK( mpi_lset( X, 0 ) );
p = (unsigned char *) X->p;
for( k = 0; k < X->n * ciL; k++ )
*p++ = (unsigned char) f_rng( p_rng );
mpi_fill_random( X, n, f_rng, p_rng );
k = mpi_msb( X );
if( k < nbits ) MPI_CHK( mpi_shift_l( X, nbits - k ) );
......
......@@ -138,12 +138,8 @@ int dhm_make_params( dhm_context *ctx, int x_size,
* Generate X as large as possible ( < P )
*/
n = x_size / sizeof( t_int ) + 1;
MPI_CHK( mpi_grow( &ctx->X, n ) );
MPI_CHK( mpi_lset( &ctx->X, 0 ) );
p = (unsigned char *) ctx->X.p;
for( i = 0; i < x_size; i++ )
*p++ = (unsigned char) f_rng( p_rng );
mpi_fill_random( &ctx->X, n, f_rng, p_rng );
while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
mpi_shift_r( &ctx->X, 1 );
......@@ -220,12 +216,8 @@ int dhm_make_public( dhm_context *ctx, int x_size,
* generate X and calculate GX = G^X mod P
*/
n = x_size / sizeof( t_int ) + 1;
MPI_CHK( mpi_grow( &ctx->X, n ) );
MPI_CHK( mpi_lset( &ctx->X, 0 ) );
p = (unsigned char *) ctx->X.p;
for( i = 0; i < x_size; i++ )
*p++ = (unsigned char) f_rng( p_rng );
mpi_fill_random( &ctx->X, n, f_rng, p_rng );
while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
mpi_shift_r( &ctx->X, 1 );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment