diff --git a/include/mbedtls/bignum.h b/include/mbedtls/bignum.h index d03b983f1..3c9eeea5b 100644 --- a/include/mbedtls/bignum.h +++ b/include/mbedtls/bignum.h @@ -299,7 +299,7 @@ size_t mbedtls_mpi_lsb( const mbedtls_mpi *X ); * * \param X MPI to use */ -size_t mbedtls_mpi_msb( const mbedtls_mpi *X ); +size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X ); /** * \brief Return the total size in bytes diff --git a/include/mbedtls/compat-1.3.h b/include/mbedtls/compat-1.3.h index 8b0342a31..3c0f15118 100644 --- a/include/mbedtls/compat-1.3.h +++ b/include/mbedtls/compat-1.3.h @@ -2149,7 +2149,7 @@ #define mpi_lset mbedtls_mpi_lset #define mpi_mod_int mbedtls_mpi_mod_int #define mpi_mod_mpi mbedtls_mpi_mod_mpi -#define mpi_msb mbedtls_mpi_msb +#define mpi_msb mbedtls_mpi_bitlen #define mpi_mul_int mbedtls_mpi_mul_int #define mpi_mul_mpi mbedtls_mpi_mul_mpi #define mpi_read_binary mbedtls_mpi_read_binary diff --git a/library/bignum.c b/library/bignum.c index 563e67a54..ad0aa2cda 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -331,7 +331,7 @@ cleanup: } /* - * Return the number of least significant bits + * Return the number of less significant zero-bits */ size_t mbedtls_mpi_lsb( const mbedtls_mpi *X ) { @@ -346,9 +346,9 @@ size_t mbedtls_mpi_lsb( const mbedtls_mpi *X ) } /* - * Return the number of most significant bits + * Return the number of bits */ -size_t mbedtls_mpi_msb( const mbedtls_mpi *X ) +size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X ) { size_t i, j; @@ -371,7 +371,7 @@ size_t mbedtls_mpi_msb( const mbedtls_mpi *X ) */ size_t mbedtls_mpi_size( const mbedtls_mpi *X ) { - return( ( mbedtls_mpi_msb( X ) + 7 ) >> 3 ); + return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 ); } /* @@ -501,7 +501,7 @@ int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, if( radix < 2 || radix > 16 ) return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); - n = mbedtls_mpi_msb( X ); + n = mbedtls_mpi_bitlen( X ); if( radix >= 4 ) n >>= 1; if( radix >= 16 ) n >>= 1; n += 3; @@ -686,7 +686,7 @@ int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count ) v0 = count / (biL ); t1 = count & (biL - 1); - i = mbedtls_mpi_msb( X ) + count; + i = mbedtls_mpi_bitlen( X ) + count; if( X->n * biL < i ) MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) ); @@ -1212,7 +1212,7 @@ int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, c MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) ); MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T2, 3 ) ); - k = mbedtls_mpi_msb( &Y ) % biL; + k = mbedtls_mpi_bitlen( &Y ) % biL; if( k < biL - 1 ) { k = biL - 1 - k; @@ -1537,7 +1537,7 @@ int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi mbedtls_mpi_init( &Apos ); memset( W, 0, sizeof( W ) ); - i = mbedtls_mpi_msb( E ); + i = mbedtls_mpi_bitlen( E ); wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 : ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1; @@ -1972,7 +1972,7 @@ static int mpi_miller_rabin( const mbedtls_mpi *X, MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) ); MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) ); - i = mbedtls_mpi_msb( X ); + i = mbedtls_mpi_bitlen( X ); /* * HAC, table 4.4 */ @@ -1989,7 +1989,7 @@ static int mpi_miller_rabin( const mbedtls_mpi *X, if( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ) { - j = mbedtls_mpi_msb( &A ) - mbedtls_mpi_msb( &W ); + j = mbedtls_mpi_bitlen( &A ) - mbedtls_mpi_bitlen( &W ); MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &A, j + 1 ) ); } A.p[0] |= 3; @@ -1998,8 +1998,8 @@ static int mpi_miller_rabin( const mbedtls_mpi *X, do { MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) ); - j = mbedtls_mpi_msb( &A ); - k = mbedtls_mpi_msb( &W ); + j = mbedtls_mpi_bitlen( &A ); + k = mbedtls_mpi_bitlen( &W ); if (j > k) { MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &A, j - k ) ); } @@ -2106,7 +2106,7 @@ int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag, MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) ); - k = mbedtls_mpi_msb( X ); + k = mbedtls_mpi_bitlen( X ); if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits + 1 ) ); mbedtls_mpi_set_bit( X, nbits-1, 1 ); diff --git a/library/ecp.c b/library/ecp.c index b733bcc10..a80274722 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -660,7 +660,7 @@ static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp ) /* N->s < 0 is a much faster test, which fails only if N is 0 */ if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) || - mbedtls_mpi_msb( N ) > 2 * grp->pbits ) + mbedtls_mpi_bitlen( N ) > 2 * grp->pbits ) { return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); } @@ -1568,7 +1568,7 @@ static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) ); /* Loop invariant: R = result so far, RP = R + P */ - i = mbedtls_mpi_msb( m ); /* one past the (zero-based) most significant bit */ + i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */ while( i-- > 0 ) { b = mbedtls_mpi_get_bit( m, i ); @@ -1747,7 +1747,7 @@ int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi * if( mbedtls_mpi_get_bit( d, 0 ) != 0 || mbedtls_mpi_get_bit( d, 1 ) != 0 || mbedtls_mpi_get_bit( d, 2 ) != 0 || - mbedtls_mpi_msb( d ) - 1 != grp->nbits ) /* mbedtls_mpi_msb is one-based! */ + mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */ return( MBEDTLS_ERR_ECP_INVALID_KEY ); else return( 0 ); @@ -1787,7 +1787,7 @@ int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) ); /* Make sure the most significant bit is nbits */ - b = mbedtls_mpi_msb( d ) - 1; /* mbedtls_mpi_msb is one-based */ + b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */ if( b > grp->nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) ); else diff --git a/library/ecp_curves.c b/library/ecp_curves.c index 41d7f79b9..e97c40143 100644 --- a/library/ecp_curves.c +++ b/library/ecp_curves.c @@ -594,8 +594,8 @@ static int ecp_group_load( mbedtls_ecp_group *grp, ecp_mpi_load( &grp->G.Y, gy, gylen ); ecp_mpi_set1( &grp->G.Z ); - grp->pbits = mbedtls_mpi_msb( &grp->P ); - grp->nbits = mbedtls_mpi_msb( &grp->N ); + grp->pbits = mbedtls_mpi_bitlen( &grp->P ); + grp->nbits = mbedtls_mpi_bitlen( &grp->N ); grp->h = 1; @@ -670,7 +670,7 @@ static int ecp_use_curve25519( mbedtls_ecp_group *grp ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->P, 1 ) ); MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &grp->P, 255 ) ); MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &grp->P, &grp->P, 19 ) ); - grp->pbits = mbedtls_mpi_msb( &grp->P ); + grp->pbits = mbedtls_mpi_bitlen( &grp->P ); /* Y intentionaly not set, since we use x/z coordinates. * This is used as a marker to identify Montgomery curves! */ diff --git a/library/pkparse.c b/library/pkparse.c index c8a68eb14..f24dbad92 100644 --- a/library/pkparse.c +++ b/library/pkparse.c @@ -274,7 +274,7 @@ static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_ if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 ) return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); - grp->pbits = mbedtls_mpi_msb( &grp->P ); + grp->pbits = mbedtls_mpi_bitlen( &grp->P ); if( p != end_field ) return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + @@ -355,7 +355,7 @@ static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_ if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 ) return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); - grp->nbits = mbedtls_mpi_msb( &grp->N ); + grp->nbits = mbedtls_mpi_bitlen( &grp->N ); /* * Allow optional elements by purposefully not enforcing p == end here. diff --git a/library/rsa.c b/library/rsa.c index 6aee10970..9f0d46186 100644 --- a/library/rsa.c +++ b/library/rsa.c @@ -118,7 +118,7 @@ int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx, continue; MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) ); - if( mbedtls_mpi_msb( &ctx->N ) != nbits ) + if( mbedtls_mpi_bitlen( &ctx->N ) != nbits ) continue; MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) ); @@ -139,7 +139,7 @@ int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx, MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) ); MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) ); - ctx->len = ( mbedtls_mpi_msb( &ctx->N ) + 7 ) >> 3; + ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3; cleanup: @@ -168,11 +168,11 @@ int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx ) ( ctx->E.p[0] & 1 ) == 0 ) return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); - if( mbedtls_mpi_msb( &ctx->N ) < 128 || - mbedtls_mpi_msb( &ctx->N ) > MBEDTLS_MPI_MAX_BITS ) + if( mbedtls_mpi_bitlen( &ctx->N ) < 128 || + mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS ) return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); - if( mbedtls_mpi_msb( &ctx->E ) < 2 || + if( mbedtls_mpi_bitlen( &ctx->E ) < 2 || mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 ) return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); @@ -980,7 +980,7 @@ int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, // Note: EMSA-PSS encoding is over the length of N - 1 bits // - msb = mbedtls_mpi_msb( &ctx->N ) - 1; + msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; p += olen - hlen * 2 - 2; *p++ = 0x01; memcpy( p, salt, slen ); @@ -1008,7 +1008,7 @@ int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, mbedtls_md_free( &md_ctx ); - msb = mbedtls_mpi_msb( &ctx->N ) - 1; + msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; sig[0] &= 0xFF >> ( olen * 8 - msb ); p += hlen; @@ -1206,7 +1206,7 @@ int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx, // Note: EMSA-PSS verification is over the length of N - 1 bits // - msb = mbedtls_mpi_msb( &ctx->N ) - 1; + msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; // Compensate for boundary condition when applying mask // diff --git a/programs/pkey/dh_client.c b/programs/pkey/dh_client.c index 546b02ebc..0b340d516 100644 --- a/programs/pkey/dh_client.c +++ b/programs/pkey/dh_client.c @@ -127,7 +127,7 @@ int main( void ) goto exit; } - rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3; + rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3; fclose( f ); diff --git a/programs/pkey/dh_server.c b/programs/pkey/dh_server.c index 81c0c9f9e..8fda29fda 100644 --- a/programs/pkey/dh_server.c +++ b/programs/pkey/dh_server.c @@ -134,7 +134,7 @@ int main( void ) goto exit; } - rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3; + rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3; fclose( f ); diff --git a/programs/pkey/rsa_decrypt.c b/programs/pkey/rsa_decrypt.c index de16de539..2a6c15fd9 100644 --- a/programs/pkey/rsa_decrypt.c +++ b/programs/pkey/rsa_decrypt.c @@ -120,7 +120,7 @@ int main( int argc, char *argv[] ) goto exit; } - rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3; + rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3; fclose( f ); diff --git a/programs/pkey/rsa_encrypt.c b/programs/pkey/rsa_encrypt.c index a3f9a6636..8604323a5 100644 --- a/programs/pkey/rsa_encrypt.c +++ b/programs/pkey/rsa_encrypt.c @@ -114,7 +114,7 @@ int main( int argc, char *argv[] ) goto exit; } - rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3; + rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3; fclose( f ); diff --git a/programs/pkey/rsa_sign.c b/programs/pkey/rsa_sign.c index e2d39fcbb..895880ff3 100644 --- a/programs/pkey/rsa_sign.c +++ b/programs/pkey/rsa_sign.c @@ -100,7 +100,7 @@ int main( int argc, char *argv[] ) goto exit; } - rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3; + rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3; fclose( f ); diff --git a/programs/pkey/rsa_verify.c b/programs/pkey/rsa_verify.c index 35467ee3e..3de84d682 100644 --- a/programs/pkey/rsa_verify.c +++ b/programs/pkey/rsa_verify.c @@ -91,7 +91,7 @@ int main( int argc, char *argv[] ) goto exit; } - rsa.len = ( mbedtls_mpi_msb( &rsa.N ) + 7 ) >> 3; + rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3; fclose( f ); diff --git a/scripts/data_files/rename-1.3-2.0.txt b/scripts/data_files/rename-1.3-2.0.txt index 3f773e3cd..d0820b0ec 100644 --- a/scripts/data_files/rename-1.3-2.0.txt +++ b/scripts/data_files/rename-1.3-2.0.txt @@ -1684,7 +1684,7 @@ mpi_lsb mbedtls_mpi_lsb mpi_lset mbedtls_mpi_lset mpi_mod_int mbedtls_mpi_mod_int mpi_mod_mpi mbedtls_mpi_mod_mpi -mpi_msb mbedtls_mpi_msb +mpi_msb mbedtls_mpi_bitlen mpi_mul_int mbedtls_mpi_mul_int mpi_mul_mpi mbedtls_mpi_mul_mpi mpi_read_binary mbedtls_mpi_read_binary diff --git a/tests/suites/test_suite_ecp.function b/tests/suites/test_suite_ecp.function index 56b8f6194..eee648693 100644 --- a/tests/suites/test_suite_ecp.function +++ b/tests/suites/test_suite_ecp.function @@ -175,7 +175,7 @@ void ecp_fast_mod( int id, char *N_str ) TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &N, &grp.P ) == 0 ); TEST_ASSERT( grp.modp( &N ) == 0 ); - TEST_ASSERT( mbedtls_mpi_msb( &N ) <= grp.pbits + 3 ); + TEST_ASSERT( mbedtls_mpi_bitlen( &N ) <= grp.pbits + 3 ); /* * Use mod rather than addition/subtraction in case previous test fails @@ -371,7 +371,7 @@ void mbedtls_ecp_tls_read_group( char *record, int result, int bits ) TEST_ASSERT( ret == result ); if( ret == 0) { - TEST_ASSERT( mbedtls_mpi_msb( &grp.P ) == (size_t) bits ); + TEST_ASSERT( mbedtls_mpi_bitlen( &grp.P ) == (size_t) bits ); TEST_ASSERT( *vbuf == 0x00 ); } diff --git a/tests/suites/test_suite_mpi.data b/tests/suites/test_suite_mpi.data index 8fce55ab0..f838f3bda 100644 --- a/tests/suites/test_suite_mpi.data +++ b/tests/suites/test_suite_mpi.data @@ -85,26 +85,26 @@ mbedtls_mpi_lsb:16:"24":2 Base test mbedtls_mpi_lsb #4 mbedtls_mpi_lsb:16:"2000":13 -Base test mbedtls_mpi_msb #1 -mbedtls_mpi_msb:10:"56125680981752282334141896320372489490613963693556392520816017892111350604111697682705498319512049040516698827829292076808006940873974979584527073481012636016353913462376755556720019831187364993587901952757307830896531678727717924":764 +Base test mbedtls_mpi_bitlen #1 +mbedtls_mpi_bitlen:10:"56125680981752282334141896320372489490613963693556392520816017892111350604111697682705498319512049040516698827829292076808006940873974979584527073481012636016353913462376755556720019831187364993587901952757307830896531678727717924":764 -Base test mbedtls_mpi_msb #2 -mbedtls_mpi_msb:10:"24":5 +Base test mbedtls_mpi_bitlen #2 +mbedtls_mpi_bitlen:10:"24":5 -Base test mbedtls_mpi_msb #3 -mbedtls_mpi_msb:10:"1":1 +Base test mbedtls_mpi_bitlen #3 +mbedtls_mpi_bitlen:10:"1":1 -Base test mbedtls_mpi_msb #4 -mbedtls_mpi_msb:10:"15":4 +Base test mbedtls_mpi_bitlen #4 +mbedtls_mpi_bitlen:10:"15":4 -Base test mbedtls_mpi_msb #5 -mbedtls_mpi_msb:10:"16":5 +Base test mbedtls_mpi_bitlen #5 +mbedtls_mpi_bitlen:10:"16":5 -Base test mbedtls_mpi_msb #6 -mbedtls_mpi_msb:10:"10":4 +Base test mbedtls_mpi_bitlen #6 +mbedtls_mpi_bitlen:10:"10":4 -Base test mbedtls_mpi_msb #7 -mbedtls_mpi_msb:10:"0":0 +Base test mbedtls_mpi_bitlen #7 +mbedtls_mpi_bitlen:10:"0":0 Base test mbedtls_mpi_cmp_int #1 mbedtls_mpi_cmp_int:693:693:0 diff --git a/tests/suites/test_suite_mpi.function b/tests/suites/test_suite_mpi.function index 2d68f8d57..1308e56aa 100644 --- a/tests/suites/test_suite_mpi.function +++ b/tests/suites/test_suite_mpi.function @@ -18,7 +18,7 @@ void mpi_null( ) TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 ); TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 ); - TEST_ASSERT( mbedtls_mpi_msb( &X ) == 0 ); + TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 ); TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 ); exit: @@ -217,13 +217,13 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void mbedtls_mpi_msb( int radix_X, char *input_X, int nr_bits ) +void mbedtls_mpi_bitlen( int radix_X, char *input_X, int nr_bits ) { mbedtls_mpi X; mbedtls_mpi_init( &X ); TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); - TEST_ASSERT( mbedtls_mpi_msb( &X ) == (size_t) nr_bits ); + TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits ); exit: mbedtls_mpi_free( &X ); @@ -789,7 +789,7 @@ void mbedtls_mpi_gen_prime( int bits, int safe, int ref_ret ) if( ref_ret == 0 ) { - size_t actual_bits = mbedtls_mpi_msb( &X ); + size_t actual_bits = mbedtls_mpi_bitlen( &X ); TEST_ASSERT( actual_bits >= (size_t) bits ); TEST_ASSERT( actual_bits <= (size_t) bits + 1 );