diff --git a/ChangeLog b/ChangeLog index c797eb706..e1b31df75 100644 --- a/ChangeLog +++ b/ChangeLog @@ -9,7 +9,8 @@ Features Changes * Major argument / variable rewrite. Introduced use of size_t instead of int for buffer lengths and loop variables for - better unsigned / signed use + better unsigned / signed use. Renamed internal bigint types + t_int and t_dbl to t_uint and t_udbl in the process = Version 0.99-pre4 released on 2011-04-01 Features diff --git a/include/polarssl/bignum.h b/include/polarssl/bignum.h index 909df1d91..00164e03f 100644 --- a/include/polarssl/bignum.h +++ b/include/polarssl/bignum.h @@ -44,27 +44,27 @@ * Define the base integer type, architecture-wise */ #if defined(POLARSSL_HAVE_INT8) -typedef signed char t_s_int; -typedef unsigned char t_int; -typedef unsigned short t_dbl; +typedef signed char t_sint; +typedef unsigned char t_uint; +typedef unsigned short t_udbl; #else #if defined(POLARSSL_HAVE_INT16) -typedef signed short t_s_int; -typedef unsigned short t_int; -typedef unsigned long t_dbl; +typedef signed short t_sint; +typedef unsigned short t_uint; +typedef unsigned long t_udbl; #else - typedef signed long t_s_int; - typedef unsigned long t_int; + typedef signed long t_sint; + typedef unsigned long t_uint; #if defined(_MSC_VER) && defined(_M_IX86) - typedef unsigned __int64 t_dbl; + typedef unsigned __int64 t_udbl; #else #if defined(__amd64__) || defined(__x86_64__) || \ defined(__ppc64__) || defined(__powerpc64__) || \ defined(__ia64__) || defined(__alpha__) - typedef unsigned int t_dbl __attribute__((mode(TI))); + typedef unsigned int t_udbl __attribute__((mode(TI))); #else #if defined(POLARSSL_HAVE_LONGLONG) - typedef unsigned long long t_dbl; + typedef unsigned long long t_udbl; #endif #endif #endif @@ -78,7 +78,7 @@ typedef struct { int s; /*!< integer sign */ size_t n; /*!< total # of limbs */ - t_int *p; /*!< pointer to limbs */ + t_uint *p; /*!< pointer to limbs */ } mpi; @@ -135,7 +135,7 @@ void mpi_swap( mpi *X, mpi *Y ); * \return 0 if successful, * 1 if memory allocation failed */ -int mpi_lset( mpi *X, t_s_int z ); +int mpi_lset( mpi *X, t_sint z ); /** * \brief Return the number of least significant bits @@ -291,7 +291,7 @@ int mpi_cmp_mpi( const mpi *X, const mpi *Y ); * -1 if X is lesser than z or * 0 if X is equal to z */ -int mpi_cmp_int( const mpi *X, t_s_int z ); +int mpi_cmp_int( const mpi *X, t_sint z ); /** * \brief Unsigned addition: X = |A| + |B| @@ -351,7 +351,7 @@ int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B ); * \return 0 if successful, * 1 if memory allocation failed */ -int mpi_add_int( mpi *X, const mpi *A, t_s_int b ); +int mpi_add_int( mpi *X, const mpi *A, t_sint b ); /** * \brief Signed substraction: X = A - b @@ -363,7 +363,7 @@ int mpi_add_int( mpi *X, const mpi *A, t_s_int b ); * \return 0 if successful, * 1 if memory allocation failed */ -int mpi_sub_int( mpi *X, const mpi *A, t_s_int b ); +int mpi_sub_int( mpi *X, const mpi *A, t_sint b ); /** * \brief Baseline multiplication: X = A * B @@ -389,7 +389,7 @@ int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ); * \return 0 if successful, * 1 if memory allocation failed */ -int mpi_mul_int( mpi *X, const mpi *A, t_s_int b ); +int mpi_mul_int( mpi *X, const mpi *A, t_sint b ); /** * \brief Division by mpi: A = Q * B + R @@ -421,7 +421,7 @@ int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B ); * * \note Either Q or R can be NULL. */ -int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_s_int b ); +int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b ); /** * \brief Modulo: R = A mod B @@ -440,7 +440,7 @@ int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B ); /** * \brief Modulo: r = A mod b * - * \param r Destination t_int + * \param r Destination t_uint * \param A Left-hand MPI * \param b Integer to divide by * @@ -449,7 +449,7 @@ int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B ); * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0, * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0 */ -int mpi_mod_int( t_int *r, const mpi *A, t_s_int b ); +int mpi_mod_int( t_uint *r, const mpi *A, t_sint b ); /** * \brief Sliding-window exponentiation: X = A^E mod N diff --git a/include/polarssl/bn_mul.h b/include/polarssl/bn_mul.h index 87b2052a7..e33c6339c 100644 --- a/include/polarssl/bn_mul.h +++ b/include/polarssl/bn_mul.h @@ -693,8 +693,8 @@ #define MULADDC_INIT \ { \ - t_dbl r; \ - t_int r0, r1; + t_udbl r; \ + t_uint r0, r1; #define MULADDC_CORE \ r = *(s++) * (t_dbl) b; \ @@ -710,8 +710,8 @@ #else #define MULADDC_INIT \ { \ - t_int s0, s1, b0, b1; \ - t_int r0, r1, rx, ry; \ + t_uint s0, s1, b0, b1; \ + t_uint r0, r1, rx, ry; \ b0 = ( b << biH ) >> biH; \ b1 = ( b >> biH ); diff --git a/library/bignum.c b/library/bignum.c index 3d8b383d9..a2b132d65 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -40,7 +40,7 @@ #include #include -#define ciL ((int) sizeof(t_int)) /* chars in limb */ +#define ciL ((int) sizeof(t_uint)) /* chars in limb */ #define biL (ciL << 3) /* bits in limb */ #define biH (ciL << 2) /* half limb size */ @@ -103,11 +103,11 @@ void mpi_free( mpi *X, ... ) */ int mpi_grow( mpi *X, size_t nblimbs ) { - t_int *p; + t_uint *p; if( X->n < nblimbs ) { - if( ( p = (t_int *) malloc( nblimbs * ciL ) ) == NULL ) + if( ( p = (t_uint *) malloc( nblimbs * ciL ) ) == NULL ) return( 1 ); memset( p, 0, nblimbs * ciL ); @@ -169,7 +169,7 @@ void mpi_swap( mpi *X, mpi *Y ) /* * Set value from integer */ -int mpi_lset( mpi *X, t_s_int z ) +int mpi_lset( mpi *X, t_sint z ) { int ret; @@ -228,7 +228,7 @@ size_t mpi_size( const mpi *X ) /* * Convert an ASCII character to digit value */ -static int mpi_get_digit( t_int *d, int radix, char c ) +static int mpi_get_digit( t_uint *d, int radix, char c ) { *d = 255; @@ -236,7 +236,7 @@ static int mpi_get_digit( t_int *d, int radix, char c ) if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37; if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57; - if( *d >= (t_int) radix ) + if( *d >= (t_uint) radix ) return( POLARSSL_ERR_MPI_INVALID_CHARACTER ); return( 0 ); @@ -249,7 +249,7 @@ int mpi_read_string( mpi *X, int radix, const char *s ) { int ret; size_t i, j, slen, n; - t_int d; + t_uint d; mpi T; if( radix < 2 || radix > 16 ) @@ -317,7 +317,7 @@ cleanup: static int mpi_write_hlp( mpi *X, int radix, char **p ) { int ret; - t_int r; + t_uint r; if( radix < 2 || radix > 16 ) return( POLARSSL_ERR_MPI_BAD_INPUT_DATA ); @@ -412,7 +412,7 @@ cleanup: */ int mpi_read_file( mpi *X, int radix, FILE *fin ) { - t_int d; + t_uint d; size_t slen; char *p; char s[1024]; @@ -485,7 +485,7 @@ int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen ) MPI_CHK( mpi_lset( X, 0 ) ); for( i = buflen, j = 0; i > n; i--, j++ ) - X->p[j / ciL] |= ((t_int) buf[i - 1]) << ((j % ciL) << 3); + X->p[j / ciL] |= ((t_uint) buf[i - 1]) << ((j % ciL) << 3); cleanup: @@ -519,7 +519,7 @@ int mpi_shift_l( mpi *X, size_t count ) { int ret; size_t i, v0, t1; - t_int r0 = 0, r1; + t_uint r0 = 0, r1; v0 = count / (biL ); t1 = count & (biL - 1); @@ -568,7 +568,7 @@ cleanup: int mpi_shift_r( mpi *X, size_t count ) { size_t i, v0, v1; - t_int r0 = 0, r1; + t_uint r0 = 0, r1; v0 = count / biL; v1 = count & (biL - 1); @@ -668,10 +668,10 @@ int mpi_cmp_mpi( const mpi *X, const mpi *Y ) /* * Compare signed values */ -int mpi_cmp_int( const mpi *X, t_s_int z ) +int mpi_cmp_int( const mpi *X, t_sint z ) { mpi Y; - t_int p[1]; + t_uint p[1]; *p = ( z < 0 ) ? -z : z; Y.s = ( z < 0 ) ? -1 : 1; @@ -688,7 +688,7 @@ int mpi_add_abs( mpi *X, const mpi *A, const mpi *B ) { int ret; size_t i, j; - t_int *o, *p, c; + t_uint *o, *p, c; if( X == B ) { @@ -736,10 +736,10 @@ cleanup: /* * Helper for mpi substraction */ -static void mpi_sub_hlp( size_t n, t_int *s, t_int *d ) +static void mpi_sub_hlp( size_t n, t_uint *s, t_uint *d ) { size_t i; - t_int c, z; + t_uint c, z; for( i = c = 0; i < n; i++, s++, d++ ) { @@ -862,10 +862,10 @@ cleanup: /* * Signed addition: X = A + b */ -int mpi_add_int( mpi *X, const mpi *A, t_s_int b ) +int mpi_add_int( mpi *X, const mpi *A, t_sint b ) { mpi _B; - t_int p[1]; + t_uint p[1]; p[0] = ( b < 0 ) ? -b : b; _B.s = ( b < 0 ) ? -1 : 1; @@ -878,10 +878,10 @@ int mpi_add_int( mpi *X, const mpi *A, t_s_int b ) /* * Signed substraction: X = A - b */ -int mpi_sub_int( mpi *X, const mpi *A, t_s_int b ) +int mpi_sub_int( mpi *X, const mpi *A, t_sint b ) { mpi _B; - t_int p[1]; + t_uint p[1]; p[0] = ( b < 0 ) ? -b : b; _B.s = ( b < 0 ) ? -1 : 1; @@ -894,9 +894,9 @@ int mpi_sub_int( mpi *X, const mpi *A, t_s_int b ) /* * Helper for mpi multiplication */ -static void mpi_mul_hlp( size_t i, t_int *s, t_int *d, t_int b ) +static void mpi_mul_hlp( size_t i, t_uint *s, t_uint *d, t_uint b ) { - t_int c = 0, t = 0; + t_uint c = 0, t = 0; #if defined(MULADDC_HUIT) for( ; i >= 8; i -= 8 ) @@ -995,10 +995,10 @@ cleanup: /* * Baseline multiplication: X = A * b */ -int mpi_mul_int( mpi *X, const mpi *A, t_s_int b ) +int mpi_mul_int( mpi *X, const mpi *A, t_sint b ) { mpi _B; - t_int p[1]; + t_uint p[1]; _B.s = 1; _B.n = 1; @@ -1073,13 +1073,13 @@ int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B ) if( r > ((t_dbl) 1 << biL) - 1) r = ((t_dbl) 1 << biL) - 1; - Z.p[i - t - 1] = (t_int) r; + Z.p[i - t - 1] = (t_uint) r; #else /* * __udiv_qrnnd_c, from gmp/longlong.h */ - t_int q0, q1, r0, r1; - t_int d0, d1, d, m; + t_uint q0, q1, r0, r1; + t_uint d0, d1, d, m; d = Y.p[t]; d0 = ( d << biH ) >> biH; @@ -1177,10 +1177,10 @@ cleanup: * 1 if memory allocation failed * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0 */ -int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_s_int b ) +int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b ) { mpi _B; - t_int p[1]; + t_uint p[1]; p[0] = ( b < 0 ) ? -b : b; _B.s = ( b < 0 ) ? -1 : 1; @@ -1216,10 +1216,10 @@ cleanup: /* * Modulo: r = A mod b */ -int mpi_mod_int( t_int *r, const mpi *A, t_s_int b ) +int mpi_mod_int( t_uint *r, const mpi *A, t_sint b ) { size_t i; - t_int x, y, z; + t_uint x, y, z; if( b == 0 ) return( POLARSSL_ERR_MPI_DIVISION_BY_ZERO ); @@ -1273,9 +1273,9 @@ int mpi_mod_int( t_int *r, const mpi *A, t_s_int b ) /* * Fast Montgomery initialization (thanks to Tom St Denis) */ -static void mpi_montg_init( t_int *mm, const mpi *N ) +static void mpi_montg_init( t_uint *mm, const mpi *N ) { - t_int x, m0 = N->p[0]; + t_uint x, m0 = N->p[0]; x = m0; x += ( ( m0 + 2 ) & 4 ) << 1; @@ -1291,10 +1291,10 @@ static void mpi_montg_init( t_int *mm, const mpi *N ) /* * Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36) */ -static void mpi_montmul( mpi *A, const mpi *B, const mpi *N, t_int mm, const mpi *T ) +static void mpi_montmul( mpi *A, const mpi *B, const mpi *N, t_uint mm, const mpi *T ) { size_t i, n, m; - t_int u0, u1, *d; + t_uint u0, u1, *d; memset( T->p, 0, T->n * ciL ); @@ -1328,9 +1328,9 @@ static void mpi_montmul( mpi *A, const mpi *B, const mpi *N, t_int mm, const mpi /* * Montgomery reduction: A = A * R^-1 mod N */ -static void mpi_montred( mpi *A, const mpi *N, t_int mm, const mpi *T ) +static void mpi_montred( mpi *A, const mpi *N, t_uint mm, const mpi *T ) { - t_int z = 1; + t_uint z = 1; mpi U; U.n = U.s = z; @@ -1348,7 +1348,7 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) size_t wbits, wsize, one = 1; size_t i, j, nblimbs; size_t bufsize, nbits; - t_int ei, mm, state; + t_uint ei, mm, state; mpi RR, T, W[64]; if( mpi_cmp_int( N, 0 ) < 0 || ( N->p[0] & 1 ) == 0 ) @@ -1439,7 +1439,7 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ) if( nblimbs-- == 0 ) break; - bufsize = sizeof( t_int ) << 3; + bufsize = sizeof( t_uint ) << 3; } bufsize--; @@ -1735,7 +1735,7 @@ int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng ) for( i = 0; small_prime[i] > 0; i++ ) { - t_int r; + t_uint r; if( mpi_cmp_int( X, small_prime[i] ) <= 0 ) return( 0 ); diff --git a/library/debug.c b/library/debug.c index 1c8dbf523..5159d3a51 100644 --- a/library/debug.c +++ b/library/debug.c @@ -142,13 +142,13 @@ void debug_print_mpi( const ssl_context *ssl, int level, if( X->p[n] != 0 ) break; - for( j = ( sizeof(t_int) << 3 ) - 1; j >= 0; j-- ) + for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- ) if( ( ( X->p[n] >> j ) & 1 ) != 0 ) break; snprintf( str, maxlen, "%s(%04d): value of '%s' (%lu bits) is:\n", file, line, text, - (unsigned long) ( ( n * ( sizeof(t_int) << 3 ) ) + j + 1 ) ); + (unsigned long) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) ); str[maxlen] = '\0'; ssl->f_dbg( ssl->p_dbg, level, str ); @@ -158,7 +158,7 @@ void debug_print_mpi( const ssl_context *ssl, int level, if( zeros && X->p[i - 1] == 0 ) continue; - for( k = sizeof( t_int ) - 1; k >= 0; k-- ) + for( k = sizeof( t_uint ) - 1; k >= 0; k-- ) { if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 ) continue; diff --git a/library/dhm.c b/library/dhm.c index 1a8211ee7..59e78f89f 100644 --- a/library/dhm.c +++ b/library/dhm.c @@ -136,7 +136,7 @@ int dhm_make_params( dhm_context *ctx, int x_size, /* * Generate X as large as possible ( < P ) */ - n = x_size / sizeof( t_int ) + 1; + n = x_size / sizeof( t_uint ) + 1; mpi_fill_random( &ctx->X, n, f_rng, p_rng ); @@ -213,7 +213,7 @@ 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; + n = x_size / sizeof( t_uint ) + 1; mpi_fill_random( &ctx->X, n, f_rng, p_rng ); diff --git a/tests/suites/test_suite_mpi.function b/tests/suites/test_suite_mpi.function index 3278eb136..2dacde8c6 100644 --- a/tests/suites/test_suite_mpi.function +++ b/tests/suites/test_suite_mpi.function @@ -455,7 +455,7 @@ mpi_mod_int:radix_X:input_X:input_Y:input_A:div_result { mpi X; int res; - t_int r; + t_uint r; mpi_init(&X, NULL); TEST_ASSERT( mpi_read_string( &X, {radix_X}, {input_X} ) == 0 );