Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/bignum.h" |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 3 | #include "mbedtls/entropy.h" |
Janos Follath | 23bdeca | 2022-07-22 18:24:06 +0100 | [diff] [blame] | 4 | #include "constant_time_internal.h" |
Gilles Peskine | 34e8a2c | 2022-09-27 22:04:51 +0200 | [diff] [blame] | 5 | #include "bignum_core.h" |
Janos Follath | 23bdeca | 2022-07-22 18:24:06 +0100 | [diff] [blame] | 6 | #include "test/constant_flow.h" |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 7 | |
Chris Jones | e64a46f | 2020-12-03 17:44:03 +0000 | [diff] [blame] | 8 | #if MBEDTLS_MPI_MAX_BITS > 792 |
| 9 | #define MPI_MAX_BITS_LARGER_THAN_792 |
Chris Jones | 4592bd8 | 2020-12-03 14:24:33 +0000 | [diff] [blame] | 10 | #endif |
Gabor Mezei | 89e3146 | 2022-08-12 15:36:56 +0200 | [diff] [blame] | 11 | |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 12 | /* Check the validity of the sign bit in an MPI object. Reject representations |
| 13 | * that are not supported by the rest of the library and indicate a bug when |
| 14 | * constructing the value. */ |
| 15 | static int sign_is_valid( const mbedtls_mpi *X ) |
| 16 | { |
Gilles Peskine | ca6e8aa | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 17 | /* Only +1 and -1 are valid sign bits, not e.g. 0 */ |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 18 | if( X->s != 1 && X->s != -1 ) |
Gilles Peskine | ca6e8aa | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 19 | return( 0 ); |
| 20 | |
| 21 | /* The value 0 must be represented with the sign +1. A "negative zero" |
| 22 | * with s=-1 is an invalid representation. Forbid that. As an exception, |
| 23 | * we sometimes test the robustness of library functions when given |
| 24 | * a negative zero input. If a test case has a negative zero as input, |
| 25 | * we don't mind if the function has a negative zero output. */ |
| 26 | if( ! mbedtls_test_case_uses_negative_0 && |
| 27 | mbedtls_mpi_bitlen( X ) == 0 && X->s != 1 ) |
| 28 | { |
| 29 | return( 0 ); |
| 30 | } |
| 31 | |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 32 | return( 1 ); |
| 33 | } |
| 34 | |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 35 | typedef struct mbedtls_test_mpi_random |
| 36 | { |
| 37 | data_t *data; |
| 38 | size_t pos; |
| 39 | size_t chunk_len; |
| 40 | } mbedtls_test_mpi_random; |
| 41 | |
| 42 | /* |
| 43 | * This function is called by the Miller-Rabin primality test each time it |
| 44 | * chooses a random witness. The witnesses (or non-witnesses as provided by the |
| 45 | * test) are stored in the data member of the state structure. Each number is in |
| 46 | * the format that mbedtls_mpi_read_string understands and is chunk_len long. |
| 47 | */ |
| 48 | int mbedtls_test_mpi_miller_rabin_determinizer( void* state, |
| 49 | unsigned char* buf, |
| 50 | size_t len ) |
| 51 | { |
| 52 | mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state; |
| 53 | |
| 54 | if( random == NULL || random->data->x == NULL || buf == NULL ) |
| 55 | return( -1 ); |
| 56 | |
| 57 | if( random->pos + random->chunk_len > random->data->len |
| 58 | || random->chunk_len > len ) |
| 59 | { |
| 60 | return( -1 ); |
| 61 | } |
| 62 | |
| 63 | memset( buf, 0, len ); |
| 64 | |
| 65 | /* The witness is written to the end of the buffer, since the buffer is |
| 66 | * used as big endian, unsigned binary data in mbedtls_mpi_read_binary. |
| 67 | * Writing the witness to the start of the buffer would result in the |
| 68 | * buffer being 'witness 000...000', which would be treated as |
| 69 | * witness * 2^n for some n. */ |
| 70 | memcpy( buf + len - random->chunk_len, &random->data->x[random->pos], |
| 71 | random->chunk_len ); |
| 72 | |
| 73 | random->pos += random->chunk_len; |
| 74 | |
| 75 | return( 0 ); |
| 76 | } |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 77 | |
| 78 | /* Random generator that is told how many bytes to return. */ |
| 79 | static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len ) |
| 80 | { |
| 81 | size_t *bytes_left = state; |
| 82 | size_t i; |
| 83 | for( i = 0; i < len; i++ ) |
| 84 | { |
| 85 | if( *bytes_left == 0 ) |
| 86 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 87 | buf[i] = *bytes_left & 0xff; |
| 88 | --( *bytes_left ); |
| 89 | } |
| 90 | return( 0 ); |
| 91 | } |
| 92 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 93 | /* END_HEADER */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 94 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 95 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | * depends_on:MBEDTLS_BIGNUM_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 97 | * END_DEPENDENCIES |
| 98 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 99 | |
Hanno Becker | b48e1aa | 2018-12-18 23:25:01 +0000 | [diff] [blame] | 100 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 101 | void mpi_null( ) |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 102 | { |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 103 | mbedtls_mpi X, Y, Z; |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 104 | |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 105 | mbedtls_mpi_init( &X ); |
| 106 | mbedtls_mpi_init( &Y ); |
| 107 | mbedtls_mpi_init( &Z ); |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 108 | |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 109 | TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 ); |
| 110 | TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 ); |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 111 | TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 ); |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 112 | TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 ); |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 113 | |
| 114 | exit: |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 115 | mbedtls_mpi_free( &X ); |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 116 | } |
| 117 | /* END_CASE */ |
| 118 | |
| 119 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 120 | void mpi_read_write_string( int radix_X, char * input_X, int radix_A, |
| 121 | char * input_A, int output_size, int result_read, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 122 | int result_write ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 123 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | mbedtls_mpi X; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 125 | char str[1000]; |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 126 | size_t len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 127 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 129 | |
Janos Follath | 04dadb7 | 2019-03-06 12:29:37 +0000 | [diff] [blame] | 130 | memset( str, '!', sizeof( str ) ); |
| 131 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 133 | if( result_read == 0 ) |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 134 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 135 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 136 | TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 137 | if( result_write == 0 ) |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 138 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 139 | TEST_ASSERT( strcasecmp( str, input_A ) == 0 ); |
Janos Follath | 04dadb7 | 2019-03-06 12:29:37 +0000 | [diff] [blame] | 140 | TEST_ASSERT( str[len] == '!' ); |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 143 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 144 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 146 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 147 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 148 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 149 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 150 | void mpi_read_binary( data_t * buf, char * input_A ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 151 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | mbedtls_mpi X; |
Janos Follath | e5670f2 | 2019-02-25 16:11:58 +0000 | [diff] [blame] | 153 | char str[1000]; |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 154 | size_t len; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 157 | |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 158 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 159 | TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 160 | TEST_ASSERT( sign_is_valid( &X ) ); |
Werner Lewis | f65a327 | 2022-07-07 11:38:44 +0100 | [diff] [blame] | 161 | TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 ); |
Werner Lewis | dc47fe7 | 2022-08-01 13:55:41 +0100 | [diff] [blame] | 162 | TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 163 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 164 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 166 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 167 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 168 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 169 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 170 | void mpi_read_binary_le( data_t * buf, char * input_A ) |
Janos Follath | a778a94 | 2019-02-13 10:28:28 +0000 | [diff] [blame] | 171 | { |
| 172 | mbedtls_mpi X; |
Janos Follath | e5670f2 | 2019-02-25 16:11:58 +0000 | [diff] [blame] | 173 | char str[1000]; |
Janos Follath | a778a94 | 2019-02-13 10:28:28 +0000 | [diff] [blame] | 174 | size_t len; |
| 175 | |
| 176 | mbedtls_mpi_init( &X ); |
| 177 | |
| 178 | |
| 179 | TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 180 | TEST_ASSERT( sign_is_valid( &X ) ); |
Werner Lewis | f65a327 | 2022-07-07 11:38:44 +0100 | [diff] [blame] | 181 | TEST_ASSERT( mbedtls_mpi_write_string( &X, 16, str, sizeof( str ), &len ) == 0 ); |
Werner Lewis | dc47fe7 | 2022-08-01 13:55:41 +0100 | [diff] [blame] | 182 | TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 ); |
Janos Follath | a778a94 | 2019-02-13 10:28:28 +0000 | [diff] [blame] | 183 | |
| 184 | exit: |
| 185 | mbedtls_mpi_free( &X ); |
| 186 | } |
| 187 | /* END_CASE */ |
| 188 | |
| 189 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 190 | void mpi_write_binary( char * input_X, data_t * input_A, |
| 191 | int output_size, int result ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 192 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | mbedtls_mpi X; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 194 | unsigned char buf[1000]; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 195 | size_t buflen; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 196 | |
| 197 | memset( buf, 0x00, 1000 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 198 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 200 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 201 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | e670f90 | 2015-10-30 09:23:19 +0100 | [diff] [blame] | 202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | buflen = mbedtls_mpi_size( &X ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 204 | if( buflen > (size_t) output_size ) |
| 205 | buflen = (size_t) output_size; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 206 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 207 | TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 208 | if( result == 0) |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 209 | { |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 210 | |
Ronald Cron | 2dbba99 | 2020-06-10 11:42:32 +0200 | [diff] [blame] | 211 | TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x, |
| 212 | buflen, input_A->len ) == 0 ); |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 213 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 214 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 215 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 217 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 218 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 219 | |
Janos Follath | e344d0f | 2019-02-19 16:17:40 +0000 | [diff] [blame] | 220 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 221 | void mpi_write_binary_le( char * input_X, data_t * input_A, |
| 222 | int output_size, int result ) |
Janos Follath | e344d0f | 2019-02-19 16:17:40 +0000 | [diff] [blame] | 223 | { |
| 224 | mbedtls_mpi X; |
| 225 | unsigned char buf[1000]; |
| 226 | size_t buflen; |
| 227 | |
| 228 | memset( buf, 0x00, 1000 ); |
| 229 | |
| 230 | mbedtls_mpi_init( &X ); |
| 231 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 232 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Janos Follath | e344d0f | 2019-02-19 16:17:40 +0000 | [diff] [blame] | 233 | |
| 234 | buflen = mbedtls_mpi_size( &X ); |
| 235 | if( buflen > (size_t) output_size ) |
| 236 | buflen = (size_t) output_size; |
| 237 | |
| 238 | TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result ); |
| 239 | if( result == 0) |
| 240 | { |
| 241 | |
Ronald Cron | 2dbba99 | 2020-06-10 11:42:32 +0200 | [diff] [blame] | 242 | TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x, |
| 243 | buflen, input_A->len ) == 0 ); |
Janos Follath | e344d0f | 2019-02-19 16:17:40 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | exit: |
| 247 | mbedtls_mpi_free( &X ); |
| 248 | } |
| 249 | /* END_CASE */ |
| 250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 252 | void mpi_read_file( char * input_file, data_t * input_A, int result ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 253 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | mbedtls_mpi X; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 255 | unsigned char buf[1000]; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 256 | size_t buflen; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 257 | FILE *file; |
Manuel Pégourié-Gonnard | e43187d | 2015-02-14 16:01:34 +0000 | [diff] [blame] | 258 | int ret; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 259 | |
| 260 | memset( buf, 0x00, 1000 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 261 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 263 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 264 | file = fopen( input_file, "r" ); |
Paul Bakker | 8a0c0a9 | 2014-04-17 16:08:20 +0200 | [diff] [blame] | 265 | TEST_ASSERT( file != NULL ); |
Werner Lewis | f65a327 | 2022-07-07 11:38:44 +0100 | [diff] [blame] | 266 | ret = mbedtls_mpi_read_file( &X, 16, file ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 267 | fclose(file); |
Manuel Pégourié-Gonnard | e43187d | 2015-02-14 16:01:34 +0000 | [diff] [blame] | 268 | TEST_ASSERT( ret == result ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 269 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 270 | if( result == 0 ) |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 271 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 272 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 273 | buflen = mbedtls_mpi_size( &X ); |
| 274 | TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 275 | |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 276 | |
Ronald Cron | 2dbba99 | 2020-06-10 11:42:32 +0200 | [diff] [blame] | 277 | TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x, |
| 278 | buflen, input_A->len ) == 0 ); |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 279 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 280 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 281 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 283 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 284 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 285 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 286 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 287 | void mpi_write_file( char * input_X, char * output_file ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 288 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | mbedtls_mpi X, Y; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 290 | FILE *file_out, *file_in; |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 291 | int ret; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 292 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 294 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 295 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 296 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 297 | file_out = fopen( output_file, "w" ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 298 | TEST_ASSERT( file_out != NULL ); |
Werner Lewis | f65a327 | 2022-07-07 11:38:44 +0100 | [diff] [blame] | 299 | ret = mbedtls_mpi_write_file( NULL, &X, 16, file_out ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 300 | fclose(file_out); |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 301 | TEST_ASSERT( ret == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 302 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 303 | file_in = fopen( output_file, "r" ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 304 | TEST_ASSERT( file_in != NULL ); |
Werner Lewis | f65a327 | 2022-07-07 11:38:44 +0100 | [diff] [blame] | 305 | ret = mbedtls_mpi_read_file( &Y, 16, file_in ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 306 | fclose(file_in); |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 307 | TEST_ASSERT( ret == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 308 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 310 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 311 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 313 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 314 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 315 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 316 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 317 | void mpi_get_bit( char * input_X, int pos, int val ) |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 318 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 319 | mbedtls_mpi X; |
| 320 | mbedtls_mpi_init( &X ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 321 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 322 | TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 323 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 324 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 325 | mbedtls_mpi_free( &X ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 326 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 327 | /* END_CASE */ |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 328 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 329 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 330 | void mpi_set_bit( char * input_X, int pos, int val, |
| 331 | char * output_Y, int result ) |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 332 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 333 | mbedtls_mpi X, Y; |
| 334 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 335 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 336 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 337 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, output_Y ) == 0 ); |
Paul Bakker | ec5ceb6 | 2016-07-14 12:47:07 +0100 | [diff] [blame] | 338 | TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result ); |
| 339 | |
| 340 | if( result == 0 ) |
| 341 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 342 | TEST_ASSERT( sign_is_valid( &X ) ); |
Paul Bakker | ec5ceb6 | 2016-07-14 12:47:07 +0100 | [diff] [blame] | 343 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); |
| 344 | } |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 345 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 346 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 348 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 349 | /* END_CASE */ |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 350 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 351 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 352 | void mpi_lsb( char * input_X, int nr_bits ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 353 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 354 | mbedtls_mpi X; |
| 355 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 356 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 357 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 358 | TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 359 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 360 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 361 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 362 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 363 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 364 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 365 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 366 | void mpi_bitlen( char * input_X, int nr_bits ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 367 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 368 | mbedtls_mpi X; |
| 369 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 370 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 371 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 372 | TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 373 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 374 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 375 | mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 376 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 377 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 378 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 379 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 380 | void mpi_gcd( char * input_X, char * input_Y, |
| 381 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 382 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 383 | mbedtls_mpi A, X, Y, Z; |
| 384 | mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 385 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 386 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 387 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 388 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 389 | TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 390 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 392 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 393 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 394 | mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 395 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 396 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 397 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 398 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 399 | void mpi_cmp_int( int input_X, int input_A, int result_CMP ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 400 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 401 | mbedtls_mpi X; |
| 402 | mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0); |
| 405 | TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 406 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 407 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 408 | mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 409 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 410 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 411 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 412 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 413 | void mpi_cmp_mpi( char * input_X, char * input_Y, |
| 414 | int input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 415 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | mbedtls_mpi X, Y; |
| 417 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 418 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 419 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 420 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 422 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 423 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 424 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 425 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 426 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 427 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 428 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 429 | void mpi_lt_mpi_ct( int size_X, char * input_X, |
| 430 | int size_Y, char * input_Y, |
| 431 | int input_ret, int input_err ) |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 432 | { |
Gilles Peskine | 0deccf1 | 2020-09-02 15:18:07 +0200 | [diff] [blame] | 433 | unsigned ret = -1; |
Janos Follath | 0e5532d | 2019-10-11 14:21:53 +0100 | [diff] [blame] | 434 | unsigned input_uret = input_ret; |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 435 | mbedtls_mpi X, Y; |
| 436 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
| 437 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 438 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 439 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 440 | |
Gilles Peskine | 9018b11 | 2020-01-21 16:30:53 +0100 | [diff] [blame] | 441 | TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 ); |
| 442 | TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 ); |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 443 | |
Janos Follath | 0e5532d | 2019-10-11 14:21:53 +0100 | [diff] [blame] | 444 | TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err ); |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 445 | if( input_err == 0 ) |
Janos Follath | 0e5532d | 2019-10-11 14:21:53 +0100 | [diff] [blame] | 446 | TEST_ASSERT( ret == input_uret ); |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 447 | |
| 448 | exit: |
| 449 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
| 450 | } |
| 451 | /* END_CASE */ |
| 452 | |
| 453 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 454 | void mpi_cmp_abs( char * input_X, char * input_Y, |
| 455 | int input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 456 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 457 | mbedtls_mpi X, Y; |
| 458 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 459 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 460 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 461 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 462 | TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 463 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 464 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 465 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 466 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 467 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 468 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 469 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 470 | void mpi_copy( char *src_hex, char *dst_hex ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 471 | { |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 472 | mbedtls_mpi src, dst, ref; |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 473 | mbedtls_mpi_init( &src ); |
| 474 | mbedtls_mpi_init( &dst ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 475 | mbedtls_mpi_init( &ref ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 476 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 477 | TEST_ASSERT( mbedtls_test_read_mpi( &src, src_hex ) == 0 ); |
| 478 | TEST_ASSERT( mbedtls_test_read_mpi( &ref, dst_hex ) == 0 ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 479 | |
| 480 | /* mbedtls_mpi_copy() */ |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 481 | TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 ); |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 482 | TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 ); |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 483 | TEST_ASSERT( sign_is_valid( &dst ) ); |
| 484 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 485 | |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 486 | /* mbedtls_mpi_safe_cond_assign(), assignment done */ |
| 487 | mbedtls_mpi_free( &dst ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 488 | TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 489 | TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 ); |
| 490 | TEST_ASSERT( sign_is_valid( &dst ) ); |
| 491 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 ); |
| 492 | |
| 493 | /* mbedtls_mpi_safe_cond_assign(), assignment not done */ |
| 494 | mbedtls_mpi_free( &dst ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 495 | TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 496 | TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 ); |
| 497 | TEST_ASSERT( sign_is_valid( &dst ) ); |
| 498 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 ); |
| 499 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 500 | exit: |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 501 | mbedtls_mpi_free( &src ); |
| 502 | mbedtls_mpi_free( &dst ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 503 | mbedtls_mpi_free( &ref ); |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 504 | } |
| 505 | /* END_CASE */ |
| 506 | |
| 507 | /* BEGIN_CASE */ |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 508 | void mpi_copy_self( char *input_X ) |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 509 | { |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 510 | mbedtls_mpi X, A; |
| 511 | mbedtls_mpi_init( &A ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 512 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 513 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 514 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 515 | TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 ); |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 516 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 517 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_X ) == 0 ); |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 518 | TEST_ASSERT( sign_is_valid( &X ) ); |
| 519 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 520 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 521 | exit: |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 522 | mbedtls_mpi_free( &A ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 523 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 524 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 525 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 526 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 527 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 528 | void mpi_swap( char *X_hex, char *Y_hex ) |
Gilles Peskine | fc1eeef | 2021-06-10 22:29:57 +0200 | [diff] [blame] | 529 | { |
| 530 | mbedtls_mpi X, Y, X0, Y0; |
| 531 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
| 532 | mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 ); |
| 533 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 534 | TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 ); |
| 535 | TEST_ASSERT( mbedtls_test_read_mpi( &Y0, Y_hex ) == 0 ); |
Gilles Peskine | fc1eeef | 2021-06-10 22:29:57 +0200 | [diff] [blame] | 536 | |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 537 | /* mbedtls_mpi_swap() */ |
Tom Cosgrove | c71ca0c | 2022-09-15 15:38:17 +0100 | [diff] [blame] | 538 | TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 ); |
| 539 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 ); |
Gilles Peskine | fc1eeef | 2021-06-10 22:29:57 +0200 | [diff] [blame] | 540 | mbedtls_mpi_swap( &X, &Y ); |
| 541 | TEST_ASSERT( sign_is_valid( &X ) ); |
| 542 | TEST_ASSERT( sign_is_valid( &Y ) ); |
| 543 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 ); |
| 544 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 ); |
| 545 | |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 546 | /* mbedtls_mpi_safe_cond_swap(), swap done */ |
| 547 | mbedtls_mpi_free( &X ); |
| 548 | mbedtls_mpi_free( &Y ); |
Tom Cosgrove | c71ca0c | 2022-09-15 15:38:17 +0100 | [diff] [blame] | 549 | TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 ); |
| 550 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 551 | TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 ); |
| 552 | TEST_ASSERT( sign_is_valid( &X ) ); |
| 553 | TEST_ASSERT( sign_is_valid( &Y ) ); |
| 554 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 ); |
| 555 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 ); |
| 556 | |
| 557 | /* mbedtls_mpi_safe_cond_swap(), swap not done */ |
| 558 | mbedtls_mpi_free( &X ); |
| 559 | mbedtls_mpi_free( &Y ); |
Tom Cosgrove | c71ca0c | 2022-09-15 15:38:17 +0100 | [diff] [blame] | 560 | TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 ); |
| 561 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 562 | TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 ); |
| 563 | TEST_ASSERT( sign_is_valid( &X ) ); |
| 564 | TEST_ASSERT( sign_is_valid( &Y ) ); |
| 565 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 ); |
| 566 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 ); |
| 567 | |
Gilles Peskine | fc1eeef | 2021-06-10 22:29:57 +0200 | [diff] [blame] | 568 | exit: |
| 569 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
| 570 | mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 ); |
| 571 | } |
| 572 | /* END_CASE */ |
| 573 | |
| 574 | /* BEGIN_CASE */ |
| 575 | void mpi_swap_self( char *X_hex ) |
| 576 | { |
| 577 | mbedtls_mpi X, X0; |
| 578 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 ); |
| 579 | |
Tom Cosgrove | c71ca0c | 2022-09-15 15:38:17 +0100 | [diff] [blame] | 580 | TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 581 | TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 ); |
Gilles Peskine | fc1eeef | 2021-06-10 22:29:57 +0200 | [diff] [blame] | 582 | |
| 583 | mbedtls_mpi_swap( &X, &X ); |
| 584 | TEST_ASSERT( sign_is_valid( &X ) ); |
| 585 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 ); |
| 586 | |
| 587 | exit: |
| 588 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 ); |
| 589 | } |
| 590 | /* END_CASE */ |
| 591 | |
| 592 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 593 | void mpi_shrink( int before, int used, int min, int after ) |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 594 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 595 | mbedtls_mpi X; |
| 596 | mbedtls_mpi_init( &X ); |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 597 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 598 | TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 ); |
Gilles Peskine | e109175 | 2021-06-15 21:19:18 +0200 | [diff] [blame] | 599 | if( used > 0 ) |
| 600 | { |
| 601 | size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint ); |
| 602 | TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 ); |
| 603 | } |
| 604 | TEST_EQUAL( X.n, (size_t) before ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 605 | TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 ); |
Gilles Peskine | e109175 | 2021-06-15 21:19:18 +0200 | [diff] [blame] | 606 | TEST_EQUAL( X.n, (size_t) after ); |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 607 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 608 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 609 | mbedtls_mpi_free( &X ); |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 610 | } |
| 611 | /* END_CASE */ |
| 612 | |
| 613 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 614 | void mpi_add_mpi( char * input_X, char * input_Y, |
| 615 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 616 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 617 | mbedtls_mpi X, Y, Z, A; |
| 618 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 619 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 620 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 621 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 622 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 624 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 626 | |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 627 | /* result == first operand */ |
| 628 | TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 629 | TEST_ASSERT( sign_is_valid( &X ) ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 630 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 631 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 632 | |
| 633 | /* result == second operand */ |
| 634 | TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 635 | TEST_ASSERT( sign_is_valid( &Y ) ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 636 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); |
| 637 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 638 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 639 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 640 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 641 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 642 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 643 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 644 | void mpi_add_mpi_inplace( char * input_X, char * input_A ) |
Janos Follath | 044a86b | 2015-10-25 10:58:03 +0100 | [diff] [blame] | 645 | { |
| 646 | mbedtls_mpi X, A; |
| 647 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); |
| 648 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 649 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Janos Follath | 6cbacec | 2015-10-25 12:29:13 +0100 | [diff] [blame] | 650 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 651 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Janos Follath | 6cbacec | 2015-10-25 12:29:13 +0100 | [diff] [blame] | 652 | TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 ); |
| 653 | TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 654 | TEST_ASSERT( sign_is_valid( &X ) ); |
Janos Follath | 6cbacec | 2015-10-25 12:29:13 +0100 | [diff] [blame] | 655 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 656 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Janos Follath | 6cbacec | 2015-10-25 12:29:13 +0100 | [diff] [blame] | 657 | TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 658 | TEST_ASSERT( sign_is_valid( &X ) ); |
Janos Follath | 6cbacec | 2015-10-25 12:29:13 +0100 | [diff] [blame] | 659 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
| 660 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 661 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Janos Follath | 044a86b | 2015-10-25 10:58:03 +0100 | [diff] [blame] | 662 | TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 663 | TEST_ASSERT( sign_is_valid( &X ) ); |
Janos Follath | 044a86b | 2015-10-25 10:58:03 +0100 | [diff] [blame] | 664 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
| 665 | |
| 666 | exit: |
| 667 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); |
| 668 | } |
| 669 | /* END_CASE */ |
| 670 | |
| 671 | |
| 672 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 673 | void mpi_add_abs( char * input_X, char * input_Y, |
| 674 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 675 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 676 | mbedtls_mpi X, Y, Z, A; |
| 677 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 678 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 679 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 680 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 681 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 682 | TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 683 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 684 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 685 | |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 686 | /* result == first operand */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 687 | TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 688 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 689 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 690 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 691 | |
| 692 | /* result == second operand */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 693 | TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 694 | TEST_ASSERT( sign_is_valid( &Y ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 695 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 696 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 697 | exit: |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 698 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 699 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 700 | /* END_CASE */ |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 701 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 702 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 703 | void mpi_add_int( char * input_X, int input_Y, |
| 704 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 705 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 706 | mbedtls_mpi X, Z, A; |
| 707 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 708 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 709 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 710 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 711 | TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 712 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 713 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 714 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 715 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 716 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 717 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 718 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 719 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 720 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 721 | void mpi_sub_mpi( char * input_X, char * input_Y, |
| 722 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 723 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | mbedtls_mpi X, Y, Z, A; |
| 725 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 726 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 727 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 728 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 729 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 730 | TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 731 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 732 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 733 | |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 734 | /* result == first operand */ |
| 735 | TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 736 | TEST_ASSERT( sign_is_valid( &X ) ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 737 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 738 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 739 | |
| 740 | /* result == second operand */ |
| 741 | TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 742 | TEST_ASSERT( sign_is_valid( &Y ) ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 743 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); |
| 744 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 745 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 746 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 747 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 748 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 749 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 750 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 751 | void mpi_sub_abs( char * input_X, char * input_Y, |
| 752 | char * input_A, int sub_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 753 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 754 | mbedtls_mpi X, Y, Z, A; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 755 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 756 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 757 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 758 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 759 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 760 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | e670f90 | 2015-10-30 09:23:19 +0100 | [diff] [blame] | 761 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 762 | res = mbedtls_mpi_sub_abs( &Z, &X, &Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 763 | TEST_ASSERT( res == sub_result ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 764 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 765 | if( res == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 766 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 767 | |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 768 | /* result == first operand */ |
| 769 | TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 770 | TEST_ASSERT( sign_is_valid( &X ) ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 771 | if( sub_result == 0 ) |
| 772 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 773 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 774 | |
| 775 | /* result == second operand */ |
| 776 | TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 777 | TEST_ASSERT( sign_is_valid( &Y ) ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 778 | if( sub_result == 0 ) |
| 779 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); |
| 780 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 781 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 782 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 783 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 784 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 785 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 786 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 787 | void mpi_sub_int( char * input_X, int input_Y, |
| 788 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 789 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 790 | mbedtls_mpi X, Z, A; |
| 791 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 792 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 793 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 794 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 795 | TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 796 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 797 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 798 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 799 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 800 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 801 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 802 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 803 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 804 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 805 | void mpi_mul_mpi( char * input_X, char * input_Y, |
| 806 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 807 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 808 | mbedtls_mpi X, Y, Z, A; |
| 809 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 810 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 811 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 812 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 813 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 814 | TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 815 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 817 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 818 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 819 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 820 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 821 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 822 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 823 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 824 | void mpi_mul_int( char * input_X, int input_Y, |
| 825 | char * input_A, char * result_comparison ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 826 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 827 | mbedtls_mpi X, Z, A; |
| 828 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 829 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 830 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 831 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 832 | TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 833 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 834 | if( strcmp( result_comparison, "==" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 835 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 836 | else if( strcmp( result_comparison, "!=" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 837 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 838 | else |
| 839 | TEST_ASSERT( "unknown operator" == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 840 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 841 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 842 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 843 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 844 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 845 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 846 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 847 | void mpi_div_mpi( char * input_X, char * input_Y, |
| 848 | char * input_A, char * input_B, |
| 849 | int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 850 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 851 | mbedtls_mpi X, Y, Q, R, A, B; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 852 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 853 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); |
| 854 | mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 855 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 856 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 857 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 858 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
| 859 | TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 860 | res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 861 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 862 | if( res == 0 ) |
| 863 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 864 | TEST_ASSERT( sign_is_valid( &Q ) ); |
| 865 | TEST_ASSERT( sign_is_valid( &R ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 866 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 ); |
| 867 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 868 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 869 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 870 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 871 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); |
| 872 | mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 873 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 874 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 875 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 876 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 877 | void mpi_div_int( char * input_X, int input_Y, |
| 878 | char * input_A, char * input_B, |
| 879 | int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 880 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 881 | mbedtls_mpi X, Q, R, A, B; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 882 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 883 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A ); |
| 884 | mbedtls_mpi_init( &B ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 885 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 886 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 887 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
| 888 | TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 889 | res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 890 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 891 | if( res == 0 ) |
| 892 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 893 | TEST_ASSERT( sign_is_valid( &Q ) ); |
| 894 | TEST_ASSERT( sign_is_valid( &R ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 895 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 ); |
| 896 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 897 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 898 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 899 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 900 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A ); |
| 901 | mbedtls_mpi_free( &B ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 902 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 903 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 904 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 905 | /* BEGIN_CASE */ |
Werner Lewis | 6baf12b | 2022-10-19 12:46:35 +0100 | [diff] [blame] | 906 | void mpi_mod_mpi( char * input_X, char * input_Y, |
| 907 | char * input_A, int div_result ) |
| 908 | { |
| 909 | mbedtls_mpi X, Y, A; |
| 910 | int res; |
| 911 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A ); |
| 912 | |
| 913 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 914 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 915 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
| 916 | res = mbedtls_mpi_mod_mpi( &X, &X, &Y ); |
| 917 | TEST_ASSERT( res == div_result ); |
| 918 | if( res == 0 ) |
| 919 | { |
| 920 | TEST_ASSERT( sign_is_valid( &X ) ); |
| 921 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
| 922 | } |
| 923 | |
| 924 | exit: |
| 925 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A ); |
| 926 | } |
| 927 | /* END_CASE */ |
| 928 | |
| 929 | /* BEGIN_CASE */ |
Tom Cosgrove | 91e35e3 | 2022-11-09 11:45:29 +0000 | [diff] [blame] | 930 | void mpi_mod_int( char * input_X, char * input_Y, |
| 931 | char * input_A, int mod_result ) |
Werner Lewis | 6baf12b | 2022-10-19 12:46:35 +0100 | [diff] [blame] | 932 | { |
| 933 | mbedtls_mpi X; |
Tom Cosgrove | 91e35e3 | 2022-11-09 11:45:29 +0000 | [diff] [blame] | 934 | mbedtls_mpi Y; |
| 935 | mbedtls_mpi A; |
Werner Lewis | 6baf12b | 2022-10-19 12:46:35 +0100 | [diff] [blame] | 936 | int res; |
| 937 | mbedtls_mpi_uint r; |
Werner Lewis | 6baf12b | 2022-10-19 12:46:35 +0100 | [diff] [blame] | 938 | |
Tom Cosgrove | 91e35e3 | 2022-11-09 11:45:29 +0000 | [diff] [blame] | 939 | mbedtls_mpi_init( &X ); |
| 940 | mbedtls_mpi_init( &Y ); |
| 941 | mbedtls_mpi_init( &A ); |
| 942 | |
| 943 | /* We use MPIs to read Y and A since the test framework limits us to |
| 944 | * ints, so we can't have 64-bit values */ |
| 945 | TEST_EQUAL( mbedtls_test_read_mpi( &X, input_X ), 0 ); |
| 946 | TEST_EQUAL( mbedtls_test_read_mpi( &Y, input_Y ), 0 ); |
| 947 | TEST_EQUAL( mbedtls_test_read_mpi( &A, input_A ), 0 ); |
| 948 | |
| 949 | TEST_EQUAL( Y.n, 1 ); |
| 950 | TEST_EQUAL( A.n, 1 ); |
| 951 | |
Tom Cosgrove | 9feb19f | 2022-11-10 12:05:55 +0000 | [diff] [blame] | 952 | /* Convert the MPIs for Y and A to (signed) mbedtls_mpi_sints */ |
| 953 | |
| 954 | /* Since we're converting sign+magnitude to two's complement, we lose one |
| 955 | * bit of value in the output. This means there are some values we can't |
| 956 | * represent, e.g. (hex) -A0000000 on 32-bit systems. These are technically |
| 957 | * invalid test cases, so could be considered "won't happen", but they are |
| 958 | * easy to test for, and this helps guard against human error. */ |
| 959 | |
| 960 | mbedtls_mpi_sint y = (mbedtls_mpi_sint) Y.p[0]; |
| 961 | TEST_ASSERT( y >= 0 ); /* If y < 0 here, we can't make negative y */ |
Tom Cosgrove | 91e35e3 | 2022-11-09 11:45:29 +0000 | [diff] [blame] | 962 | if( Y.s == -1 ) |
| 963 | y = -y; |
Tom Cosgrove | 9feb19f | 2022-11-10 12:05:55 +0000 | [diff] [blame] | 964 | |
| 965 | mbedtls_mpi_sint a = (mbedtls_mpi_sint) A.p[0]; |
| 966 | TEST_ASSERT( a >= 0 ); /* Same goes for a */ |
Tom Cosgrove | 91e35e3 | 2022-11-09 11:45:29 +0000 | [diff] [blame] | 967 | if( A.s == -1 ) |
| 968 | a = -a; |
| 969 | |
| 970 | res = mbedtls_mpi_mod_int( &r, &X, y ); |
| 971 | TEST_EQUAL( res, mod_result ); |
Werner Lewis | 6baf12b | 2022-10-19 12:46:35 +0100 | [diff] [blame] | 972 | if( res == 0 ) |
| 973 | { |
Tom Cosgrove | 91e35e3 | 2022-11-09 11:45:29 +0000 | [diff] [blame] | 974 | TEST_EQUAL( r, a ); |
Werner Lewis | 6baf12b | 2022-10-19 12:46:35 +0100 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | exit: |
| 978 | mbedtls_mpi_free( &X ); |
Tom Cosgrove | 91e35e3 | 2022-11-09 11:45:29 +0000 | [diff] [blame] | 979 | mbedtls_mpi_free( &Y ); |
| 980 | mbedtls_mpi_free( &A ); |
Werner Lewis | 6baf12b | 2022-10-19 12:46:35 +0100 | [diff] [blame] | 981 | } |
| 982 | /* END_CASE */ |
| 983 | |
| 984 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 985 | void mpi_exp_mod( char * input_A, char * input_E, |
| 986 | char * input_N, char * input_X, |
| 987 | int exp_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 988 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 989 | mbedtls_mpi A, E, N, RR, Z, X; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 990 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 991 | mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); |
| 992 | mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 993 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 994 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
| 995 | TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 ); |
| 996 | TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 ); |
| 997 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 998 | |
Gilles Peskine | 342f71b | 2021-06-09 18:31:35 +0200 | [diff] [blame] | 999 | res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL ); |
Gilles Peskine | 722c62c | 2021-06-15 21:55:05 +0200 | [diff] [blame] | 1000 | TEST_ASSERT( res == exp_result ); |
Gilles Peskine | 342f71b | 2021-06-09 18:31:35 +0200 | [diff] [blame] | 1001 | if( res == 0 ) |
| 1002 | { |
| 1003 | TEST_ASSERT( sign_is_valid( &Z ) ); |
| 1004 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 ); |
| 1005 | } |
| 1006 | |
| 1007 | /* Now test again with the speed-up parameter supplied as an output. */ |
| 1008 | res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ); |
Gilles Peskine | 722c62c | 2021-06-15 21:55:05 +0200 | [diff] [blame] | 1009 | TEST_ASSERT( res == exp_result ); |
Gilles Peskine | 342f71b | 2021-06-09 18:31:35 +0200 | [diff] [blame] | 1010 | if( res == 0 ) |
| 1011 | { |
| 1012 | TEST_ASSERT( sign_is_valid( &Z ) ); |
| 1013 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 ); |
| 1014 | } |
| 1015 | |
| 1016 | /* Now test again with the speed-up parameter supplied in calculated form. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1017 | res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ); |
Gilles Peskine | 722c62c | 2021-06-15 21:55:05 +0200 | [diff] [blame] | 1018 | TEST_ASSERT( res == exp_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1019 | if( res == 0 ) |
| 1020 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1021 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1022 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1023 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1024 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1025 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1026 | mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); |
| 1027 | mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1028 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1029 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1030 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1031 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1032 | void mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes, |
| 1033 | char * input_RR, int exp_result ) |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1034 | { |
| 1035 | mbedtls_mpi A, E, N, RR, Z; |
| 1036 | mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); |
| 1037 | mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); |
| 1038 | |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1039 | /* Set A to 2^(A_bytes - 1) + 1 */ |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1040 | TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1041 | TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1042 | TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 ); |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1043 | |
| 1044 | /* Set E to 2^(E_bytes - 1) + 1 */ |
| 1045 | TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 ); |
| 1046 | TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1047 | TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 ); |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1048 | |
| 1049 | /* Set N to 2^(N_bytes - 1) + 1 */ |
| 1050 | TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 ); |
| 1051 | TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1052 | TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 ); |
| 1053 | |
| 1054 | if( strlen( input_RR ) ) |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1055 | TEST_ASSERT( mbedtls_test_read_mpi( &RR, input_RR ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1056 | |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1057 | TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1058 | |
| 1059 | exit: |
| 1060 | mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); |
| 1061 | mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); |
| 1062 | } |
| 1063 | /* END_CASE */ |
| 1064 | |
| 1065 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1066 | void mpi_inv_mod( char * input_X, char * input_Y, |
| 1067 | char * input_A, int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1068 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1069 | mbedtls_mpi X, Y, Z, A; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1070 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1071 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1072 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1073 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1074 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 1075 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1076 | res = mbedtls_mpi_inv_mod( &Z, &X, &Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1077 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1078 | if( res == 0 ) |
| 1079 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1080 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1081 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1082 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1083 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1084 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1085 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1086 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1087 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1088 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1090 | void mpi_is_prime( char * input_X, int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1091 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1092 | mbedtls_mpi X; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1093 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1094 | mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1095 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1096 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1097 | res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1098 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1099 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1100 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1101 | mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1102 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1103 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1104 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1105 | /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1106 | void mpi_is_prime_det( data_t * input_X, data_t * witnesses, |
| 1107 | int chunk_len, int rounds ) |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 1108 | { |
| 1109 | mbedtls_mpi X; |
| 1110 | int res; |
| 1111 | mbedtls_test_mpi_random rand; |
| 1112 | |
| 1113 | mbedtls_mpi_init( &X ); |
| 1114 | rand.data = witnesses; |
| 1115 | rand.pos = 0; |
| 1116 | rand.chunk_len = chunk_len; |
| 1117 | |
| 1118 | TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 ); |
Darryl Green | ac2ead0 | 2018-10-02 15:30:39 +0100 | [diff] [blame] | 1119 | res = mbedtls_mpi_is_prime_ext( &X, rounds - 1, |
| 1120 | mbedtls_test_mpi_miller_rabin_determinizer, |
| 1121 | &rand ); |
| 1122 | TEST_ASSERT( res == 0 ); |
| 1123 | |
| 1124 | rand.data = witnesses; |
| 1125 | rand.pos = 0; |
| 1126 | rand.chunk_len = chunk_len; |
| 1127 | |
Janos Follath | a0b67c2 | 2018-09-18 14:48:23 +0100 | [diff] [blame] | 1128 | res = mbedtls_mpi_is_prime_ext( &X, rounds, |
| 1129 | mbedtls_test_mpi_miller_rabin_determinizer, |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 1130 | &rand ); |
Darryl Green | ac2ead0 | 2018-10-02 15:30:39 +0100 | [diff] [blame] | 1131 | TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE ); |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 1132 | |
| 1133 | exit: |
| 1134 | mbedtls_mpi_free( &X ); |
| 1135 | } |
| 1136 | /* END_CASE */ |
| 1137 | |
| 1138 | /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1139 | void mpi_gen_prime( int bits, int flags, int ref_ret ) |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1140 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1141 | mbedtls_mpi X; |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1142 | int my_ret; |
| 1143 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1144 | mbedtls_mpi_init( &X ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1145 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1146 | my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, |
| 1147 | mbedtls_test_rnd_std_rand, NULL ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1148 | TEST_ASSERT( my_ret == ref_ret ); |
| 1149 | |
| 1150 | if( ref_ret == 0 ) |
| 1151 | { |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 1152 | size_t actual_bits = mbedtls_mpi_bitlen( &X ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1153 | |
| 1154 | TEST_ASSERT( actual_bits >= (size_t) bits ); |
| 1155 | TEST_ASSERT( actual_bits <= (size_t) bits + 1 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1156 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1157 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1158 | TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, |
| 1159 | mbedtls_test_rnd_std_rand, |
| 1160 | NULL ) == 0 ); |
Janos Follath | a3cb7eb | 2018-08-14 15:31:54 +0100 | [diff] [blame] | 1161 | if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1162 | { |
Hanno Becker | d4d6057 | 2018-01-10 07:12:01 +0000 | [diff] [blame] | 1163 | /* X = ( X - 1 ) / 2 */ |
| 1164 | TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 ); |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1165 | TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, |
| 1166 | mbedtls_test_rnd_std_rand, |
| 1167 | NULL ) == 0 ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1171 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1172 | mbedtls_mpi_free( &X ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1173 | } |
| 1174 | /* END_CASE */ |
| 1175 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1176 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1177 | void mpi_shift_l( char * input_X, int shift_X, |
| 1178 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1179 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1180 | mbedtls_mpi X, A; |
| 1181 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1182 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1183 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1184 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1185 | TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1186 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1187 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1188 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1189 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1190 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1191 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1192 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1193 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1194 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1195 | void mpi_shift_r( char * input_X, int shift_X, |
| 1196 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1197 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1198 | mbedtls_mpi X, A; |
| 1199 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1200 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1201 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1202 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1203 | TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1204 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1205 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1206 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1207 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1208 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1209 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1210 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1211 | |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 1212 | /* BEGIN_CASE */ |
Gilles Peskine | 422e867 | 2021-04-02 00:02:27 +0200 | [diff] [blame] | 1213 | void mpi_fill_random( int wanted_bytes, int rng_bytes, |
| 1214 | int before, int expected_ret ) |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 1215 | { |
| 1216 | mbedtls_mpi X; |
| 1217 | int ret; |
| 1218 | size_t bytes_left = rng_bytes; |
| 1219 | mbedtls_mpi_init( &X ); |
| 1220 | |
Gilles Peskine | 422e867 | 2021-04-02 00:02:27 +0200 | [diff] [blame] | 1221 | if( before != 0 ) |
| 1222 | { |
| 1223 | /* Set X to sign(before) * 2^(|before|-1) */ |
| 1224 | TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 ); |
| 1225 | if( before < 0 ) |
| 1226 | before = - before; |
| 1227 | TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 ); |
| 1228 | } |
| 1229 | |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 1230 | ret = mbedtls_mpi_fill_random( &X, wanted_bytes, |
| 1231 | f_rng_bytes_left, &bytes_left ); |
| 1232 | TEST_ASSERT( ret == expected_ret ); |
| 1233 | |
| 1234 | if( expected_ret == 0 ) |
| 1235 | { |
| 1236 | /* mbedtls_mpi_fill_random is documented to use bytes from the RNG |
| 1237 | * as a big-endian representation of the number. We know when |
| 1238 | * our RNG function returns null bytes, so we know how many |
| 1239 | * leading zero bytes the number has. */ |
| 1240 | size_t leading_zeros = 0; |
| 1241 | if( wanted_bytes > 0 && rng_bytes % 256 == 0 ) |
| 1242 | leading_zeros = 1; |
| 1243 | TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros == |
| 1244 | (size_t) wanted_bytes ); |
| 1245 | TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1246 | TEST_ASSERT( sign_is_valid( &X ) ); |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | exit: |
| 1250 | mbedtls_mpi_free( &X ); |
| 1251 | } |
| 1252 | /* END_CASE */ |
| 1253 | |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 1254 | /* BEGIN_CASE */ |
Gilles Peskine | af601f9 | 2022-11-15 23:02:14 +0100 | [diff] [blame] | 1255 | void most_negative_mpi_sint( ) |
| 1256 | { |
| 1257 | /* Ad hoc tests for n = -p = -2^(biL-1) as a mbedtls_mpi_sint. We |
| 1258 | * guarantee that mbedtls_mpi_sint is a two's complement type, so this |
| 1259 | * is a valid value. However, negating it (`-n`) has undefined behavior |
| 1260 | * (although in practice `-n` evaluates to the value n). |
| 1261 | * |
| 1262 | * This function has ad hoc tests for this value. It's separated from other |
| 1263 | * functions because the test framework makes it hard to pass this value |
| 1264 | * into test cases. |
| 1265 | * |
| 1266 | * In the comments here: |
| 1267 | * - biL = number of bits in limbs |
| 1268 | * - p = 2^(biL-1) (smallest positive value not in mbedtls_mpi_sint range) |
| 1269 | * - n = -2^(biL-1) (largest negative value in mbedtls_mpi_sint range) |
| 1270 | */ |
| 1271 | |
| 1272 | mbedtls_mpi A, R, X; |
| 1273 | mbedtls_mpi_init( &A ); |
| 1274 | mbedtls_mpi_init( &R ); |
| 1275 | mbedtls_mpi_init( &X ); |
| 1276 | |
Gilles Peskine | af601f9 | 2022-11-15 23:02:14 +0100 | [diff] [blame] | 1277 | mbedtls_mpi_uint most_positive_plus_1 = (mbedtls_mpi_uint) 1 << ( biL - 1 ); |
| 1278 | const mbedtls_mpi_sint most_positive = most_positive_plus_1 - 1; |
| 1279 | const mbedtls_mpi_sint most_negative = - most_positive - 1; |
| 1280 | TEST_EQUAL( (mbedtls_mpi_uint) most_negative, |
| 1281 | (mbedtls_mpi_uint) 1 << ( biL - 1 ) ); |
| 1282 | TEST_EQUAL( (mbedtls_mpi_uint) most_negative << 1, 0 ); |
| 1283 | |
| 1284 | /* Test mbedtls_mpi_lset() */ |
| 1285 | TEST_EQUAL( mbedtls_mpi_lset( &A, most_negative ), 0 ); |
| 1286 | TEST_EQUAL( A.s, -1 ); |
| 1287 | TEST_EQUAL( A.n, 1 ); |
| 1288 | TEST_EQUAL( A.p[0], most_positive_plus_1 ); |
| 1289 | |
| 1290 | /* Test mbedtls_mpi_cmp_int(): -p == -p */ |
| 1291 | TEST_EQUAL( mbedtls_mpi_cmp_int( &A, most_negative ), 0 ); |
| 1292 | |
| 1293 | /* Test mbedtls_mpi_cmp_int(): -(p+1) < -p */ |
| 1294 | A.p[0] = most_positive_plus_1 + 1; |
| 1295 | TEST_EQUAL( mbedtls_mpi_cmp_int( &A, most_negative ), -1 ); |
| 1296 | |
| 1297 | /* Test mbedtls_mpi_cmp_int(): -(p-1) > -p */ |
| 1298 | A.p[0] = most_positive_plus_1 - 1; |
| 1299 | TEST_EQUAL( mbedtls_mpi_cmp_int( &A, most_negative ), 1 ); |
| 1300 | |
| 1301 | /* Test mbedtls_mpi_add_int(): (p-1) + (-p) */ |
| 1302 | TEST_EQUAL( mbedtls_mpi_lset( &A, most_positive ), 0 ); |
| 1303 | TEST_EQUAL( mbedtls_mpi_add_int( &X, &A, most_negative ), 0 ); |
| 1304 | TEST_EQUAL( mbedtls_mpi_cmp_int( &X, -1 ), 0 ); |
| 1305 | |
| 1306 | /* Test mbedtls_mpi_add_int(): (0) + (-p) */ |
| 1307 | TEST_EQUAL( mbedtls_mpi_lset( &A, 0 ), 0 ); |
| 1308 | TEST_EQUAL( mbedtls_mpi_add_int( &X, &A, most_negative ), 0 ); |
| 1309 | TEST_EQUAL( mbedtls_mpi_cmp_int( &X, most_negative ), 0 ); |
| 1310 | |
| 1311 | /* Test mbedtls_mpi_add_int(): (-p) + (-p) */ |
| 1312 | TEST_EQUAL( mbedtls_mpi_lset( &A, most_negative ), 0 ); |
| 1313 | TEST_EQUAL( mbedtls_mpi_add_int( &X, &A, most_negative ), 0 ); |
| 1314 | TEST_EQUAL( X.s, -1 ); |
| 1315 | TEST_EQUAL( X.n, 2 ); |
| 1316 | TEST_EQUAL( X.p[0], 0 ); |
| 1317 | TEST_EQUAL( X.p[1], 1 ); |
| 1318 | |
| 1319 | /* Test mbedtls_mpi_sub_int(): (p) - (-p) */ |
| 1320 | mbedtls_mpi_free( &X ); |
| 1321 | TEST_EQUAL( mbedtls_mpi_lset( &A, most_positive ), 0 ); |
| 1322 | TEST_EQUAL( mbedtls_mpi_sub_int( &X, &A, most_negative ), 0 ); |
| 1323 | TEST_EQUAL( X.s, 1 ); |
| 1324 | TEST_EQUAL( X.n, 1 ); |
| 1325 | TEST_EQUAL( X.p[0], ~(mbedtls_mpi_uint)0 ); |
| 1326 | |
| 1327 | /* Test mbedtls_mpi_sub_int(): (0) - (-p) */ |
| 1328 | TEST_EQUAL( mbedtls_mpi_lset( &A, 0 ), 0 ); |
| 1329 | TEST_EQUAL( mbedtls_mpi_sub_int( &X, &A, most_negative ), 0 ); |
| 1330 | TEST_EQUAL( X.s, 1 ); |
| 1331 | TEST_EQUAL( X.n, 1 ); |
| 1332 | TEST_EQUAL( X.p[0], most_positive_plus_1 ); |
| 1333 | |
| 1334 | /* Test mbedtls_mpi_sub_int(): (-p) - (-p) */ |
| 1335 | TEST_EQUAL( mbedtls_mpi_lset( &A, most_negative ), 0 ); |
| 1336 | TEST_EQUAL( mbedtls_mpi_sub_int( &X, &A, most_negative ), 0 ); |
| 1337 | TEST_EQUAL( mbedtls_mpi_cmp_int( &X, 0 ), 0 ); |
| 1338 | |
| 1339 | /* Test mbedtls_mpi_div_int(): (-p+1) / (-p) */ |
| 1340 | TEST_EQUAL( mbedtls_mpi_lset( &A, -most_positive ), 0 ); |
| 1341 | TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 ); |
| 1342 | TEST_EQUAL( mbedtls_mpi_cmp_int( &X, 0 ), 0 ); |
| 1343 | TEST_EQUAL( mbedtls_mpi_cmp_int( &R, -most_positive ), 0 ); |
| 1344 | |
| 1345 | /* Test mbedtls_mpi_div_int(): (-p) / (-p) */ |
| 1346 | TEST_EQUAL( mbedtls_mpi_lset( &A, most_negative ), 0 ); |
| 1347 | TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 ); |
| 1348 | TEST_EQUAL( mbedtls_mpi_cmp_int( &X, 1 ), 0 ); |
| 1349 | TEST_EQUAL( mbedtls_mpi_cmp_int( &R, 0 ), 0 ); |
| 1350 | |
| 1351 | /* Test mbedtls_mpi_div_int(): (-2*p) / (-p) */ |
| 1352 | TEST_EQUAL( mbedtls_mpi_shift_l( &A, 1 ), 0 ); |
| 1353 | TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 ); |
| 1354 | TEST_EQUAL( mbedtls_mpi_cmp_int( &X, 2 ), 0 ); |
| 1355 | TEST_EQUAL( mbedtls_mpi_cmp_int( &R, 0 ), 0 ); |
| 1356 | |
| 1357 | /* Test mbedtls_mpi_div_int(): (-2*p+1) / (-p) */ |
| 1358 | TEST_EQUAL( mbedtls_mpi_add_int( &A, &A, 1 ), 0 ); |
| 1359 | TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 ); |
| 1360 | TEST_EQUAL( mbedtls_mpi_cmp_int( &X, 1 ), 0 ); |
| 1361 | TEST_EQUAL( mbedtls_mpi_cmp_int( &R, -most_positive ), 0 ); |
| 1362 | |
| 1363 | /* Test mbedtls_mpi_div_int(): (p-1) / (-p) */ |
| 1364 | TEST_EQUAL( mbedtls_mpi_lset( &A, most_positive ), 0 ); |
| 1365 | TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 ); |
| 1366 | TEST_EQUAL( mbedtls_mpi_cmp_int( &X, 0 ), 0 ); |
| 1367 | TEST_EQUAL( mbedtls_mpi_cmp_int( &R, most_positive ), 0 ); |
| 1368 | |
| 1369 | /* Test mbedtls_mpi_div_int(): (p) / (-p) */ |
| 1370 | TEST_EQUAL( mbedtls_mpi_add_int( &A, &A, 1 ), 0 ); |
| 1371 | TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 ); |
| 1372 | TEST_EQUAL( mbedtls_mpi_cmp_int( &X, -1 ), 0 ); |
| 1373 | TEST_EQUAL( mbedtls_mpi_cmp_int( &R, 0 ), 0 ); |
| 1374 | |
| 1375 | /* Test mbedtls_mpi_div_int(): (2*p) / (-p) */ |
| 1376 | TEST_EQUAL( mbedtls_mpi_shift_l( &A, 1 ), 0 ); |
| 1377 | TEST_EQUAL( mbedtls_mpi_div_int( &X, &R, &A, most_negative ), 0 ); |
| 1378 | TEST_EQUAL( mbedtls_mpi_cmp_int( &X, -2 ), 0 ); |
| 1379 | TEST_EQUAL( mbedtls_mpi_cmp_int( &R, 0 ), 0 ); |
| 1380 | |
| 1381 | /* Test mbedtls_mpi_mod_int(): never valid */ |
| 1382 | TEST_EQUAL( mbedtls_mpi_mod_int( X.p, &A, most_negative ), |
| 1383 | MBEDTLS_ERR_MPI_NEGATIVE_VALUE ); |
| 1384 | |
| 1385 | /* Test mbedtls_mpi_random(): never valid */ |
| 1386 | TEST_EQUAL( mbedtls_mpi_random( &X, most_negative, &A, |
| 1387 | mbedtls_test_rnd_std_rand, NULL ), |
| 1388 | MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 1389 | |
| 1390 | exit: |
| 1391 | mbedtls_mpi_free( &A ); |
| 1392 | mbedtls_mpi_free( &R ); |
| 1393 | mbedtls_mpi_free( &X ); |
| 1394 | } |
| 1395 | /* END_CASE */ |
| 1396 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1397 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1398 | void mpi_selftest( ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 1399 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 1400 | TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 1401 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1402 | /* END_CASE */ |