From 287781a965bfc1603c6314632803d303ddbf406a Mon Sep 17 00:00:00 2001
From: Paul Bakker
Date: Sat, 26 Mar 2011 13:18:49 +0000
Subject: [PATCH] - Added mpi_fill_random() for centralized filling of big
numbers with random data (Fixed ticket #10)
---
ChangeLog | 2 ++
include/polarssl/bignum.h | 13 +++++++++++++
library/bignum.c | 31 ++++++++++++++++++-------------
library/dhm.c | 12 ++----------
4 files changed, 35 insertions(+), 23 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index e81cb2ce5..2ea47e304 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/include/polarssl/bignum.h b/include/polarssl/bignum.h
index 79bc354c0..ebc1f8f2f 100644
--- a/include/polarssl/bignum.h
+++ b/include/polarssl/bignum.h
@@ -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)
*
diff --git a/library/bignum.c b/library/bignum.c
index 55188b785..8ae3921db 100644
--- a/library/bignum.c
+++ b/library/bignum.c
@@ -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 ) );
diff --git a/library/dhm.c b/library/dhm.c
index 9b99a2b17..bcad7fcb3 100644
--- a/library/dhm.c
+++ b/library/dhm.c
@@ -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 );