diff --git a/include/polarssl/bignum.h b/include/polarssl/bignum.h index 2db29d5ae..dc54af8cd 100644 --- a/include/polarssl/bignum.h +++ b/include/polarssl/bignum.h @@ -691,6 +691,8 @@ int mpi_fill_random( mpi *X, size_t size, * * \return 0 if successful, * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed + * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is <= 1, + * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N. */ int mpi_gcd( mpi *G, const mpi *A, const mpi *B ); diff --git a/library/bignum.c b/library/bignum.c index 211cb2af5..9d12a86cc 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1891,7 +1891,7 @@ int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N ) int ret; mpi G, TA, TU, U1, U2, TB, TV, V1, V2; - if( mpi_cmp_int( N, 0 ) <= 0 ) + if( mpi_cmp_int( N, 1 ) <= 0 ) return( POLARSSL_ERR_MPI_BAD_INPUT_DATA ); mpi_init( &TA ); mpi_init( &TU ); mpi_init( &U1 ); mpi_init( &U2 ); diff --git a/tests/suites/test_suite_mpi.data b/tests/suites/test_suite_mpi.data index cff62b4d9..52cbd7e6a 100644 --- a/tests/suites/test_suite_mpi.data +++ b/tests/suites/test_suite_mpi.data @@ -550,6 +550,9 @@ mpi_inv_mod:10:"3":10:"-11":10:"4":POLARSSL_ERR_MPI_BAD_INPUT_DATA Base test mpi_inv_mod #4 mpi_inv_mod:10:"2":10:"4":10:"0":POLARSSL_ERR_MPI_NOT_ACCEPTABLE +Base test mbedtls_mpi_inv_mod #5 +mpi_inv_mod:10:"3":10:"1":10:"0":POLARSSL_ERR_MPI_BAD_INPUT_DATA + Test mpi_inv_mod #1 mpi_inv_mod:16:"aa4df5cb14b4c31237f98bd1faf527c283c2d0f3eec89718664ba33f9762907c":16:"fffbbd660b94412ae61ead9c2906a344116e316a256fd387874c6c675b1d587d":16:"8d6a5c1d7adeae3e94b9bcd2c47e0d46e778bc8804a2cc25c02d775dc3d05b0c":0