Commit 6c591fab authored by Paul Bakker's avatar Paul Bakker
Browse files

- mpi_init() and mpi_free() only accept a single argument and do not accept...

 - mpi_init() and mpi_free() only accept a single argument and do not accept variable arguments anymore. This prevents unexpected memory corruption in a number of use cases.
parent f968857a
......@@ -13,6 +13,8 @@ Changes
instead of int for buffer lengths and loop variables for
better unsigned / signed use. Renamed internal bigint types
t_int and t_dbl to t_uint and t_udbl in the process
* mpi_init() and mpi_free() now only accept a single MPI
argument and do not accept variable argument lists anymore.
= Version 0.99-pre4 released on 2011-04-01
Features
......
......@@ -92,14 +92,18 @@ extern "C" {
#endif
/**
* \brief Initialize one or more mpi
* \brief Initialize one MPI
*
* \param X One MPI to initialize.
*/
void mpi_init( mpi *X, ... );
void mpi_init( mpi *X );
/**
* \brief Unallocate one or more mpi
* \brief Unallocate one MPI
*
* \param X One MPI to unallocate.
*/
void mpi_free( mpi *X, ... );
void mpi_free( mpi *X );
/**
* \brief Enlarge to the specified number of limbs
......
......@@ -38,7 +38,6 @@
#include "polarssl/bn_mul.h"
#include <stdlib.h>
#include <stdarg.h>
#define ciL (sizeof(t_uint)) /* chars in limb */
#define biL (ciL << 3) /* bits in limb */
......@@ -51,51 +50,35 @@
#define CHARS_TO_LIMBS(i) (((i) + ciL - 1) / ciL)
/*
* Initialize one or more mpi
* Initialize one MPI
*/
void mpi_init( mpi *X, ... )
void mpi_init( mpi *X )
{
va_list args;
if( X == NULL )
return;
va_start( args, X );
while( X != NULL )
{
X->s = 1;
X->n = 0;
X->p = NULL;
X = va_arg( args, mpi* );
}
va_end( args );
X->s = 1;
X->n = 0;
X->p = NULL;
}
/*
* Unallocate one or more mpi
* Unallocate one MPI
*/
void mpi_free( mpi *X, ... )
void mpi_free( mpi *X )
{
va_list args;
if( X == NULL )
return;
va_start( args, X );
while( X != NULL )
if( X->p != NULL )
{
if( X->p != NULL )
{
memset( X->p, 0, X->n * ciL );
free( X->p );
}
X->s = 1;
X->n = 0;
X->p = NULL;
X = va_arg( args, mpi* );
memset( X->p, 0, X->n * ciL );
free( X->p );
}
va_end( args );
X->s = 1;
X->n = 0;
X->p = NULL;
}
/*
......@@ -258,7 +241,7 @@ int mpi_read_string( mpi *X, int radix, const char *s )
if( radix < 2 || radix > 16 )
return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
mpi_init( &T, NULL );
mpi_init( &T );
slen = strlen( s );
......@@ -309,7 +292,7 @@ int mpi_read_string( mpi *X, int radix, const char *s )
cleanup:
mpi_free( &T, NULL );
mpi_free( &T );
return( ret );
}
......@@ -366,7 +349,7 @@ int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen )
}
p = s;
mpi_init( &T, NULL );
mpi_init( &T );
if( X->s == -1 )
*p++ = '-';
......@@ -405,7 +388,7 @@ int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen )
cleanup:
mpi_free( &T, NULL );
mpi_free( &T );
return( ret );
}
......@@ -771,7 +754,7 @@ int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B )
if( mpi_cmp_abs( A, B ) < 0 )
return( POLARSSL_ERR_MPI_NEGATIVE_VALUE );
mpi_init( &TB, NULL );
mpi_init( &TB );
if( X == B )
{
......@@ -797,7 +780,7 @@ int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B )
cleanup:
mpi_free( &TB, NULL );
mpi_free( &TB );
return( ret );
}
......@@ -969,7 +952,7 @@ int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B )
size_t i, j;
mpi TA, TB;
mpi_init( &TA, &TB, NULL );
mpi_init( &TA ); mpi_init( &TB );
if( X == A ) { MPI_CHK( mpi_copy( &TA, A ) ); A = &TA; }
if( X == B ) { MPI_CHK( mpi_copy( &TB, B ) ); B = &TB; }
......@@ -992,7 +975,7 @@ int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B )
cleanup:
mpi_free( &TB, &TA, NULL );
mpi_free( &TB ); mpi_free( &TA );
return( ret );
}
......@@ -1025,7 +1008,8 @@ int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B )
if( mpi_cmp_int( B, 0 ) == 0 )
return( POLARSSL_ERR_MPI_DIVISION_BY_ZERO );
mpi_init( &X, &Y, &Z, &T1, &T2, NULL );
mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
mpi_init( &T1 ); mpi_init( &T2 );
if( mpi_cmp_abs( A, B ) < 0 )
{
......@@ -1170,7 +1154,8 @@ int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B )
cleanup:
mpi_free( &X, &Y, &Z, &T1, &T2, NULL );
mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
mpi_free( &T1 ); mpi_free( &T2 );
return( ret );
}
......@@ -1363,7 +1348,7 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR )
* Init temps and window size
*/
mpi_montg_init( &mm, N );
mpi_init( &RR, &T, NULL );
mpi_init( &RR ); mpi_init( &T );
memset( W, 0, sizeof( W ) );
i = mpi_msb( E );
......@@ -1514,11 +1499,12 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR )
cleanup:
for( i = (one << (wsize - 1)); i < (one << wsize); i++ )
mpi_free( &W[i], NULL );
mpi_free( &W[i] );
mpi_free( &W[1] ); mpi_free( &T );
if( _RR != NULL )
mpi_free( &W[1], &T, NULL );
else mpi_free( &W[1], &T, &RR, NULL );
if( _RR == NULL )
mpi_free( &RR );
return( ret );
}
......@@ -1532,7 +1518,7 @@ int mpi_gcd( mpi *G, const mpi *A, const mpi *B )
size_t lz, lzt;
mpi TG, TA, TB;
mpi_init( &TG, &TA, &TB, NULL );
mpi_init( &TG ); mpi_init( &TA ); mpi_init( &TB );
MPI_CHK( mpi_copy( &TA, A ) );
MPI_CHK( mpi_copy( &TB, B ) );
......@@ -1570,7 +1556,7 @@ int mpi_gcd( mpi *G, const mpi *A, const mpi *B )
cleanup:
mpi_free( &TB, &TA, &TG, NULL );
mpi_free( &TG ); mpi_free( &TA ); mpi_free( &TB );
return( ret );
}
......@@ -1605,8 +1591,9 @@ int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N )
if( mpi_cmp_int( N, 0 ) <= 0 )
return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
mpi_init( &TA, &TU, &U1, &U2, &G,
&TB, &TV, &V1, &V2, NULL );
mpi_init( &TA ); mpi_init( &TU ); mpi_init( &U1 ); mpi_init( &U2 );
mpi_init( &G ); mpi_init( &TB ); mpi_init( &TV );
mpi_init( &V1 ); mpi_init( &V2 );
MPI_CHK( mpi_gcd( &G, A, N ) );
......@@ -1681,8 +1668,9 @@ int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N )
cleanup:
mpi_free( &V2, &V1, &TV, &TB, &G,
&U2, &U1, &TU, &TA, NULL );
mpi_free( &TA ); mpi_free( &TU ); mpi_free( &U1 ); mpi_free( &U2 );
mpi_free( &G ); mpi_free( &TB ); mpi_free( &TV );
mpi_free( &V1 ); mpi_free( &V2 );
return( ret );
}
......@@ -1728,7 +1716,8 @@ int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng )
if( mpi_cmp_int( X, 2 ) == 0 )
return( 0 );
mpi_init( &W, &R, &T, &A, &RR, NULL );
mpi_init( &W ); mpi_init( &R ); mpi_init( &T ); mpi_init( &A );
mpi_init( &RR );
xs = X->s; X->s = 1;
......@@ -1821,7 +1810,8 @@ cleanup:
X->s = xs;
mpi_free( &RR, &A, &T, &R, &W, NULL );
mpi_free( &W ); mpi_free( &R ); mpi_free( &T ); mpi_free( &A );
mpi_free( &RR );
return( ret );
}
......@@ -1839,7 +1829,7 @@ int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
if( nbits < 3 || nbits > 4096 )
return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
mpi_init( &Y, NULL );
mpi_init( &Y );
n = BITS_TO_LIMBS( nbits );
......@@ -1888,7 +1878,7 @@ int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
cleanup:
mpi_free( &Y, NULL );
mpi_free( &Y );
return( ret );
}
......@@ -1914,7 +1904,8 @@ int mpi_self_test( int verbose )
int ret, i;
mpi A, E, N, X, Y, U, V;
mpi_init( &A, &E, &N, &X, &Y, &U, &V, NULL );
mpi_init( &A ); mpi_init( &E ); mpi_init( &N ); mpi_init( &X );
mpi_init( &Y ); mpi_init( &U ); mpi_init( &V );
MPI_CHK( mpi_read_string( &A, 16,
"EFE021C2645FD1DC586E69184AF4A31E" \
......@@ -2052,7 +2043,8 @@ cleanup:
if( ret != 0 && verbose != 0 )
printf( "Unexpected error, return code = %08X\n", ret );
mpi_free( &V, &U, &Y, &X, &N, &E, &A, NULL );
mpi_free( &A ); mpi_free( &E ); mpi_free( &N ); mpi_free( &X );
mpi_free( &Y ); mpi_free( &U ); mpi_free( &V );
if( verbose != 0 )
printf( "\n" );
......
......@@ -74,7 +74,7 @@ static int dhm_check_range( const mpi *public_param, const mpi *P )
mpi L, U;
int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
mpi_init( &L, &U, NULL );
mpi_init( &L ); mpi_init( &U );
mpi_lset( &L, 2 );
mpi_sub_int( &U, P, 2 );
......@@ -84,7 +84,7 @@ static int dhm_check_range( const mpi *public_param, const mpi *P )
ret = 0;
}
mpi_free( &L, &U, NULL );
mpi_free( &L ); mpi_free( &U );
return( ret );
}
......@@ -270,9 +270,9 @@ cleanup:
*/
void dhm_free( dhm_context *ctx )
{
mpi_free( &ctx->RP, &ctx->K, &ctx->GY,
&ctx->GX, &ctx->X, &ctx->G,
&ctx->P, NULL );
mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
mpi_free( &ctx->P );
}
#if defined(POLARSSL_SELF_TEST)
......
......@@ -68,7 +68,7 @@ int rsa_gen_key( rsa_context *ctx,
if( f_rng == NULL || nbits < 128 || exponent < 3 )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
mpi_init( &P1, &Q1, &H, &G, NULL );
mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
/*
* find primes P and Q with Q < P so that:
......@@ -116,7 +116,7 @@ int rsa_gen_key( rsa_context *ctx,
cleanup:
mpi_free( &G, &H, &Q1, &P1, NULL );
mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
if( ret != 0 )
{
......@@ -166,7 +166,9 @@ int rsa_check_privkey( const rsa_context *ctx )
if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
mpi_init( &PQ, &DE, &P1, &Q1, &H, &I, &G, &G2, &L1, &L2, NULL );
mpi_init( &PQ ); mpi_init( &DE ); mpi_init( &P1 ); mpi_init( &Q1 );
mpi_init( &H ); mpi_init( &I ); mpi_init( &G ); mpi_init( &G2 );
mpi_init( &L1 ); mpi_init( &L2 );
MPI_CHK( mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
MPI_CHK( mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
......@@ -182,20 +184,25 @@ int rsa_check_privkey( const rsa_context *ctx )
/*
* Check for a valid PKCS1v2 private key
*/
if( mpi_cmp_mpi( &PQ, &ctx->N ) == 0 &&
mpi_cmp_int( &L2, 0 ) == 0 &&
mpi_cmp_int( &I, 1 ) == 0 &&
mpi_cmp_int( &G, 1 ) == 0 )
if( mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
mpi_cmp_int( &L2, 0 ) != 0 ||
mpi_cmp_int( &I, 1 ) != 0 ||
mpi_cmp_int( &G, 1 ) != 0 )
{
mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, &G2, &L1, &L2, NULL );
return( 0 );
ret = POLARSSL_ERR_RSA_KEY_CHECK_FAILED;
}
cleanup:
mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, &G2, &L1, &L2, NULL );
return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED | ret );
mpi_free( &PQ ); mpi_free( &DE ); mpi_free( &P1 ); mpi_free( &Q1 );
mpi_free( &H ); mpi_free( &I ); mpi_free( &G ); mpi_free( &G2 );
mpi_free( &L1 ); mpi_free( &L2 );
if( ret != 0 )
return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED | ret );
return( 0 );
}
/*
......@@ -209,13 +216,13 @@ int rsa_public( rsa_context *ctx,
size_t olen;
mpi T;
mpi_init( &T, NULL );
mpi_init( &T );
MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
{
mpi_free( &T, NULL );
mpi_free( &T );
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
}
......@@ -225,7 +232,7 @@ int rsa_public( rsa_context *ctx,
cleanup:
mpi_free( &T, NULL );
mpi_free( &T );
if( ret != 0 )
return( POLARSSL_ERR_RSA_PUBLIC_FAILED | ret );
......@@ -244,13 +251,13 @@ int rsa_private( rsa_context *ctx,
size_t olen;
mpi T, T1, T2;
mpi_init( &T, &T1, &T2, NULL );
mpi_init( &T ); mpi_init( &T1 ); mpi_init( &T2 );
MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
{
mpi_free( &T, NULL );
mpi_free( &T );
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
}
......@@ -285,7 +292,7 @@ int rsa_private( rsa_context *ctx,
cleanup:
mpi_free( &T, &T1, &T2, NULL );
mpi_free( &T ); mpi_free( &T1 ); mpi_free( &T2 );
if( ret != 0 )
return( POLARSSL_ERR_RSA_PRIVATE_FAILED | ret );
......@@ -1006,10 +1013,10 @@ int rsa_pkcs1_verify( rsa_context *ctx,
*/
void rsa_free( rsa_context *ctx )
{
mpi_free( &ctx->RQ, &ctx->RP, &ctx->RN,
&ctx->QP, &ctx->DQ, &ctx->DP,
&ctx->Q, &ctx->P, &ctx->D,
&ctx->E, &ctx->N, NULL );
mpi_free( &ctx->RQ ); mpi_free( &ctx->RP ); mpi_free( &ctx->RN );
mpi_free( &ctx->QP ); mpi_free( &ctx->DQ ); mpi_free( &ctx->DP );
mpi_free( &ctx->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D );
mpi_free( &ctx->E ); mpi_free( &ctx->N );
}
#if defined(POLARSSL_SELF_TEST)
......
......@@ -49,7 +49,7 @@ int main( void )
havege_state hs;
FILE *fout;
mpi_init( &G, &P, &Q, NULL );
mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
mpi_read_string( &G, 10, GENERATOR );
printf( "\n . Seeding the random number generator..." );
......@@ -113,7 +113,7 @@ int main( void )
exit:
mpi_free( &Q, &P, &G, NULL );
mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
#else
printf( "\n ! Prime-number generation is not available.\n\n" );
#endif
......
......@@ -35,8 +35,9 @@ int main( void )
{
mpi E, P, Q, N, H, D, X, Y, Z;
mpi_init( &E, &P, &Q, &N, &H,
&D, &X, &Y, &Z, NULL );
mpi_init( &E ); mpi_init( &P ); mpi_init( &Q ); mpi_init( &N );
mpi_init( &H ); mpi_init( &D ); mpi_init( &X ); mpi_init( &Y );
mpi_init( &Z );
mpi_read_string( &P, 10, "2789" );
mpi_read_string( &Q, 10, "3203" );
......@@ -69,8 +70,9 @@ int main( void )
mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL );
printf( "\n" );
mpi_free( &Z, &Y, &X, &D, &H,
&N, &Q, &P, &E, NULL );
mpi_free( &E ); mpi_free( &P ); mpi_free( &Q ); mpi_free( &N );
mpi_free( &H ); mpi_free( &D ); mpi_free( &X ); mpi_free( &Y );
mpi_free( &Z );
#ifdef WIN32
printf( " Press Enter to exit this program.\n" );
......
......@@ -45,7 +45,7 @@ debug_print_mpi:radix:value:file:line:prefix:result_str
struct buffer_data buffer;
mpi val;
mpi_init( &val, NULL );
mpi_init( &val );
memset( &ssl, 0, sizeof( ssl_context ) );
memset( buffer.buf, 0, 2000 );
......@@ -57,6 +57,8 @@ debug_print_mpi:radix:value:file:line:prefix:result_str
debug_print_mpi( &ssl, 0, {file}, {line}, {prefix}, &val);
TEST_ASSERT( strcmp( buffer.buf, {result_str} ) == 0 );
mpi_free( &val );
}
END_CASE
......
......@@ -18,7 +18,7 @@ mpi_read_write_string:radix_X:input_X:radix_A:input_A:output_size:result_read:re
char str[1000];
size_t len = {output_size};
mpi_init(&X, NULL);
mpi_init( &X );
TEST_ASSERT( mpi_read_string( &X, {radix_X}, {input_X} ) == {result_read} );
if( {result_read} == 0 )
......@@ -29,6 +29,8 @@ mpi_read_write_string:radix_X:input_X:radix_A:input_A:output_size:result_read:re
TEST_ASSERT( strcasecmp( str, {input_A} ) == 0 );
}
}
mpi_free( &X );
}
END_CASE
......@@ -41,13 +43,15 @@ mpi_read_binary:input_X:radix_A:input_A
size_t len = 1000;
size_t input_len;
mpi_init(&X, NULL);
mpi_init( &X );
input_len = unhexify( buf, {input_X} );
TEST_ASSERT( mpi_read_binary( &X, buf, input_len ) == 0 );
TEST_ASSERT( mpi_write_string( &X, {radix_A}, (char *) str, &len ) == 0 );
TEST_ASSERT( strcmp( (char *) str, {input_A} ) == 0 );
mpi_free( &X );
}
END_CASE
......@@ -62,7 +66,7 @@ mpi_write_binary:radix_X:input_X:input_A:output_size:result
memset( buf, 0x00, 1000 );
memset( str, 0x00, 1000 );
mpi_init(&X, NULL);
mpi_init( &X );
TEST_ASSERT( mpi_read_string( &X, {radix_X}, {input_X} ) == 0 );
......@@ -77,6 +81,8 @@ mpi_write_binary:radix_X:input_X:input_A:output_size:result
TEST_ASSERT( strcasecmp( (char *) str, {input_A} ) == 0 );
}
mpi_free( &X );
}
END_CASE
......@@ -92,7 +98,7 @@ mpi_read_file:radix_X:input_file:input_A:result
memset( buf, 0x00, 1000 );
memset( str, 0x00, 1000 );
mpi_init(&X, NULL);
mpi_init( &X );
file = fopen( {input_file}, "r" );
TEST_ASSERT( mpi_read_file( &X, {radix_X}, file ) == {result} );
......@@ -107,6 +113,8 @@ mpi_read_file:radix_X:input_file:input_A:result
TEST_ASSERT( strcasecmp( (char *) str, {input_A} ) == 0 );
}
mpi_free( &X );
}
END_CASE
......@@ -116,7 +124,7 @@ mpi_write_file:radix_X:input_X:output_radix:output_file
mpi X, Y;
FILE *file_out, *file_in;
mpi_init( &X, &Y, NULL );
mpi_init( &X ); mpi_init( &Y );
TEST_ASSERT( mpi_read_string( &X, {radix_X}, {input_X} ) == 0 );
......@@ -129,6 +137,8 @@ mpi_write_file:radix_X:input_X:output_radix:output_file
fclose(file_in);
TEST_ASSERT( mpi_cmp_mpi( &X, &Y ) == 0 );
mpi_free( &X ); mpi_free( &Y );
}
END_CASE
......@@ -136,10 +146,12 @@ BEGIN_CASE
mpi_lsb:radix_X:input_X:nr_bits