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 | f1d617d | 2022-07-21 09:29:32 +0100 | [diff] [blame] | 4 | #include "bignum_core.h" |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 5 | #include "bignum_mod.h" |
| 6 | #include "bignum_mod_raw.h" |
Janos Follath | 23bdeca | 2022-07-22 18:24:06 +0100 | [diff] [blame] | 7 | #include "constant_time_internal.h" |
| 8 | #include "test/constant_flow.h" |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 9 | |
Chris Jones | e64a46f | 2020-12-03 17:44:03 +0000 | [diff] [blame] | 10 | #if MBEDTLS_MPI_MAX_BITS > 792 |
| 11 | #define MPI_MAX_BITS_LARGER_THAN_792 |
Chris Jones | 4592bd8 | 2020-12-03 14:24:33 +0000 | [diff] [blame] | 12 | #endif |
Gabor Mezei | 89e3146 | 2022-08-12 15:36:56 +0200 | [diff] [blame] | 13 | |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 14 | /* Check the validity of the sign bit in an MPI object. Reject representations |
| 15 | * that are not supported by the rest of the library and indicate a bug when |
| 16 | * constructing the value. */ |
| 17 | static int sign_is_valid( const mbedtls_mpi *X ) |
| 18 | { |
| 19 | if( X->s != 1 && X->s != -1 ) |
| 20 | return( 0 ); // invalid sign bit, e.g. 0 |
| 21 | if( mbedtls_mpi_bitlen( X ) == 0 && X->s != 1 ) |
| 22 | return( 0 ); // negative zero |
| 23 | return( 1 ); |
| 24 | } |
| 25 | |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 26 | typedef struct mbedtls_test_mpi_random |
| 27 | { |
| 28 | data_t *data; |
| 29 | size_t pos; |
| 30 | size_t chunk_len; |
| 31 | } mbedtls_test_mpi_random; |
| 32 | |
| 33 | /* |
| 34 | * This function is called by the Miller-Rabin primality test each time it |
| 35 | * chooses a random witness. The witnesses (or non-witnesses as provided by the |
| 36 | * test) are stored in the data member of the state structure. Each number is in |
| 37 | * the format that mbedtls_mpi_read_string understands and is chunk_len long. |
| 38 | */ |
| 39 | int mbedtls_test_mpi_miller_rabin_determinizer( void* state, |
| 40 | unsigned char* buf, |
| 41 | size_t len ) |
| 42 | { |
| 43 | mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state; |
| 44 | |
| 45 | if( random == NULL || random->data->x == NULL || buf == NULL ) |
| 46 | return( -1 ); |
| 47 | |
| 48 | if( random->pos + random->chunk_len > random->data->len |
| 49 | || random->chunk_len > len ) |
| 50 | { |
| 51 | return( -1 ); |
| 52 | } |
| 53 | |
| 54 | memset( buf, 0, len ); |
| 55 | |
| 56 | /* The witness is written to the end of the buffer, since the buffer is |
| 57 | * used as big endian, unsigned binary data in mbedtls_mpi_read_binary. |
| 58 | * Writing the witness to the start of the buffer would result in the |
| 59 | * buffer being 'witness 000...000', which would be treated as |
| 60 | * witness * 2^n for some n. */ |
| 61 | memcpy( buf + len - random->chunk_len, &random->data->x[random->pos], |
| 62 | random->chunk_len ); |
| 63 | |
| 64 | random->pos += random->chunk_len; |
| 65 | |
| 66 | return( 0 ); |
| 67 | } |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 68 | |
| 69 | /* Random generator that is told how many bytes to return. */ |
| 70 | static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len ) |
| 71 | { |
| 72 | size_t *bytes_left = state; |
| 73 | size_t i; |
| 74 | for( i = 0; i < len; i++ ) |
| 75 | { |
| 76 | if( *bytes_left == 0 ) |
| 77 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 78 | buf[i] = *bytes_left & 0xff; |
| 79 | --( *bytes_left ); |
| 80 | } |
| 81 | return( 0 ); |
| 82 | } |
| 83 | |
Gilles Peskine | eedefa5 | 2021-04-13 19:50:04 +0200 | [diff] [blame] | 84 | /* Test whether bytes represents (in big-endian base 256) a number b that |
| 85 | * is significantly above a power of 2. That is, b must not have a long run |
| 86 | * of unset bits after the most significant bit. |
| 87 | * |
| 88 | * Let n be the bit-size of b, i.e. the integer such that 2^n <= b < 2^{n+1}. |
| 89 | * This function returns 1 if, when drawing a number between 0 and b, |
| 90 | * the probability that this number is at least 2^n is not negligible. |
| 91 | * This probability is (b - 2^n) / b and this function checks that this |
| 92 | * number is above some threshold A. The threshold value is heuristic and |
| 93 | * based on the needs of mpi_random_many(). |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 94 | */ |
| 95 | static int is_significantly_above_a_power_of_2( data_t *bytes ) |
| 96 | { |
| 97 | const uint8_t *p = bytes->x; |
| 98 | size_t len = bytes->len; |
| 99 | unsigned x; |
Gilles Peskine | eedefa5 | 2021-04-13 19:50:04 +0200 | [diff] [blame] | 100 | |
| 101 | /* Skip leading null bytes */ |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 102 | while( len > 0 && p[0] == 0 ) |
| 103 | { |
| 104 | ++p; |
| 105 | --len; |
| 106 | } |
Gilles Peskine | eedefa5 | 2021-04-13 19:50:04 +0200 | [diff] [blame] | 107 | /* 0 is not significantly above a power of 2 */ |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 108 | if( len == 0 ) |
| 109 | return( 0 ); |
Gilles Peskine | eedefa5 | 2021-04-13 19:50:04 +0200 | [diff] [blame] | 110 | /* Extract the (up to) 2 most significant bytes */ |
| 111 | if( len == 1 ) |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 112 | x = p[0]; |
| 113 | else |
| 114 | x = ( p[0] << 8 ) | p[1]; |
| 115 | |
Gilles Peskine | eedefa5 | 2021-04-13 19:50:04 +0200 | [diff] [blame] | 116 | /* Shift the most significant bit of x to position 8 and mask it out */ |
| 117 | while( ( x & 0xfe00 ) != 0 ) |
| 118 | x >>= 1; |
| 119 | x &= 0x00ff; |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 120 | |
Gilles Peskine | eedefa5 | 2021-04-13 19:50:04 +0200 | [diff] [blame] | 121 | /* At this point, x = floor((b - 2^n) / 2^(n-8)). b is significantly above |
| 122 | * a power of 2 iff x is significantly above 0 compared to 2^8. |
| 123 | * Testing x >= 2^4 amounts to picking A = 1/16 in the function |
| 124 | * description above. */ |
| 125 | return( x >= 0x10 ); |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 128 | /* END_HEADER */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 129 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 130 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | * depends_on:MBEDTLS_BIGNUM_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 132 | * END_DEPENDENCIES |
| 133 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 134 | |
Hanno Becker | b48e1aa | 2018-12-18 23:25:01 +0000 | [diff] [blame] | 135 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 136 | void mpi_null( ) |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 137 | { |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 138 | mbedtls_mpi X, Y, Z; |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 139 | |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 140 | mbedtls_mpi_init( &X ); |
| 141 | mbedtls_mpi_init( &Y ); |
| 142 | mbedtls_mpi_init( &Z ); |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 143 | |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 144 | TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 ); |
| 145 | TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 ); |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 146 | TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 ); |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 147 | TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 ); |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 148 | |
| 149 | exit: |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 150 | mbedtls_mpi_free( &X ); |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 151 | } |
| 152 | /* END_CASE */ |
| 153 | |
| 154 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 155 | void mpi_read_write_string( int radix_X, char * input_X, int radix_A, |
| 156 | char * input_A, int output_size, int result_read, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 157 | int result_write ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 158 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | mbedtls_mpi X; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 160 | char str[1000]; |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 161 | size_t len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 162 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 164 | |
Janos Follath | 04dadb7 | 2019-03-06 12:29:37 +0000 | [diff] [blame] | 165 | memset( str, '!', sizeof( str ) ); |
| 166 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | 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] | 168 | if( result_read == 0 ) |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 169 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 170 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 171 | 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] | 172 | if( result_write == 0 ) |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 173 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 174 | TEST_ASSERT( strcasecmp( str, input_A ) == 0 ); |
Janos Follath | 04dadb7 | 2019-03-06 12:29:37 +0000 | [diff] [blame] | 175 | TEST_ASSERT( str[len] == '!' ); |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 178 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 179 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 180 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 181 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 182 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 183 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 184 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 185 | void mpi_read_binary( data_t * buf, char * input_A ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 186 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | mbedtls_mpi X; |
Janos Follath | e5670f2 | 2019-02-25 16:11:58 +0000 | [diff] [blame] | 188 | char str[1000]; |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 189 | size_t len; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 190 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 192 | |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 193 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 194 | TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 195 | TEST_ASSERT( sign_is_valid( &X ) ); |
Werner Lewis | f65a327 | 2022-07-07 11:38:44 +0100 | [diff] [blame] | 196 | 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] | 197 | TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 198 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 199 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 201 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 202 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 203 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 204 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 205 | void mpi_core_io_null() |
Janos Follath | 91dc67d | 2022-07-22 14:24:58 +0100 | [diff] [blame] | 206 | { |
| 207 | mbedtls_mpi_uint X = 0; |
| 208 | int ret; |
| 209 | |
| 210 | ret = mbedtls_mpi_core_read_be( &X, 1, NULL, 0 ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 211 | TEST_EQUAL( ret, 0 ); |
Janos Follath | 91dc67d | 2022-07-22 14:24:58 +0100 | [diff] [blame] | 212 | ret = mbedtls_mpi_core_write_be( &X, 1, NULL, 0 ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 213 | TEST_EQUAL( ret, 0 ); |
Janos Follath | 91dc67d | 2022-07-22 14:24:58 +0100 | [diff] [blame] | 214 | |
| 215 | ret = mbedtls_mpi_core_read_be( NULL, 0, NULL, 0 ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 216 | TEST_EQUAL( ret, 0 ); |
Janos Follath | 91dc67d | 2022-07-22 14:24:58 +0100 | [diff] [blame] | 217 | ret = mbedtls_mpi_core_write_be( NULL, 0, NULL, 0 ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 218 | TEST_EQUAL( ret, 0 ); |
Janos Follath | 91dc67d | 2022-07-22 14:24:58 +0100 | [diff] [blame] | 219 | |
| 220 | ret = mbedtls_mpi_core_read_le( &X, 1, NULL, 0 ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 221 | TEST_EQUAL( ret, 0 ); |
Janos Follath | 91dc67d | 2022-07-22 14:24:58 +0100 | [diff] [blame] | 222 | ret = mbedtls_mpi_core_write_le( &X, 1, NULL, 0 ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 223 | TEST_EQUAL( ret, 0 ); |
Janos Follath | 91dc67d | 2022-07-22 14:24:58 +0100 | [diff] [blame] | 224 | |
| 225 | ret = mbedtls_mpi_core_read_le( NULL, 0, NULL, 0 ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 226 | TEST_EQUAL( ret, 0 ); |
Janos Follath | 91dc67d | 2022-07-22 14:24:58 +0100 | [diff] [blame] | 227 | ret = mbedtls_mpi_core_write_le( NULL, 0, NULL, 0 ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 228 | TEST_EQUAL( ret, 0 ); |
Janos Follath | 91dc67d | 2022-07-22 14:24:58 +0100 | [diff] [blame] | 229 | |
| 230 | exit: |
| 231 | ; |
| 232 | } |
| 233 | /* END_CASE */ |
| 234 | |
| 235 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 236 | void mpi_core_io_be( data_t *input, int nb_int, int nx_32_int, int iret, |
| 237 | int oret ) |
Janos Follath | f1d617d | 2022-07-21 09:29:32 +0100 | [diff] [blame] | 238 | { |
Janos Follath | f1d617d | 2022-07-21 09:29:32 +0100 | [diff] [blame] | 239 | if( iret != 0 ) |
| 240 | TEST_ASSERT( oret == 0 ); |
| 241 | |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 242 | TEST_LE_S( 0, nb_int ); |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 243 | size_t nb = nb_int; |
Janos Follath | f1d617d | 2022-07-21 09:29:32 +0100 | [diff] [blame] | 244 | |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 245 | unsigned char buf[1024]; |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 246 | TEST_LE_U( nb, sizeof( buf ) ); |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 247 | |
Janos Follath | 1cb3b97 | 2022-08-11 10:50:04 +0100 | [diff] [blame] | 248 | /* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need |
| 249 | * to halve the number of limbs to have the same size. */ |
Janos Follath | 6b8e0c2 | 2022-08-22 09:54:25 +0100 | [diff] [blame] | 250 | size_t nx; |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 251 | TEST_LE_S( 0, nx_32_int ); |
Janos Follath | 6b8e0c2 | 2022-08-22 09:54:25 +0100 | [diff] [blame] | 252 | if( sizeof( mbedtls_mpi_uint ) == 8 ) |
| 253 | nx = nx_32_int / 2 + nx_32_int % 2; |
| 254 | else |
| 255 | nx = nx_32_int; |
Janos Follath | f1d617d | 2022-07-21 09:29:32 +0100 | [diff] [blame] | 256 | |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 257 | mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )]; |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 258 | TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) ); |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 259 | |
| 260 | int ret = mbedtls_mpi_core_read_be( X, nx, input->x, input->len ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 261 | TEST_EQUAL( ret, iret ); |
Janos Follath | f1d617d | 2022-07-21 09:29:32 +0100 | [diff] [blame] | 262 | |
| 263 | if( iret == 0 ) |
| 264 | { |
| 265 | ret = mbedtls_mpi_core_write_be( X, nx, buf, nb ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 266 | TEST_EQUAL( ret, oret ); |
Janos Follath | f1d617d | 2022-07-21 09:29:32 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | if( ( iret == 0 ) && ( oret == 0 ) ) |
| 270 | { |
| 271 | if( nb > input->len ) |
| 272 | { |
| 273 | size_t leading_zeroes = nb - input->len; |
| 274 | TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 ); |
| 275 | for( size_t i = 0; i < leading_zeroes; i++ ) |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 276 | TEST_EQUAL( buf[i], 0 ); |
Janos Follath | f1d617d | 2022-07-21 09:29:32 +0100 | [diff] [blame] | 277 | } |
| 278 | else |
| 279 | { |
| 280 | size_t leading_zeroes = input->len - nb; |
| 281 | TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 ); |
| 282 | for( size_t i = 0; i < leading_zeroes; i++ ) |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 283 | TEST_EQUAL( input->x[i], 0 ); |
Janos Follath | f1d617d | 2022-07-21 09:29:32 +0100 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | exit: |
| 288 | ; |
Janos Follath | f1d617d | 2022-07-21 09:29:32 +0100 | [diff] [blame] | 289 | } |
| 290 | /* END_CASE */ |
| 291 | |
| 292 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 293 | void mpi_core_io_le( data_t *input, int nb_int, int nx_32_int, int iret, |
| 294 | int oret ) |
Janos Follath | 6ff3536 | 2022-07-21 15:27:21 +0100 | [diff] [blame] | 295 | { |
Janos Follath | 6ff3536 | 2022-07-21 15:27:21 +0100 | [diff] [blame] | 296 | if( iret != 0 ) |
| 297 | TEST_ASSERT( oret == 0 ); |
| 298 | |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 299 | TEST_LE_S( 0, nb_int ); |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 300 | size_t nb = nb_int; |
Janos Follath | 6ff3536 | 2022-07-21 15:27:21 +0100 | [diff] [blame] | 301 | |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 302 | unsigned char buf[1024]; |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 303 | TEST_LE_U( nb, sizeof( buf ) ); |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 304 | |
Janos Follath | 9dfb562 | 2022-08-11 12:15:55 +0100 | [diff] [blame] | 305 | /* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need |
| 306 | * to halve the number of limbs to have the same size. */ |
Janos Follath | 6b8e0c2 | 2022-08-22 09:54:25 +0100 | [diff] [blame] | 307 | size_t nx; |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 308 | TEST_LE_S( 0, nx_32_int ); |
Janos Follath | 6b8e0c2 | 2022-08-22 09:54:25 +0100 | [diff] [blame] | 309 | if( sizeof( mbedtls_mpi_uint ) == 8 ) |
| 310 | nx = nx_32_int / 2 + nx_32_int % 2; |
| 311 | else |
| 312 | nx = nx_32_int; |
Janos Follath | 6ff3536 | 2022-07-21 15:27:21 +0100 | [diff] [blame] | 313 | |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 314 | mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )]; |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 315 | TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) ); |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 316 | |
| 317 | int ret = mbedtls_mpi_core_read_le( X, nx, input->x, input->len ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 318 | TEST_EQUAL( ret, iret ); |
Janos Follath | 6ff3536 | 2022-07-21 15:27:21 +0100 | [diff] [blame] | 319 | |
| 320 | if( iret == 0 ) |
| 321 | { |
| 322 | ret = mbedtls_mpi_core_write_le( X, nx, buf, nb ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 323 | TEST_EQUAL( ret, oret ); |
Janos Follath | 6ff3536 | 2022-07-21 15:27:21 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | if( ( iret == 0 ) && ( oret == 0 ) ) |
| 327 | { |
| 328 | if( nb > input->len ) |
| 329 | { |
| 330 | TEST_ASSERT( memcmp( buf, input->x, input->len ) == 0 ); |
| 331 | for( size_t i = input->len; i < nb; i++ ) |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 332 | TEST_EQUAL( buf[i], 0 ); |
Janos Follath | 6ff3536 | 2022-07-21 15:27:21 +0100 | [diff] [blame] | 333 | } |
| 334 | else |
| 335 | { |
| 336 | TEST_ASSERT( memcmp( input->x, buf, nb ) == 0 ); |
| 337 | for( size_t i = nb; i < input->len; i++ ) |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 338 | TEST_EQUAL( input->x[i], 0 ); |
Janos Follath | 6ff3536 | 2022-07-21 15:27:21 +0100 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | exit: |
| 343 | ; |
Janos Follath | 6ff3536 | 2022-07-21 15:27:21 +0100 | [diff] [blame] | 344 | } |
| 345 | /* END_CASE */ |
| 346 | |
| 347 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 348 | void mpi_mod_setup( int ext_rep, int int_rep, int iret ) |
Janos Follath | 1694969 | 2022-08-08 13:37:20 +0100 | [diff] [blame] | 349 | { |
| 350 | #define MLIMBS 8 |
| 351 | mbedtls_mpi_uint mp[MLIMBS]; |
| 352 | mbedtls_mpi_mod_modulus m; |
| 353 | int ret; |
| 354 | |
| 355 | memset( mp, 0xFF, sizeof(mp) ); |
| 356 | |
| 357 | mbedtls_mpi_mod_modulus_init( &m ); |
| 358 | ret = mbedtls_mpi_mod_modulus_setup( &m, mp, MLIMBS, ext_rep, int_rep ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 359 | TEST_EQUAL( ret, iret ); |
Janos Follath | 1694969 | 2022-08-08 13:37:20 +0100 | [diff] [blame] | 360 | |
| 361 | /* Address sanitiser should catch if we try to free mp */ |
| 362 | mbedtls_mpi_mod_modulus_free( &m ); |
| 363 | |
| 364 | /* Make sure that the modulus doesn't have reference to mp anymore */ |
| 365 | TEST_ASSERT( m.p != mp ); |
| 366 | |
| 367 | exit: |
| 368 | /* It should be safe to call an mbedtls free several times */ |
| 369 | mbedtls_mpi_mod_modulus_free( &m ); |
| 370 | |
| 371 | #undef MLIMBS |
| 372 | } |
| 373 | /* END_CASE */ |
| 374 | |
| 375 | |
| 376 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 377 | void mpi_mod_raw_io( data_t *input, int nb_int, int nx_32_int, |
| 378 | int iendian, int iret, int oret ) |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 379 | { |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 380 | if( iret != 0 ) |
| 381 | TEST_ASSERT( oret == 0 ); |
| 382 | |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 383 | TEST_LE_S( 0, nb_int ); |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 384 | size_t nb = nb_int; |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 385 | |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 386 | unsigned char buf[1024]; |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 387 | TEST_LE_U( nb, sizeof( buf ) ); |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 388 | |
Gabor Mezei | 7f08178 | 2022-08-12 18:00:33 +0200 | [diff] [blame] | 389 | /* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need |
| 390 | * to halve the number of limbs to have the same size. */ |
Janos Follath | 6b8e0c2 | 2022-08-22 09:54:25 +0100 | [diff] [blame] | 391 | size_t nx; |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 392 | TEST_LE_S( 0, nx_32_int ); |
Janos Follath | 6b8e0c2 | 2022-08-22 09:54:25 +0100 | [diff] [blame] | 393 | if( sizeof( mbedtls_mpi_uint ) == 8 ) |
| 394 | nx = nx_32_int / 2 + nx_32_int % 2; |
| 395 | else |
| 396 | nx = nx_32_int; |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 397 | |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 398 | mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )]; |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 399 | TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) ); |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 400 | |
| 401 | int endian; |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 402 | if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID ) |
| 403 | endian = MBEDTLS_MPI_MOD_EXT_REP_LE; |
| 404 | else |
| 405 | endian = iendian; |
| 406 | |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 407 | mbedtls_mpi_mod_modulus m; |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 408 | mbedtls_mpi_mod_modulus_init( &m ); |
Janos Follath | 8162064 | 2022-08-15 11:13:38 +0100 | [diff] [blame] | 409 | mbedtls_mpi_uint init[sizeof( X ) / sizeof( X[0] )]; |
| 410 | memset( init, 0xFF, sizeof( init ) ); |
| 411 | int ret = mbedtls_mpi_mod_modulus_setup( &m, init, nx, endian, |
| 412 | MBEDTLS_MPI_MOD_REP_MONTGOMERY ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 413 | TEST_EQUAL( ret, 0 ); |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 414 | |
| 415 | if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID && iret != 0 ) |
| 416 | m.ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID; |
| 417 | |
| 418 | ret = mbedtls_mpi_mod_raw_read( X, &m, input->x, input->len ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 419 | TEST_EQUAL( ret, iret ); |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 420 | |
| 421 | if( iret == 0 ) |
| 422 | { |
| 423 | if( iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID && oret != 0 ) |
| 424 | m.ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID; |
| 425 | |
| 426 | ret = mbedtls_mpi_mod_raw_write( X, &m, buf, nb ); |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 427 | TEST_EQUAL( ret, oret ); |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | if( ( iret == 0 ) && ( oret == 0 ) ) |
| 431 | { |
| 432 | if( nb > input->len ) |
| 433 | { |
| 434 | if( endian == MBEDTLS_MPI_MOD_EXT_REP_BE ) |
| 435 | { |
| 436 | size_t leading_zeroes = nb - input->len; |
| 437 | TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 ); |
| 438 | for( size_t i = 0; i < leading_zeroes; i++ ) |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 439 | TEST_EQUAL( buf[i], 0 ); |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 440 | } |
| 441 | else |
| 442 | { |
| 443 | TEST_ASSERT( memcmp( buf, input->x, input->len ) == 0 ); |
| 444 | for( size_t i = input->len; i < nb; i++ ) |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 445 | TEST_EQUAL( buf[i], 0 ); |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | if( endian == MBEDTLS_MPI_MOD_EXT_REP_BE ) |
| 451 | { |
| 452 | size_t leading_zeroes = input->len - nb; |
| 453 | TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 ); |
| 454 | for( size_t i = 0; i < leading_zeroes; i++ ) |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 455 | TEST_EQUAL( input->x[i], 0 ); |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 456 | } |
| 457 | else |
| 458 | { |
| 459 | TEST_ASSERT( memcmp( input->x, buf, nb ) == 0 ); |
| 460 | for( size_t i = nb; i < input->len; i++ ) |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 461 | TEST_EQUAL( input->x[i], 0 ); |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | exit: |
| 467 | mbedtls_mpi_mod_modulus_free( &m ); |
Gabor Mezei | 23a1ce9 | 2022-08-02 11:54:44 +0200 | [diff] [blame] | 468 | } |
| 469 | /* END_CASE */ |
| 470 | |
| 471 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 472 | void mpi_read_binary_le( data_t * buf, char * input_A ) |
Janos Follath | a778a94 | 2019-02-13 10:28:28 +0000 | [diff] [blame] | 473 | { |
| 474 | mbedtls_mpi X; |
Janos Follath | e5670f2 | 2019-02-25 16:11:58 +0000 | [diff] [blame] | 475 | char str[1000]; |
Janos Follath | a778a94 | 2019-02-13 10:28:28 +0000 | [diff] [blame] | 476 | size_t len; |
| 477 | |
| 478 | mbedtls_mpi_init( &X ); |
| 479 | |
| 480 | |
| 481 | 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] | 482 | TEST_ASSERT( sign_is_valid( &X ) ); |
Werner Lewis | f65a327 | 2022-07-07 11:38:44 +0100 | [diff] [blame] | 483 | 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] | 484 | TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 ); |
Janos Follath | a778a94 | 2019-02-13 10:28:28 +0000 | [diff] [blame] | 485 | |
| 486 | exit: |
| 487 | mbedtls_mpi_free( &X ); |
| 488 | } |
| 489 | /* END_CASE */ |
| 490 | |
| 491 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 492 | void mpi_write_binary( char * input_X, data_t * input_A, |
| 493 | int output_size, int result ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 494 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 495 | mbedtls_mpi X; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 496 | unsigned char buf[1000]; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 497 | size_t buflen; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 498 | |
| 499 | memset( buf, 0x00, 1000 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 500 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 501 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 502 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 503 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | e670f90 | 2015-10-30 09:23:19 +0100 | [diff] [blame] | 504 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 505 | buflen = mbedtls_mpi_size( &X ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 506 | if( buflen > (size_t) output_size ) |
| 507 | buflen = (size_t) output_size; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 508 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 510 | if( result == 0) |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 511 | { |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 512 | |
Ronald Cron | 2dbba99 | 2020-06-10 11:42:32 +0200 | [diff] [blame] | 513 | TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x, |
| 514 | buflen, input_A->len ) == 0 ); |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 515 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 516 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 517 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 518 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 519 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 520 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 521 | |
Janos Follath | e344d0f | 2019-02-19 16:17:40 +0000 | [diff] [blame] | 522 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 523 | void mpi_write_binary_le( char * input_X, data_t * input_A, |
| 524 | int output_size, int result ) |
Janos Follath | e344d0f | 2019-02-19 16:17:40 +0000 | [diff] [blame] | 525 | { |
| 526 | mbedtls_mpi X; |
| 527 | unsigned char buf[1000]; |
| 528 | size_t buflen; |
| 529 | |
| 530 | memset( buf, 0x00, 1000 ); |
| 531 | |
| 532 | mbedtls_mpi_init( &X ); |
| 533 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 534 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Janos Follath | e344d0f | 2019-02-19 16:17:40 +0000 | [diff] [blame] | 535 | |
| 536 | buflen = mbedtls_mpi_size( &X ); |
| 537 | if( buflen > (size_t) output_size ) |
| 538 | buflen = (size_t) output_size; |
| 539 | |
| 540 | TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result ); |
| 541 | if( result == 0) |
| 542 | { |
| 543 | |
Ronald Cron | 2dbba99 | 2020-06-10 11:42:32 +0200 | [diff] [blame] | 544 | TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x, |
| 545 | buflen, input_A->len ) == 0 ); |
Janos Follath | e344d0f | 2019-02-19 16:17:40 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | exit: |
| 549 | mbedtls_mpi_free( &X ); |
| 550 | } |
| 551 | /* END_CASE */ |
| 552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 553 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 554 | 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] | 555 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | mbedtls_mpi X; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 557 | unsigned char buf[1000]; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 558 | size_t buflen; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 559 | FILE *file; |
Manuel Pégourié-Gonnard | e43187d | 2015-02-14 16:01:34 +0000 | [diff] [blame] | 560 | int ret; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 561 | |
| 562 | memset( buf, 0x00, 1000 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 563 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 564 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 565 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 566 | file = fopen( input_file, "r" ); |
Paul Bakker | 8a0c0a9 | 2014-04-17 16:08:20 +0200 | [diff] [blame] | 567 | TEST_ASSERT( file != NULL ); |
Werner Lewis | f65a327 | 2022-07-07 11:38:44 +0100 | [diff] [blame] | 568 | ret = mbedtls_mpi_read_file( &X, 16, file ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 569 | fclose(file); |
Manuel Pégourié-Gonnard | e43187d | 2015-02-14 16:01:34 +0000 | [diff] [blame] | 570 | TEST_ASSERT( ret == result ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 571 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 572 | if( result == 0 ) |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 573 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 574 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 575 | buflen = mbedtls_mpi_size( &X ); |
| 576 | TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 577 | |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 578 | |
Ronald Cron | 2dbba99 | 2020-06-10 11:42:32 +0200 | [diff] [blame] | 579 | TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x, |
| 580 | buflen, input_A->len ) == 0 ); |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 581 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 582 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 583 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 584 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 585 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 586 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 587 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 589 | void mpi_write_file( char * input_X, char * output_file ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 590 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 591 | mbedtls_mpi X, Y; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 592 | FILE *file_out, *file_in; |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 593 | int ret; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 594 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 595 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 596 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 597 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 598 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 599 | file_out = fopen( output_file, "w" ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 600 | TEST_ASSERT( file_out != NULL ); |
Werner Lewis | f65a327 | 2022-07-07 11:38:44 +0100 | [diff] [blame] | 601 | ret = mbedtls_mpi_write_file( NULL, &X, 16, file_out ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 602 | fclose(file_out); |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 603 | TEST_ASSERT( ret == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 604 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 605 | file_in = fopen( output_file, "r" ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 606 | TEST_ASSERT( file_in != NULL ); |
Werner Lewis | f65a327 | 2022-07-07 11:38:44 +0100 | [diff] [blame] | 607 | ret = mbedtls_mpi_read_file( &Y, 16, file_in ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 608 | fclose(file_in); |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 609 | TEST_ASSERT( ret == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 610 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 611 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 612 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 613 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 614 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 615 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 616 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 617 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 618 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 619 | void mpi_get_bit( char * input_X, int pos, int val ) |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 620 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | mbedtls_mpi X; |
| 622 | mbedtls_mpi_init( &X ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 623 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 624 | TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 625 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 626 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 627 | mbedtls_mpi_free( &X ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 628 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 629 | /* END_CASE */ |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 630 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 631 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 632 | void mpi_set_bit( char * input_X, int pos, int val, |
| 633 | char * output_Y, int result ) |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 634 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | mbedtls_mpi X, Y; |
| 636 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 637 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 638 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 639 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, output_Y ) == 0 ); |
Paul Bakker | ec5ceb6 | 2016-07-14 12:47:07 +0100 | [diff] [blame] | 640 | TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result ); |
| 641 | |
| 642 | if( result == 0 ) |
| 643 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 644 | TEST_ASSERT( sign_is_valid( &X ) ); |
Paul Bakker | ec5ceb6 | 2016-07-14 12:47:07 +0100 | [diff] [blame] | 645 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); |
| 646 | } |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 647 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 648 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 650 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 651 | /* END_CASE */ |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 652 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 653 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 654 | void mpi_lsb( char * input_X, int nr_bits ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 655 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 656 | mbedtls_mpi X; |
| 657 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 658 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 659 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 660 | TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 661 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 662 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 663 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 664 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 665 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 666 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 667 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 668 | void mpi_bitlen( char * input_X, int nr_bits ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 669 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 670 | mbedtls_mpi X; |
| 671 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 672 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 673 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 674 | TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 675 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 676 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 678 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 679 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 680 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 681 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 682 | void mpi_gcd( char * input_X, char * input_Y, |
| 683 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 684 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 685 | mbedtls_mpi A, X, Y, Z; |
| 686 | 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] | 687 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 688 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 689 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 690 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 691 | TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 692 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 693 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 694 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 695 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 696 | 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] | 697 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 698 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 699 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 700 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 701 | 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] | 702 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 703 | mbedtls_mpi X; |
| 704 | mbedtls_mpi_init( &X ); |
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 | TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0); |
| 707 | TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 708 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 709 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 710 | mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 711 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 712 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 713 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 714 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 715 | void mpi_cmp_mpi( char * input_X, char * input_Y, |
| 716 | int input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 717 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 718 | mbedtls_mpi X, Y; |
| 719 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 720 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 721 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 722 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 723 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 724 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 725 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 726 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 727 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 728 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 729 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 730 | /* BEGIN_CASE */ |
Janos Follath | deb8030 | 2022-08-19 13:32:17 +0100 | [diff] [blame] | 731 | void mpi_core_lt_ct( data_t * input_X, data_t * input_Y, int input_ret ) |
Janos Follath | 23bdeca | 2022-07-22 18:24:06 +0100 | [diff] [blame] | 732 | { |
| 733 | #define MAX_LEN 64 |
| 734 | mbedtls_mpi_uint X[MAX_LEN]; |
| 735 | mbedtls_mpi_uint Y[MAX_LEN]; |
| 736 | unsigned exp_ret = input_ret; |
| 737 | unsigned ret; |
| 738 | size_t len = CHARS_TO_LIMBS( |
| 739 | input_X->len > input_Y->len ? input_X->len : input_Y->len ); |
| 740 | |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 741 | TEST_LE_U( len, MAX_LEN ); |
Janos Follath | 23bdeca | 2022-07-22 18:24:06 +0100 | [diff] [blame] | 742 | |
| 743 | TEST_ASSERT( mbedtls_mpi_core_read_be( X, len, input_X->x, input_X->len ) |
| 744 | == 0 ); |
| 745 | TEST_ASSERT( mbedtls_mpi_core_read_be( Y, len, input_Y->x, input_Y->len ) |
| 746 | == 0 ); |
| 747 | |
| 748 | TEST_CF_SECRET( X, len * sizeof( mbedtls_mpi_uint ) ); |
| 749 | TEST_CF_SECRET( Y, len * sizeof( mbedtls_mpi_uint ) ); |
| 750 | |
| 751 | ret = mbedtls_mpi_core_lt_ct( X, Y, len ); |
| 752 | |
| 753 | TEST_CF_PUBLIC( X, len * sizeof( mbedtls_mpi_uint ) ); |
| 754 | TEST_CF_PUBLIC( Y, len * sizeof( mbedtls_mpi_uint ) ); |
| 755 | TEST_CF_PUBLIC( &ret, sizeof( ret ) ); |
| 756 | |
Janos Follath | 494a6d2 | 2022-08-22 09:36:17 +0100 | [diff] [blame] | 757 | TEST_EQUAL( ret, exp_ret ); |
Janos Follath | 23bdeca | 2022-07-22 18:24:06 +0100 | [diff] [blame] | 758 | |
| 759 | exit: |
| 760 | ; |
| 761 | |
| 762 | #undef MAX_LEN |
| 763 | } |
| 764 | /* END_CASE */ |
| 765 | |
| 766 | /* BEGIN_CASE */ |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 767 | void mpi_core_cond_assign( data_t * input_X, |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 768 | data_t * input_Y ) |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 769 | { |
| 770 | #define MAX_LEN 64 |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 771 | mbedtls_mpi_uint *X = NULL; |
| 772 | mbedtls_mpi_uint *Y = NULL; |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 773 | size_t len_X = CHARS_TO_LIMBS( input_X->len ); |
| 774 | size_t len_Y = CHARS_TO_LIMBS( input_Y->len ); |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 775 | size_t len_X_byte = len_X * sizeof( mbedtls_mpi_uint ); |
| 776 | size_t len_Y_byte = len_Y * sizeof( mbedtls_mpi_uint ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 777 | |
| 778 | TEST_ASSERT( len_X <= MAX_LEN ); |
| 779 | TEST_ASSERT( len_Y <= MAX_LEN ); |
| 780 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 781 | ASSERT_ALLOC( X, len_X_byte ); |
| 782 | ASSERT_ALLOC( Y, len_Y_byte ); |
| 783 | |
Gabor Mezei | 027d696 | 2022-09-16 17:16:27 +0200 | [diff] [blame] | 784 | TEST_ASSERT( mbedtls_mpi_core_read_be( X, len_X, input_X->x, input_X->len ) |
| 785 | == 0 ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 786 | |
Gabor Mezei | 027d696 | 2022-09-16 17:16:27 +0200 | [diff] [blame] | 787 | TEST_ASSERT( mbedtls_mpi_core_read_be( Y, len_Y, input_Y->x, input_Y->len ) |
| 788 | == 0 ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 789 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 790 | /* condition is false */ |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 791 | TEST_CF_SECRET( X, len_X_byte ); |
| 792 | TEST_CF_SECRET( Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 793 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 794 | mbedtls_mpi_core_cond_assign( X, Y, len_Y, 0 ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 795 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 796 | TEST_CF_PUBLIC( X, len_X_byte ); |
| 797 | TEST_CF_PUBLIC( Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 798 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 799 | TEST_ASSERT( memcmp( X, Y, MIN( len_X_byte, len_Y_byte ) ) != 0 ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 800 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 801 | /* condition is true */ |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 802 | TEST_CF_SECRET( X, len_X_byte ); |
| 803 | TEST_CF_SECRET( Y, len_Y_byte ); |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 804 | |
| 805 | mbedtls_mpi_core_cond_assign( X, Y, len_Y, 1 ); |
| 806 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 807 | TEST_CF_PUBLIC( X, len_X_byte ); |
| 808 | TEST_CF_PUBLIC( Y, len_Y_byte ); |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 809 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 810 | ASSERT_COMPARE( X, len_Y_byte, Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 811 | |
| 812 | exit: |
Gabor Mezei | 8b05e3b | 2022-09-28 12:37:02 +0200 | [diff] [blame^] | 813 | mbedtls_free( X ); |
| 814 | mbedtls_free( Y ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 815 | |
| 816 | #undef MAX_LEN |
| 817 | } |
| 818 | /* END_CASE */ |
| 819 | |
| 820 | /* BEGIN_CASE */ |
| 821 | void mpi_core_cond_swap( data_t * input_X, |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 822 | data_t * input_Y ) |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 823 | { |
| 824 | #define MAX_LEN 64 |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 825 | mbedtls_mpi_uint *tmp_X = NULL; |
| 826 | mbedtls_mpi_uint *tmp_Y = NULL; |
| 827 | mbedtls_mpi_uint *X = NULL; |
| 828 | mbedtls_mpi_uint *Y = NULL; |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 829 | size_t len_X = CHARS_TO_LIMBS( input_X->len ); |
| 830 | size_t len_Y = CHARS_TO_LIMBS( input_Y->len ); |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 831 | size_t len_X_byte = len_X * sizeof( mbedtls_mpi_uint ); |
| 832 | size_t len_Y_byte = len_Y * sizeof( mbedtls_mpi_uint ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 833 | |
| 834 | TEST_ASSERT( len_X <= MAX_LEN ); |
| 835 | TEST_ASSERT( len_Y <= MAX_LEN ); |
| 836 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 837 | ASSERT_ALLOC( tmp_X, len_X_byte ); |
| 838 | ASSERT_ALLOC( tmp_Y, len_Y_byte ); |
| 839 | |
Gabor Mezei | 027d696 | 2022-09-16 17:16:27 +0200 | [diff] [blame] | 840 | TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_X, len_X, |
| 841 | input_X->x, input_X->len ) |
| 842 | == 0 ); |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 843 | ASSERT_ALLOC( X, len_X_byte ); |
| 844 | memcpy( X, tmp_X, len_X_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 845 | |
Gabor Mezei | 027d696 | 2022-09-16 17:16:27 +0200 | [diff] [blame] | 846 | TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_Y, len_Y, |
| 847 | input_Y->x, input_Y->len ) |
| 848 | == 0 ); |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 849 | ASSERT_ALLOC( Y, len_Y_byte ); |
| 850 | memcpy( Y, tmp_Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 851 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 852 | /* condition is false */ |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 853 | TEST_CF_SECRET( X, len_X_byte ); |
| 854 | TEST_CF_SECRET( Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 855 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 856 | mbedtls_mpi_core_cond_swap( X, Y, len_X, 0 ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 857 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 858 | TEST_CF_PUBLIC( X, len_X_byte ); |
| 859 | TEST_CF_PUBLIC( Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 860 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 861 | ASSERT_COMPARE( X, len_X_byte, tmp_X, len_X_byte ); |
| 862 | ASSERT_COMPARE( Y, len_Y_byte, tmp_Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 863 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 864 | /* condition is true */ |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 865 | TEST_CF_SECRET( X, len_X_byte ); |
| 866 | TEST_CF_SECRET( Y, len_Y_byte ); |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 867 | |
| 868 | mbedtls_mpi_core_cond_swap( X, Y, len_X, 1 ); |
| 869 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 870 | TEST_CF_PUBLIC( X, len_X_byte ); |
| 871 | TEST_CF_PUBLIC( Y, len_Y_byte ); |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 872 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 873 | ASSERT_COMPARE( X, len_Y_byte, tmp_Y, len_Y_byte ); |
| 874 | ASSERT_COMPARE( Y, len_X_byte, tmp_X, len_X_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 875 | |
| 876 | exit: |
Gabor Mezei | 8b05e3b | 2022-09-28 12:37:02 +0200 | [diff] [blame^] | 877 | mbedtls_free( tmp_X ); |
| 878 | mbedtls_free( tmp_Y ); |
| 879 | mbedtls_free( X ); |
| 880 | mbedtls_free( Y ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 881 | |
| 882 | #undef MAX_LEN |
| 883 | } |
| 884 | /* END_CASE */ |
| 885 | |
| 886 | /* BEGIN_CASE */ |
| 887 | void mpi_mod_raw_cond_assign( data_t * input_X, |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 888 | data_t * input_Y ) |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 889 | { |
| 890 | #define MAX_LEN 64 |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 891 | mbedtls_mpi_uint *X = NULL; |
| 892 | mbedtls_mpi_uint *Y = NULL; |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 893 | mbedtls_mpi_uint buff_m[MAX_LEN]; |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 894 | mbedtls_mpi_mod_modulus m; |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 895 | size_t len_X = CHARS_TO_LIMBS( input_X->len ); |
| 896 | size_t len_Y = CHARS_TO_LIMBS( input_Y->len ); |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 897 | size_t len_X_byte = len_X * sizeof( mbedtls_mpi_uint ); |
| 898 | size_t len_Y_byte = len_Y * sizeof( mbedtls_mpi_uint ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 899 | |
| 900 | TEST_ASSERT( len_X <= MAX_LEN ); |
| 901 | TEST_ASSERT( len_Y <= MAX_LEN ); |
| 902 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 903 | ASSERT_ALLOC( X, len_X_byte ); |
| 904 | ASSERT_ALLOC( Y, len_Y_byte ); |
| 905 | |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 906 | memset( buff_m, 0, MAX_LEN * sizeof( mbedtls_mpi_uint ) ); |
| 907 | mbedtls_mpi_mod_modulus_init( &m ); |
| 908 | TEST_ASSERT( mbedtls_mpi_mod_modulus_setup( |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 909 | &m, buff_m, len_Y, |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 910 | MBEDTLS_MPI_MOD_EXT_REP_BE, |
| 911 | MBEDTLS_MPI_MOD_REP_MONTGOMERY ) |
| 912 | == 0 ); |
| 913 | |
Gabor Mezei | 027d696 | 2022-09-16 17:16:27 +0200 | [diff] [blame] | 914 | TEST_ASSERT( mbedtls_mpi_core_read_be( X, len_X, |
| 915 | input_X->x, input_X->len ) |
| 916 | == 0 ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 917 | |
Gabor Mezei | 027d696 | 2022-09-16 17:16:27 +0200 | [diff] [blame] | 918 | TEST_ASSERT( mbedtls_mpi_core_read_be( Y, len_Y, |
| 919 | input_Y->x, input_Y->len ) |
| 920 | == 0 ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 921 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 922 | /* condition is false */ |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 923 | TEST_CF_SECRET( X, len_X_byte ); |
| 924 | TEST_CF_SECRET( Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 925 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 926 | mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 0 ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 927 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 928 | TEST_CF_PUBLIC( X, len_X_byte ); |
| 929 | TEST_CF_PUBLIC( Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 930 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 931 | TEST_ASSERT( memcmp( X, Y, m.limbs * sizeof( mbedtls_mpi_uint ) ) != 0 ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 932 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 933 | /* condition is true */ |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 934 | TEST_CF_SECRET( X, len_X_byte ); |
| 935 | TEST_CF_SECRET( Y, len_Y_byte ); |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 936 | |
| 937 | mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 1 ); |
| 938 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 939 | TEST_CF_PUBLIC( X, len_X_byte ); |
| 940 | TEST_CF_PUBLIC( Y, len_Y_byte ); |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 941 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 942 | ASSERT_COMPARE( X, len_Y_byte, Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 943 | |
| 944 | exit: |
Gabor Mezei | 8b05e3b | 2022-09-28 12:37:02 +0200 | [diff] [blame^] | 945 | mbedtls_free( X ); |
| 946 | mbedtls_free( Y ); |
| 947 | |
| 948 | mbedtls_mpi_mod_modulus_free( &m ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 949 | |
| 950 | #undef MAX_LEN |
| 951 | } |
| 952 | /* END_CASE */ |
| 953 | |
| 954 | /* BEGIN_CASE */ |
| 955 | void mpi_mod_raw_cond_swap( data_t * input_X, |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 956 | data_t * input_Y ) |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 957 | { |
| 958 | #define MAX_LEN 64 |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 959 | mbedtls_mpi_uint *tmp_X = NULL; |
| 960 | mbedtls_mpi_uint *tmp_Y = NULL; |
| 961 | mbedtls_mpi_uint *X = NULL; |
| 962 | mbedtls_mpi_uint *Y = NULL; |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 963 | mbedtls_mpi_uint buff_m[MAX_LEN]; |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 964 | mbedtls_mpi_mod_modulus m; |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 965 | size_t len_X = CHARS_TO_LIMBS( input_X->len ); |
| 966 | size_t len_Y = CHARS_TO_LIMBS( input_Y->len ); |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 967 | size_t len_X_byte = len_X * sizeof( mbedtls_mpi_uint ); |
| 968 | size_t len_Y_byte = len_Y * sizeof( mbedtls_mpi_uint ); |
| 969 | size_t len_m_byte = len_X_byte; |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 970 | |
| 971 | TEST_ASSERT( len_X <= MAX_LEN ); |
| 972 | TEST_ASSERT( len_Y <= MAX_LEN ); |
| 973 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 974 | ASSERT_ALLOC( tmp_X, len_X_byte ); |
| 975 | ASSERT_ALLOC( tmp_Y, len_Y_byte ); |
| 976 | |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 977 | memset( buff_m, 0, MAX_LEN * sizeof( mbedtls_mpi_uint ) ); |
| 978 | mbedtls_mpi_mod_modulus_init( &m ); |
| 979 | TEST_ASSERT( mbedtls_mpi_mod_modulus_setup( |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 980 | &m, buff_m, len_X, |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 981 | MBEDTLS_MPI_MOD_EXT_REP_BE, |
| 982 | MBEDTLS_MPI_MOD_REP_MONTGOMERY ) |
| 983 | == 0 ); |
| 984 | |
Gabor Mezei | 027d696 | 2022-09-16 17:16:27 +0200 | [diff] [blame] | 985 | TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_X, len_X, input_X->x, input_X->len ) |
| 986 | == 0 ); |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 987 | ASSERT_ALLOC( X, len_X_byte ); |
| 988 | memcpy( X, tmp_X, len_X_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 989 | |
Gabor Mezei | 027d696 | 2022-09-16 17:16:27 +0200 | [diff] [blame] | 990 | TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_Y, len_Y, input_Y->x, input_Y->len ) |
| 991 | == 0 ); |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 992 | ASSERT_ALLOC( Y, len_Y_byte ); |
| 993 | memcpy( Y, tmp_Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 994 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 995 | /* condition is false */ |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 996 | TEST_CF_SECRET( X, len_X_byte ); |
| 997 | TEST_CF_SECRET( Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 998 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 999 | mbedtls_mpi_mod_raw_cond_swap( X, Y, &m, 0 ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 1000 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 1001 | TEST_CF_PUBLIC( X, len_X_byte ); |
| 1002 | TEST_CF_PUBLIC( Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 1003 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 1004 | ASSERT_COMPARE( X, len_X_byte, tmp_X, len_X_byte ); |
| 1005 | ASSERT_COMPARE( Y, len_Y_byte, tmp_Y, len_Y_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 1006 | |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 1007 | /* condition is true */ |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 1008 | TEST_CF_SECRET( X, len_X_byte ); |
| 1009 | TEST_CF_SECRET( Y, len_Y_byte ); |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 1010 | |
| 1011 | mbedtls_mpi_mod_raw_cond_swap( X, Y, &m, 1 ); |
| 1012 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 1013 | TEST_CF_PUBLIC( X, len_X_byte ); |
| 1014 | TEST_CF_PUBLIC( Y, len_Y_byte ); |
Gabor Mezei | 821d151 | 2022-09-27 12:41:28 +0200 | [diff] [blame] | 1015 | |
Gabor Mezei | a758488 | 2022-09-27 13:18:02 +0200 | [diff] [blame] | 1016 | ASSERT_COMPARE( X, len_m_byte, tmp_Y, len_Y_byte ); |
| 1017 | ASSERT_COMPARE( Y, len_m_byte, tmp_X, len_X_byte ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 1018 | |
| 1019 | exit: |
Gabor Mezei | 8b05e3b | 2022-09-28 12:37:02 +0200 | [diff] [blame^] | 1020 | mbedtls_free( tmp_X ); |
| 1021 | mbedtls_free( tmp_Y ); |
| 1022 | mbedtls_free( X ); |
| 1023 | mbedtls_free( Y ); |
| 1024 | |
| 1025 | mbedtls_mpi_mod_modulus_free( &m ); |
Gabor Mezei | b27b1c5 | 2022-09-12 16:36:48 +0200 | [diff] [blame] | 1026 | |
| 1027 | #undef MAX_LEN |
| 1028 | } |
| 1029 | /* END_CASE */ |
| 1030 | |
| 1031 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1032 | void mpi_lt_mpi_ct( int size_X, char * input_X, |
| 1033 | int size_Y, char * input_Y, |
| 1034 | int input_ret, int input_err ) |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 1035 | { |
Gilles Peskine | 0deccf1 | 2020-09-02 15:18:07 +0200 | [diff] [blame] | 1036 | unsigned ret = -1; |
Janos Follath | 0e5532d | 2019-10-11 14:21:53 +0100 | [diff] [blame] | 1037 | unsigned input_uret = input_ret; |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 1038 | mbedtls_mpi X, Y; |
| 1039 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
| 1040 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1041 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1042 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 1043 | |
Gilles Peskine | 9018b11 | 2020-01-21 16:30:53 +0100 | [diff] [blame] | 1044 | TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 ); |
| 1045 | TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 ); |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 1046 | |
Janos Follath | 0e5532d | 2019-10-11 14:21:53 +0100 | [diff] [blame] | 1047 | TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err ); |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 1048 | if( input_err == 0 ) |
Janos Follath | 0e5532d | 2019-10-11 14:21:53 +0100 | [diff] [blame] | 1049 | TEST_ASSERT( ret == input_uret ); |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 1050 | |
| 1051 | exit: |
| 1052 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
| 1053 | } |
| 1054 | /* END_CASE */ |
| 1055 | |
| 1056 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1057 | void mpi_cmp_abs( char * input_X, char * input_Y, |
| 1058 | int input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1059 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1060 | mbedtls_mpi X, Y; |
| 1061 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1062 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1063 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1064 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1065 | TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1066 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1067 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1068 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1069 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1070 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1071 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1072 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1073 | void mpi_copy( char *src_hex, char *dst_hex ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1074 | { |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 1075 | mbedtls_mpi src, dst, ref; |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 1076 | mbedtls_mpi_init( &src ); |
| 1077 | mbedtls_mpi_init( &dst ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 1078 | mbedtls_mpi_init( &ref ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1079 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1080 | TEST_ASSERT( mbedtls_test_read_mpi( &src, src_hex ) == 0 ); |
| 1081 | TEST_ASSERT( mbedtls_test_read_mpi( &ref, dst_hex ) == 0 ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 1082 | |
| 1083 | /* mbedtls_mpi_copy() */ |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1084 | TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 ); |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 1085 | TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 ); |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 1086 | TEST_ASSERT( sign_is_valid( &dst ) ); |
| 1087 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1088 | |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 1089 | /* mbedtls_mpi_safe_cond_assign(), assignment done */ |
| 1090 | mbedtls_mpi_free( &dst ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1091 | TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 1092 | TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 ); |
| 1093 | TEST_ASSERT( sign_is_valid( &dst ) ); |
| 1094 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 ); |
| 1095 | |
| 1096 | /* mbedtls_mpi_safe_cond_assign(), assignment not done */ |
| 1097 | mbedtls_mpi_free( &dst ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1098 | TEST_ASSERT( mbedtls_test_read_mpi( &dst, dst_hex ) == 0 ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 1099 | TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 ); |
| 1100 | TEST_ASSERT( sign_is_valid( &dst ) ); |
| 1101 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 ); |
| 1102 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1103 | exit: |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 1104 | mbedtls_mpi_free( &src ); |
| 1105 | mbedtls_mpi_free( &dst ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 1106 | mbedtls_mpi_free( &ref ); |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 1107 | } |
| 1108 | /* END_CASE */ |
| 1109 | |
| 1110 | /* BEGIN_CASE */ |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 1111 | void mpi_copy_self( char *input_X ) |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 1112 | { |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 1113 | mbedtls_mpi X, A; |
| 1114 | mbedtls_mpi_init( &A ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1115 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 1116 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1117 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1118 | TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 ); |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 1119 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1120 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_X ) == 0 ); |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 1121 | TEST_ASSERT( sign_is_valid( &X ) ); |
| 1122 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1123 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1124 | exit: |
Gilles Peskine | 90ec8e8 | 2021-06-10 15:17:30 +0200 | [diff] [blame] | 1125 | mbedtls_mpi_free( &A ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1126 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 1127 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1128 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 1129 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1130 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1131 | void mpi_swap( char *X_hex, char *Y_hex ) |
Gilles Peskine | fc1eeef | 2021-06-10 22:29:57 +0200 | [diff] [blame] | 1132 | { |
| 1133 | mbedtls_mpi X, Y, X0, Y0; |
| 1134 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
| 1135 | mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 ); |
| 1136 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1137 | TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 ); |
| 1138 | TEST_ASSERT( mbedtls_test_read_mpi( &Y0, Y_hex ) == 0 ); |
Gilles Peskine | fc1eeef | 2021-06-10 22:29:57 +0200 | [diff] [blame] | 1139 | |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 1140 | /* mbedtls_mpi_swap() */ |
Tom Cosgrove | c71ca0c | 2022-09-15 15:38:17 +0100 | [diff] [blame] | 1141 | TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 ); |
| 1142 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 ); |
Gilles Peskine | fc1eeef | 2021-06-10 22:29:57 +0200 | [diff] [blame] | 1143 | mbedtls_mpi_swap( &X, &Y ); |
| 1144 | TEST_ASSERT( sign_is_valid( &X ) ); |
| 1145 | TEST_ASSERT( sign_is_valid( &Y ) ); |
| 1146 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 ); |
| 1147 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 ); |
| 1148 | |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 1149 | /* mbedtls_mpi_safe_cond_swap(), swap done */ |
| 1150 | mbedtls_mpi_free( &X ); |
| 1151 | mbedtls_mpi_free( &Y ); |
Tom Cosgrove | c71ca0c | 2022-09-15 15:38:17 +0100 | [diff] [blame] | 1152 | TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 ); |
| 1153 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 1154 | TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 ); |
| 1155 | TEST_ASSERT( sign_is_valid( &X ) ); |
| 1156 | TEST_ASSERT( sign_is_valid( &Y ) ); |
| 1157 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 ); |
| 1158 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 ); |
| 1159 | |
| 1160 | /* mbedtls_mpi_safe_cond_swap(), swap not done */ |
| 1161 | mbedtls_mpi_free( &X ); |
| 1162 | mbedtls_mpi_free( &Y ); |
Tom Cosgrove | c71ca0c | 2022-09-15 15:38:17 +0100 | [diff] [blame] | 1163 | TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 ); |
| 1164 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, Y_hex ) == 0 ); |
Gilles Peskine | d0722f8 | 2021-06-10 23:00:33 +0200 | [diff] [blame] | 1165 | TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 ); |
| 1166 | TEST_ASSERT( sign_is_valid( &X ) ); |
| 1167 | TEST_ASSERT( sign_is_valid( &Y ) ); |
| 1168 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 ); |
| 1169 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 ); |
| 1170 | |
Gilles Peskine | fc1eeef | 2021-06-10 22:29:57 +0200 | [diff] [blame] | 1171 | exit: |
| 1172 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
| 1173 | mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 ); |
| 1174 | } |
| 1175 | /* END_CASE */ |
| 1176 | |
| 1177 | /* BEGIN_CASE */ |
| 1178 | void mpi_swap_self( char *X_hex ) |
| 1179 | { |
| 1180 | mbedtls_mpi X, X0; |
| 1181 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 ); |
| 1182 | |
Tom Cosgrove | c71ca0c | 2022-09-15 15:38:17 +0100 | [diff] [blame] | 1183 | TEST_ASSERT( mbedtls_test_read_mpi( &X, X_hex ) == 0 ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1184 | TEST_ASSERT( mbedtls_test_read_mpi( &X0, X_hex ) == 0 ); |
Gilles Peskine | fc1eeef | 2021-06-10 22:29:57 +0200 | [diff] [blame] | 1185 | |
| 1186 | mbedtls_mpi_swap( &X, &X ); |
| 1187 | TEST_ASSERT( sign_is_valid( &X ) ); |
| 1188 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 ); |
| 1189 | |
| 1190 | exit: |
| 1191 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 ); |
| 1192 | } |
| 1193 | /* END_CASE */ |
| 1194 | |
| 1195 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1196 | 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] | 1197 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1198 | mbedtls_mpi X; |
| 1199 | mbedtls_mpi_init( &X ); |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 1200 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1201 | TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 ); |
Gilles Peskine | e109175 | 2021-06-15 21:19:18 +0200 | [diff] [blame] | 1202 | if( used > 0 ) |
| 1203 | { |
| 1204 | size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint ); |
| 1205 | TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 ); |
| 1206 | } |
| 1207 | TEST_EQUAL( X.n, (size_t) before ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1208 | TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 ); |
Gilles Peskine | e109175 | 2021-06-15 21:19:18 +0200 | [diff] [blame] | 1209 | TEST_EQUAL( X.n, (size_t) after ); |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 1210 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1211 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1212 | mbedtls_mpi_free( &X ); |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 1213 | } |
| 1214 | /* END_CASE */ |
| 1215 | |
| 1216 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1217 | void mpi_add_mpi( char * input_X, char * input_Y, |
| 1218 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1219 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1220 | mbedtls_mpi X, Y, Z, A; |
| 1221 | 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] | 1222 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1223 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1224 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 1225 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1226 | TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1227 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1228 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1229 | |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1230 | /* result == first operand */ |
| 1231 | TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1232 | TEST_ASSERT( sign_is_valid( &X ) ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1233 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1234 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1235 | |
| 1236 | /* result == second operand */ |
| 1237 | TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1238 | TEST_ASSERT( sign_is_valid( &Y ) ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1239 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); |
| 1240 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1241 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1242 | 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] | 1243 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1244 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1245 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1246 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1247 | void mpi_add_mpi_inplace( char * input_X, char * input_A ) |
Janos Follath | 044a86b | 2015-10-25 10:58:03 +0100 | [diff] [blame] | 1248 | { |
| 1249 | mbedtls_mpi X, A; |
| 1250 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); |
| 1251 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1252 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Janos Follath | 6cbacec | 2015-10-25 12:29:13 +0100 | [diff] [blame] | 1253 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1254 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Janos Follath | 6cbacec | 2015-10-25 12:29:13 +0100 | [diff] [blame] | 1255 | TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 ); |
| 1256 | TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1257 | TEST_ASSERT( sign_is_valid( &X ) ); |
Janos Follath | 6cbacec | 2015-10-25 12:29:13 +0100 | [diff] [blame] | 1258 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1259 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Janos Follath | 6cbacec | 2015-10-25 12:29:13 +0100 | [diff] [blame] | 1260 | TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1261 | TEST_ASSERT( sign_is_valid( &X ) ); |
Janos Follath | 6cbacec | 2015-10-25 12:29:13 +0100 | [diff] [blame] | 1262 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
| 1263 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1264 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Janos Follath | 044a86b | 2015-10-25 10:58:03 +0100 | [diff] [blame] | 1265 | TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1266 | TEST_ASSERT( sign_is_valid( &X ) ); |
Janos Follath | 044a86b | 2015-10-25 10:58:03 +0100 | [diff] [blame] | 1267 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
| 1268 | |
| 1269 | exit: |
| 1270 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); |
| 1271 | } |
| 1272 | /* END_CASE */ |
| 1273 | |
| 1274 | |
| 1275 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1276 | void mpi_add_abs( char * input_X, char * input_Y, |
| 1277 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1278 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1279 | mbedtls_mpi X, Y, Z, A; |
| 1280 | 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] | 1281 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1282 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1283 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 1284 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1285 | TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1286 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1287 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1288 | |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1289 | /* result == first operand */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1290 | TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1291 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1292 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1293 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1294 | |
| 1295 | /* result == second operand */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1296 | TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1297 | TEST_ASSERT( sign_is_valid( &Y ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1298 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1299 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1300 | exit: |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1301 | 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] | 1302 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1303 | /* END_CASE */ |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 1304 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1305 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1306 | void mpi_add_int( char * input_X, int input_Y, |
| 1307 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1308 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1309 | mbedtls_mpi X, Z, A; |
| 1310 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1311 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1312 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1313 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1314 | TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1315 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1316 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1317 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1318 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1319 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1320 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1321 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1322 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1323 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1324 | void mpi_sub_mpi( char * input_X, char * input_Y, |
| 1325 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1326 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1327 | mbedtls_mpi X, Y, Z, A; |
| 1328 | 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] | 1329 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1330 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1331 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 1332 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1333 | TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1334 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1335 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1336 | |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1337 | /* result == first operand */ |
| 1338 | TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1339 | TEST_ASSERT( sign_is_valid( &X ) ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1340 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1341 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1342 | |
| 1343 | /* result == second operand */ |
| 1344 | TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1345 | TEST_ASSERT( sign_is_valid( &Y ) ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1346 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); |
| 1347 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1348 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1349 | 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] | 1350 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1351 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1352 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1353 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1354 | void mpi_sub_abs( char * input_X, char * input_Y, |
| 1355 | char * input_A, int sub_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1356 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1357 | mbedtls_mpi X, Y, Z, A; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1358 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1359 | 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] | 1360 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1361 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1362 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 1363 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | e670f90 | 2015-10-30 09:23:19 +0100 | [diff] [blame] | 1364 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1365 | res = mbedtls_mpi_sub_abs( &Z, &X, &Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1366 | TEST_ASSERT( res == sub_result ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1367 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1368 | if( res == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1369 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1370 | |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1371 | /* result == first operand */ |
| 1372 | TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1373 | TEST_ASSERT( sign_is_valid( &X ) ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1374 | if( sub_result == 0 ) |
| 1375 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1376 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1377 | |
| 1378 | /* result == second operand */ |
| 1379 | TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1380 | TEST_ASSERT( sign_is_valid( &Y ) ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1381 | if( sub_result == 0 ) |
| 1382 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); |
| 1383 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1384 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1385 | 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] | 1386 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1387 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1388 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1389 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1390 | void mpi_sub_int( char * input_X, int input_Y, |
| 1391 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1392 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1393 | mbedtls_mpi X, Z, A; |
| 1394 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1395 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1396 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1397 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1398 | TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1399 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1400 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1401 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1402 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1403 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1404 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1405 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1406 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1407 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1408 | void mpi_mul_mpi( char * input_X, char * input_Y, |
| 1409 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1410 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1411 | mbedtls_mpi X, Y, Z, A; |
| 1412 | 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] | 1413 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1414 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1415 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 1416 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1417 | TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1418 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1419 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1420 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1421 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1422 | 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] | 1423 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1424 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1425 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1426 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1427 | void mpi_mul_int( char * input_X, int input_Y, |
| 1428 | char * input_A, char * result_comparison ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1429 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1430 | mbedtls_mpi X, Z, A; |
| 1431 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1432 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1433 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1434 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1435 | TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1436 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1437 | if( strcmp( result_comparison, "==" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1438 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1439 | else if( strcmp( result_comparison, "!=" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1440 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1441 | else |
| 1442 | TEST_ASSERT( "unknown operator" == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1443 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1444 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1445 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1446 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1447 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1448 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1449 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1450 | void mpi_div_mpi( char * input_X, char * input_Y, |
| 1451 | char * input_A, char * input_B, |
| 1452 | int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1453 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1454 | mbedtls_mpi X, Y, Q, R, A, B; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1455 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1456 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); |
| 1457 | mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1458 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1459 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1460 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 1461 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
| 1462 | TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1463 | res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1464 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1465 | if( res == 0 ) |
| 1466 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1467 | TEST_ASSERT( sign_is_valid( &Q ) ); |
| 1468 | TEST_ASSERT( sign_is_valid( &R ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1469 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 ); |
| 1470 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1471 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1472 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1473 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1474 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); |
| 1475 | mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1476 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1477 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1478 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1479 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1480 | void mpi_div_int( char * input_X, int input_Y, |
| 1481 | char * input_A, char * input_B, |
| 1482 | int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1483 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1484 | mbedtls_mpi X, Q, R, A, B; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1485 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1486 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A ); |
| 1487 | mbedtls_mpi_init( &B ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1488 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1489 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1490 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
| 1491 | TEST_ASSERT( mbedtls_test_read_mpi( &B, input_B ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1492 | res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1493 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1494 | if( res == 0 ) |
| 1495 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1496 | TEST_ASSERT( sign_is_valid( &Q ) ); |
| 1497 | TEST_ASSERT( sign_is_valid( &R ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1498 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 ); |
| 1499 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1500 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1501 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1502 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1503 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A ); |
| 1504 | mbedtls_mpi_free( &B ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1505 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1506 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1507 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1508 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1509 | void mpi_mod_mpi( char * input_X, char * input_Y, |
| 1510 | char * input_A, int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1511 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1512 | mbedtls_mpi X, Y, A; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1513 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1514 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1515 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1516 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1517 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 1518 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1519 | res = mbedtls_mpi_mod_mpi( &X, &X, &Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1520 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1521 | if( res == 0 ) |
| 1522 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1523 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1524 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1525 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1526 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1527 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1528 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1529 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1530 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1531 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1532 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1533 | void mpi_mod_int( char * input_X, int input_Y, |
| 1534 | int input_A, int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1535 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1536 | mbedtls_mpi X; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1537 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1538 | mbedtls_mpi_uint r; |
| 1539 | mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1540 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1541 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1542 | res = mbedtls_mpi_mod_int( &r, &X, input_Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1543 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1544 | if( res == 0 ) |
| 1545 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1546 | TEST_ASSERT( r == (mbedtls_mpi_uint) input_A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1547 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1548 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1549 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1550 | mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1551 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1552 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1553 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1554 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1555 | void mpi_exp_mod( char * input_A, char * input_E, |
| 1556 | char * input_N, char * input_X, |
| 1557 | int exp_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1558 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1559 | mbedtls_mpi A, E, N, RR, Z, X; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1560 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1561 | mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); |
| 1562 | mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1563 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1564 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
| 1565 | TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 ); |
| 1566 | TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 ); |
| 1567 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1568 | |
Gilles Peskine | 342f71b | 2021-06-09 18:31:35 +0200 | [diff] [blame] | 1569 | res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL ); |
Gilles Peskine | 722c62c | 2021-06-15 21:55:05 +0200 | [diff] [blame] | 1570 | TEST_ASSERT( res == exp_result ); |
Gilles Peskine | 342f71b | 2021-06-09 18:31:35 +0200 | [diff] [blame] | 1571 | if( res == 0 ) |
| 1572 | { |
| 1573 | TEST_ASSERT( sign_is_valid( &Z ) ); |
| 1574 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 ); |
| 1575 | } |
| 1576 | |
| 1577 | /* Now test again with the speed-up parameter supplied as an output. */ |
| 1578 | res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ); |
Gilles Peskine | 722c62c | 2021-06-15 21:55:05 +0200 | [diff] [blame] | 1579 | TEST_ASSERT( res == exp_result ); |
Gilles Peskine | 342f71b | 2021-06-09 18:31:35 +0200 | [diff] [blame] | 1580 | if( res == 0 ) |
| 1581 | { |
| 1582 | TEST_ASSERT( sign_is_valid( &Z ) ); |
| 1583 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 ); |
| 1584 | } |
| 1585 | |
| 1586 | /* 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] | 1587 | res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ); |
Gilles Peskine | 722c62c | 2021-06-15 21:55:05 +0200 | [diff] [blame] | 1588 | TEST_ASSERT( res == exp_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1589 | if( res == 0 ) |
| 1590 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1591 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1592 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1593 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1594 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1595 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1596 | mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); |
| 1597 | mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1598 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1599 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1600 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1601 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1602 | void mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes, |
| 1603 | char * input_RR, int exp_result ) |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1604 | { |
| 1605 | mbedtls_mpi A, E, N, RR, Z; |
| 1606 | mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); |
| 1607 | mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); |
| 1608 | |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1609 | /* Set A to 2^(A_bytes - 1) + 1 */ |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1610 | TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1611 | TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1612 | TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 ); |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1613 | |
| 1614 | /* Set E to 2^(E_bytes - 1) + 1 */ |
| 1615 | TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 ); |
| 1616 | TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1617 | TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 ); |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1618 | |
| 1619 | /* Set N to 2^(N_bytes - 1) + 1 */ |
| 1620 | TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 ); |
| 1621 | TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1622 | TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 ); |
| 1623 | |
| 1624 | if( strlen( input_RR ) ) |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1625 | TEST_ASSERT( mbedtls_test_read_mpi( &RR, input_RR ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1626 | |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1627 | 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] | 1628 | |
| 1629 | exit: |
| 1630 | mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); |
| 1631 | mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); |
| 1632 | } |
| 1633 | /* END_CASE */ |
| 1634 | |
| 1635 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1636 | void mpi_inv_mod( char * input_X, char * input_Y, |
| 1637 | char * input_A, int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1638 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1639 | mbedtls_mpi X, Y, Z, A; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1640 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1641 | 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] | 1642 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1643 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1644 | TEST_ASSERT( mbedtls_test_read_mpi( &Y, input_Y ) == 0 ); |
| 1645 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1646 | res = mbedtls_mpi_inv_mod( &Z, &X, &Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1647 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1648 | if( res == 0 ) |
| 1649 | { |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1650 | TEST_ASSERT( sign_is_valid( &Z ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1651 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1652 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1653 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1654 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1655 | 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] | 1656 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1657 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1658 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1659 | /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1660 | void mpi_is_prime( char * input_X, int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1661 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1662 | mbedtls_mpi X; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1663 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1664 | mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1665 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1666 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1667 | 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] | 1668 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1669 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1670 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1671 | mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1672 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1673 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1675 | /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1676 | void mpi_is_prime_det( data_t * input_X, data_t * witnesses, |
| 1677 | int chunk_len, int rounds ) |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 1678 | { |
| 1679 | mbedtls_mpi X; |
| 1680 | int res; |
| 1681 | mbedtls_test_mpi_random rand; |
| 1682 | |
| 1683 | mbedtls_mpi_init( &X ); |
| 1684 | rand.data = witnesses; |
| 1685 | rand.pos = 0; |
| 1686 | rand.chunk_len = chunk_len; |
| 1687 | |
| 1688 | 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] | 1689 | res = mbedtls_mpi_is_prime_ext( &X, rounds - 1, |
| 1690 | mbedtls_test_mpi_miller_rabin_determinizer, |
| 1691 | &rand ); |
| 1692 | TEST_ASSERT( res == 0 ); |
| 1693 | |
| 1694 | rand.data = witnesses; |
| 1695 | rand.pos = 0; |
| 1696 | rand.chunk_len = chunk_len; |
| 1697 | |
Janos Follath | a0b67c2 | 2018-09-18 14:48:23 +0100 | [diff] [blame] | 1698 | res = mbedtls_mpi_is_prime_ext( &X, rounds, |
| 1699 | mbedtls_test_mpi_miller_rabin_determinizer, |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 1700 | &rand ); |
Darryl Green | ac2ead0 | 2018-10-02 15:30:39 +0100 | [diff] [blame] | 1701 | TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE ); |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 1702 | |
| 1703 | exit: |
| 1704 | mbedtls_mpi_free( &X ); |
| 1705 | } |
| 1706 | /* END_CASE */ |
| 1707 | |
| 1708 | /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1709 | 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] | 1710 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1711 | mbedtls_mpi X; |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1712 | int my_ret; |
| 1713 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1714 | mbedtls_mpi_init( &X ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1715 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1716 | my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, |
| 1717 | mbedtls_test_rnd_std_rand, NULL ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1718 | TEST_ASSERT( my_ret == ref_ret ); |
| 1719 | |
| 1720 | if( ref_ret == 0 ) |
| 1721 | { |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 1722 | size_t actual_bits = mbedtls_mpi_bitlen( &X ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1723 | |
| 1724 | TEST_ASSERT( actual_bits >= (size_t) bits ); |
| 1725 | TEST_ASSERT( actual_bits <= (size_t) bits + 1 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1726 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1727 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1728 | TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, |
| 1729 | mbedtls_test_rnd_std_rand, |
| 1730 | NULL ) == 0 ); |
Janos Follath | a3cb7eb | 2018-08-14 15:31:54 +0100 | [diff] [blame] | 1731 | if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1732 | { |
Hanno Becker | d4d6057 | 2018-01-10 07:12:01 +0000 | [diff] [blame] | 1733 | /* X = ( X - 1 ) / 2 */ |
| 1734 | TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 ); |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1735 | TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, |
| 1736 | mbedtls_test_rnd_std_rand, |
| 1737 | NULL ) == 0 ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1741 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1742 | mbedtls_mpi_free( &X ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1743 | } |
| 1744 | /* END_CASE */ |
| 1745 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1746 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1747 | void mpi_shift_l( char * input_X, int shift_X, |
| 1748 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1749 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1750 | mbedtls_mpi X, A; |
| 1751 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1752 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1753 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1754 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1755 | TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1756 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1757 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1758 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1759 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1760 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1761 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1762 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1763 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1764 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 1765 | void mpi_shift_r( char * input_X, int shift_X, |
| 1766 | char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1767 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1768 | mbedtls_mpi X, A; |
| 1769 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1770 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 1771 | TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 ); |
| 1772 | TEST_ASSERT( mbedtls_test_read_mpi( &A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1773 | TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1774 | TEST_ASSERT( sign_is_valid( &X ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1775 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1776 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1777 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1778 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1779 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1780 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1781 | |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 1782 | /* BEGIN_CASE */ |
Gilles Peskine | 422e867 | 2021-04-02 00:02:27 +0200 | [diff] [blame] | 1783 | void mpi_fill_random( int wanted_bytes, int rng_bytes, |
| 1784 | int before, int expected_ret ) |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 1785 | { |
| 1786 | mbedtls_mpi X; |
| 1787 | int ret; |
| 1788 | size_t bytes_left = rng_bytes; |
| 1789 | mbedtls_mpi_init( &X ); |
| 1790 | |
Gilles Peskine | 422e867 | 2021-04-02 00:02:27 +0200 | [diff] [blame] | 1791 | if( before != 0 ) |
| 1792 | { |
| 1793 | /* Set X to sign(before) * 2^(|before|-1) */ |
| 1794 | TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 ); |
| 1795 | if( before < 0 ) |
| 1796 | before = - before; |
| 1797 | TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 ); |
| 1798 | } |
| 1799 | |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 1800 | ret = mbedtls_mpi_fill_random( &X, wanted_bytes, |
| 1801 | f_rng_bytes_left, &bytes_left ); |
| 1802 | TEST_ASSERT( ret == expected_ret ); |
| 1803 | |
| 1804 | if( expected_ret == 0 ) |
| 1805 | { |
| 1806 | /* mbedtls_mpi_fill_random is documented to use bytes from the RNG |
| 1807 | * as a big-endian representation of the number. We know when |
| 1808 | * our RNG function returns null bytes, so we know how many |
| 1809 | * leading zero bytes the number has. */ |
| 1810 | size_t leading_zeros = 0; |
| 1811 | if( wanted_bytes > 0 && rng_bytes % 256 == 0 ) |
| 1812 | leading_zeros = 1; |
| 1813 | TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros == |
| 1814 | (size_t) wanted_bytes ); |
| 1815 | TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1816 | TEST_ASSERT( sign_is_valid( &X ) ); |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | exit: |
| 1820 | mbedtls_mpi_free( &X ); |
| 1821 | } |
| 1822 | /* END_CASE */ |
| 1823 | |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 1824 | /* BEGIN_CASE */ |
| 1825 | void mpi_random_many( int min, data_t *bound_bytes, int iterations ) |
| 1826 | { |
| 1827 | /* Generate numbers in the range 1..bound-1. Do it iterations times. |
| 1828 | * This function assumes that the value of bound is at least 2 and |
| 1829 | * that iterations is large enough that a one-in-2^iterations chance |
| 1830 | * effectively never occurs. |
| 1831 | */ |
| 1832 | |
| 1833 | mbedtls_mpi upper_bound; |
| 1834 | size_t n_bits; |
| 1835 | mbedtls_mpi result; |
| 1836 | size_t b; |
| 1837 | /* If upper_bound is small, stats[b] is the number of times the value b |
| 1838 | * has been generated. Otherwise stats[b] is the number of times a |
| 1839 | * value with bit b set has been generated. */ |
| 1840 | size_t *stats = NULL; |
| 1841 | size_t stats_len; |
| 1842 | int full_stats; |
| 1843 | size_t i; |
| 1844 | |
| 1845 | mbedtls_mpi_init( &upper_bound ); |
| 1846 | mbedtls_mpi_init( &result ); |
| 1847 | |
| 1848 | TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound, |
| 1849 | bound_bytes->x, bound_bytes->len ) ); |
| 1850 | n_bits = mbedtls_mpi_bitlen( &upper_bound ); |
| 1851 | /* Consider a bound "small" if it's less than 2^5. This value is chosen |
| 1852 | * to be small enough that the probability of missing one value is |
| 1853 | * negligible given the number of iterations. It must be less than |
| 1854 | * 256 because some of the code below assumes that "small" values |
| 1855 | * fit in a byte. */ |
| 1856 | if( n_bits <= 5 ) |
| 1857 | { |
| 1858 | full_stats = 1; |
| 1859 | stats_len = bound_bytes->x[bound_bytes->len - 1]; |
| 1860 | } |
| 1861 | else |
| 1862 | { |
| 1863 | full_stats = 0; |
| 1864 | stats_len = n_bits; |
| 1865 | } |
| 1866 | ASSERT_ALLOC( stats, stats_len ); |
| 1867 | |
| 1868 | for( i = 0; i < (size_t) iterations; i++ ) |
| 1869 | { |
| 1870 | mbedtls_test_set_step( i ); |
| 1871 | TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound, |
| 1872 | mbedtls_test_rnd_std_rand, NULL ) ); |
| 1873 | |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1874 | TEST_ASSERT( sign_is_valid( &result ) ); |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 1875 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 ); |
| 1876 | TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 ); |
| 1877 | if( full_stats ) |
| 1878 | { |
| 1879 | uint8_t value; |
| 1880 | TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) ); |
| 1881 | TEST_ASSERT( value < stats_len ); |
| 1882 | ++stats[value]; |
| 1883 | } |
| 1884 | else |
| 1885 | { |
| 1886 | for( b = 0; b < n_bits; b++ ) |
| 1887 | stats[b] += mbedtls_mpi_get_bit( &result, b ); |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | if( full_stats ) |
| 1892 | { |
Gilles Peskine | d463edf | 2021-04-13 20:45:05 +0200 | [diff] [blame] | 1893 | for( b = min; b < stats_len; b++ ) |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 1894 | { |
| 1895 | mbedtls_test_set_step( 1000000 + b ); |
| 1896 | /* Assert that each value has been reached at least once. |
| 1897 | * This is almost guaranteed if the iteration count is large |
| 1898 | * enough. This is a very crude way of checking the distribution. |
| 1899 | */ |
| 1900 | TEST_ASSERT( stats[b] > 0 ); |
| 1901 | } |
| 1902 | } |
| 1903 | else |
| 1904 | { |
Gilles Peskine | ceefe5d | 2021-06-02 21:24:04 +0200 | [diff] [blame] | 1905 | int statistically_safe_all_the_way = |
| 1906 | is_significantly_above_a_power_of_2( bound_bytes ); |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 1907 | for( b = 0; b < n_bits; b++ ) |
| 1908 | { |
| 1909 | mbedtls_test_set_step( 1000000 + b ); |
| 1910 | /* Assert that each bit has been set in at least one result and |
| 1911 | * clear in at least one result. Provided that iterations is not |
| 1912 | * too small, it would be extremely unlikely for this not to be |
| 1913 | * the case if the results are uniformly distributed. |
| 1914 | * |
| 1915 | * As an exception, the top bit may legitimately never be set |
| 1916 | * if bound is a power of 2 or only slightly above. |
| 1917 | */ |
Gilles Peskine | ceefe5d | 2021-06-02 21:24:04 +0200 | [diff] [blame] | 1918 | if( statistically_safe_all_the_way || b != n_bits - 1 ) |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 1919 | { |
| 1920 | TEST_ASSERT( stats[b] > 0 ); |
| 1921 | } |
| 1922 | TEST_ASSERT( stats[b] < (size_t) iterations ); |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | exit: |
| 1927 | mbedtls_mpi_free( &upper_bound ); |
| 1928 | mbedtls_mpi_free( &result ); |
| 1929 | mbedtls_free( stats ); |
| 1930 | } |
| 1931 | /* END_CASE */ |
| 1932 | |
Gilles Peskine | 1e918f4 | 2021-03-29 22:14:51 +0200 | [diff] [blame] | 1933 | /* BEGIN_CASE */ |
Gilles Peskine | 422e867 | 2021-04-02 00:02:27 +0200 | [diff] [blame] | 1934 | void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before ) |
Gilles Peskine | 1a7df4e | 2021-04-01 15:57:18 +0200 | [diff] [blame] | 1935 | { |
| 1936 | mbedtls_mpi upper_bound; |
| 1937 | mbedtls_mpi result; |
| 1938 | |
| 1939 | mbedtls_mpi_init( &upper_bound ); |
| 1940 | mbedtls_mpi_init( &result ); |
| 1941 | |
Gilles Peskine | 422e867 | 2021-04-02 00:02:27 +0200 | [diff] [blame] | 1942 | if( before != 0 ) |
| 1943 | { |
| 1944 | /* Set result to sign(before) * 2^(|before|-1) */ |
| 1945 | TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 ); |
| 1946 | if( before < 0 ) |
| 1947 | before = - before; |
| 1948 | TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 ); |
| 1949 | } |
| 1950 | |
Gilles Peskine | 1a7df4e | 2021-04-01 15:57:18 +0200 | [diff] [blame] | 1951 | TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) ); |
| 1952 | TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound, |
| 1953 | bound_bytes->x, bound_bytes->len ) ); |
| 1954 | TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound, |
| 1955 | mbedtls_test_rnd_std_rand, NULL ) ); |
Gilles Peskine | dffc710 | 2021-06-10 15:34:15 +0200 | [diff] [blame] | 1956 | TEST_ASSERT( sign_is_valid( &result ) ); |
Gilles Peskine | 1a7df4e | 2021-04-01 15:57:18 +0200 | [diff] [blame] | 1957 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 ); |
| 1958 | TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 ); |
| 1959 | |
| 1960 | exit: |
| 1961 | mbedtls_mpi_free( &upper_bound ); |
| 1962 | mbedtls_mpi_free( &result ); |
| 1963 | } |
| 1964 | /* END_CASE */ |
| 1965 | |
| 1966 | /* BEGIN_CASE */ |
Gilles Peskine | 1e918f4 | 2021-03-29 22:14:51 +0200 | [diff] [blame] | 1967 | void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret ) |
| 1968 | { |
| 1969 | mbedtls_mpi upper_bound; |
| 1970 | mbedtls_mpi result; |
| 1971 | int actual_ret; |
| 1972 | |
| 1973 | mbedtls_mpi_init( &upper_bound ); |
| 1974 | mbedtls_mpi_init( &result ); |
| 1975 | |
| 1976 | TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound, |
| 1977 | bound_bytes->x, bound_bytes->len ) ); |
| 1978 | actual_ret = mbedtls_mpi_random( &result, min, &upper_bound, |
| 1979 | mbedtls_test_rnd_std_rand, NULL ); |
| 1980 | TEST_EQUAL( expected_ret, actual_ret ); |
| 1981 | |
| 1982 | exit: |
| 1983 | mbedtls_mpi_free( &upper_bound ); |
| 1984 | mbedtls_mpi_free( &result ); |
| 1985 | } |
| 1986 | /* END_CASE */ |
| 1987 | |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 1988 | /* BEGIN_CASE */ |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 1989 | void mpi_core_add_if( char * input_A, char * input_B, |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 1990 | char * input_S4, int carry4, |
| 1991 | char * input_S8, int carry8 ) |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 1992 | { |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 1993 | mbedtls_mpi S4, S8, A, B; |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 1994 | mbedtls_mpi_uint *a = NULL; /* first value to add */ |
| 1995 | mbedtls_mpi_uint *b = NULL; /* second value to add */ |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 1996 | mbedtls_mpi_uint *sum = NULL; |
| 1997 | mbedtls_mpi_uint *d = NULL; /* destination - the in/out first operand */ |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 1998 | |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 1999 | mbedtls_mpi_init( &A ); |
| 2000 | mbedtls_mpi_init( &B ); |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 2001 | mbedtls_mpi_init( &S4 ); |
| 2002 | mbedtls_mpi_init( &S8 ); |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2003 | |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 2004 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) ); |
| 2005 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) ); |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 2006 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &S4, input_S4 ) ); |
| 2007 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &S8, input_S8 ) ); |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2008 | |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 2009 | /* We only need to work with one of (S4, carry4) or (S8, carry8) depending |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2010 | * on sizeof(mbedtls_mpi_uint) |
| 2011 | */ |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 2012 | mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &S4 : &S8; |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2013 | mbedtls_mpi_uint carry = ( sizeof(mbedtls_mpi_uint) == 4 ) ? carry4 : carry8; |
| 2014 | |
| 2015 | /* All of the inputs are +ve (or zero) */ |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 2016 | TEST_EQUAL( 1, A.s ); |
| 2017 | TEST_EQUAL( 1, B.s ); |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2018 | TEST_EQUAL( 1, X->s ); |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2019 | |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 2020 | /* Test cases are such that A <= B, so #limbs should be <= */ |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 2021 | TEST_LE_U( A.n, B.n ); |
| 2022 | TEST_LE_U( X->n, B.n ); |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2023 | |
| 2024 | /* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */ |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2025 | |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 2026 | /* mbedtls_mpi_core_add_if() uses input arrays of mbedtls_mpi_uints which |
| 2027 | * must be the same size. The MPIs we've read in will only have arrays |
| 2028 | * large enough for the number they represent. Therefore we create new |
| 2029 | * raw arrays of mbedtls_mpi_uints and populate them from the MPIs we've |
| 2030 | * just read in. |
| 2031 | * |
| 2032 | * We generated test data such that B was always >= A, so that's how many |
| 2033 | * limbs each of these need. |
| 2034 | */ |
Tom Cosgrove | 1135b20 | 2022-09-02 11:46:18 +0100 | [diff] [blame] | 2035 | size_t limbs = B.n; |
Tom Cosgrove | 1135b20 | 2022-09-02 11:46:18 +0100 | [diff] [blame] | 2036 | size_t bytes = limbs * sizeof(mbedtls_mpi_uint); |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 2037 | |
Tom Cosgrove | 2b17792 | 2022-09-15 14:07:18 +0100 | [diff] [blame] | 2038 | /* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */ |
| 2039 | ASSERT_ALLOC( a, bytes ); |
| 2040 | ASSERT_ALLOC( b, bytes ); |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 2041 | ASSERT_ALLOC( sum, bytes ); |
Tom Cosgrove | 2b17792 | 2022-09-15 14:07:18 +0100 | [diff] [blame] | 2042 | ASSERT_ALLOC( d, bytes ); |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2043 | |
| 2044 | /* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as |
| 2045 | * processed by mbedtls_mpi_core_add_if()) are little endian, we can just |
Tom Cosgrove | 2b17792 | 2022-09-15 14:07:18 +0100 | [diff] [blame] | 2046 | * copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC()) |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2047 | */ |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 2048 | memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) ); |
| 2049 | memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) ); |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 2050 | memcpy( sum, X->p, X->n * sizeof(mbedtls_mpi_uint) ); |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2051 | |
Tom Cosgrove | dbc1561 | 2022-09-15 15:36:23 +0100 | [diff] [blame] | 2052 | /* The test cases have a <= b to avoid repetition, so we test a + b then, |
| 2053 | * if a != b, b + a. If a == b, we can test when a and b are aliased */ |
Tom Cosgrove | 17f1fdc | 2022-09-15 15:23:56 +0100 | [diff] [blame] | 2054 | |
| 2055 | /* a + b */ |
| 2056 | |
| 2057 | /* cond = 0 => d unchanged, no carry */ |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 2058 | memcpy( d, a, bytes ); |
Tom Cosgrove | 1135b20 | 2022-09-02 11:46:18 +0100 | [diff] [blame] | 2059 | TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, b, limbs, 0 ) ); |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 2060 | ASSERT_COMPARE( d, bytes, a, bytes ); |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2061 | |
Tom Cosgrove | 17f1fdc | 2022-09-15 15:23:56 +0100 | [diff] [blame] | 2062 | /* cond = 1 => correct result and carry */ |
Tom Cosgrove | 1135b20 | 2022-09-02 11:46:18 +0100 | [diff] [blame] | 2063 | TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, b, limbs, 1 ) ); |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 2064 | ASSERT_COMPARE( d, bytes, sum, bytes ); |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2065 | |
Tom Cosgrove | 1135b20 | 2022-09-02 11:46:18 +0100 | [diff] [blame] | 2066 | if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 ) |
| 2067 | { |
Tom Cosgrove | dbc1561 | 2022-09-15 15:36:23 +0100 | [diff] [blame] | 2068 | /* a == b, so test where a and b are aliased */ |
| 2069 | |
Tom Cosgrove | 17f1fdc | 2022-09-15 15:23:56 +0100 | [diff] [blame] | 2070 | /* cond = 0 => d unchanged, no carry */ |
Tom Cosgrove | 1135b20 | 2022-09-02 11:46:18 +0100 | [diff] [blame] | 2071 | TEST_EQUAL( 0, mbedtls_mpi_core_add_if( b, b, limbs, 0 ) ); |
| 2072 | ASSERT_COMPARE( b, bytes, B.p, bytes ); |
| 2073 | |
Tom Cosgrove | 17f1fdc | 2022-09-15 15:23:56 +0100 | [diff] [blame] | 2074 | /* cond = 1 => correct result and carry */ |
Tom Cosgrove | 1135b20 | 2022-09-02 11:46:18 +0100 | [diff] [blame] | 2075 | TEST_EQUAL( carry, mbedtls_mpi_core_add_if( b, b, limbs, 1 ) ); |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 2076 | ASSERT_COMPARE( b, bytes, sum, bytes ); |
Tom Cosgrove | 1135b20 | 2022-09-02 11:46:18 +0100 | [diff] [blame] | 2077 | } |
Tom Cosgrove | dbc1561 | 2022-09-15 15:36:23 +0100 | [diff] [blame] | 2078 | else |
| 2079 | { |
| 2080 | /* a != b, so test b + a */ |
| 2081 | |
| 2082 | /* cond = 0 => d unchanged, no carry */ |
| 2083 | memcpy( d, b, bytes ); |
| 2084 | TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, a, limbs, 0 ) ); |
| 2085 | ASSERT_COMPARE( d, bytes, b, bytes ); |
| 2086 | |
| 2087 | /* cond = 1 => correct result and carry */ |
| 2088 | TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, a, limbs, 1 ) ); |
| 2089 | ASSERT_COMPARE( d, bytes, sum, bytes ); |
| 2090 | } |
Tom Cosgrove | 1135b20 | 2022-09-02 11:46:18 +0100 | [diff] [blame] | 2091 | |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2092 | exit: |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 2093 | mbedtls_free( a ); |
| 2094 | mbedtls_free( b ); |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 2095 | mbedtls_free( sum ); |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 2096 | mbedtls_free( d ); |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2097 | |
Tom Cosgrove | 50c477b | 2022-09-15 14:28:30 +0100 | [diff] [blame] | 2098 | mbedtls_mpi_free( &S4 ); |
| 2099 | mbedtls_mpi_free( &S8 ); |
Tom Cosgrove | eceb4cc | 2022-09-02 10:46:09 +0100 | [diff] [blame] | 2100 | mbedtls_mpi_free( &A ); |
| 2101 | mbedtls_mpi_free( &B ); |
Tom Cosgrove | 0cc7865 | 2022-08-23 16:26:52 +0100 | [diff] [blame] | 2102 | } |
| 2103 | /* END_CASE */ |
| 2104 | |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2105 | /* BEGIN_CASE */ |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2106 | void mpi_core_sub( char * input_A, char * input_B, |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 2107 | char * input_X4, char * input_X8, |
| 2108 | int carry ) |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2109 | { |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2110 | mbedtls_mpi A, B, X4, X8; |
| 2111 | mbedtls_mpi_uint *a = NULL; |
| 2112 | mbedtls_mpi_uint *b = NULL; |
| 2113 | mbedtls_mpi_uint *x = NULL; /* expected */ |
| 2114 | mbedtls_mpi_uint *r = NULL; /* result */ |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2115 | |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2116 | mbedtls_mpi_init( &A ); |
| 2117 | mbedtls_mpi_init( &B ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2118 | mbedtls_mpi_init( &X4 ); |
| 2119 | mbedtls_mpi_init( &X8 ); |
| 2120 | |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2121 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) ); |
| 2122 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) ); |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2123 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) ); |
| 2124 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2125 | |
| 2126 | /* All of the inputs are +ve (or zero) */ |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2127 | TEST_EQUAL( 1, A.s ); |
| 2128 | TEST_EQUAL( 1, B.s ); |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2129 | TEST_EQUAL( 1, X4.s ); |
| 2130 | TEST_EQUAL( 1, X8.s ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2131 | |
| 2132 | /* Get the number of limbs we will need */ |
Tom Cosgrove | e2159f2 | 2022-09-15 14:40:10 +0100 | [diff] [blame] | 2133 | size_t limbs = MAX( A.n, B.n ); |
Tom Cosgrove | b0fb17a | 2022-09-01 15:04:43 +0100 | [diff] [blame] | 2134 | size_t bytes = limbs * sizeof(mbedtls_mpi_uint); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2135 | |
| 2136 | /* We only need to work with X4 or X8, depending on sizeof(mbedtls_mpi_uint) */ |
| 2137 | mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8; |
| 2138 | |
| 2139 | /* The result shouldn't have more limbs than the longest input */ |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 2140 | TEST_LE_U( X->n, limbs ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2141 | |
| 2142 | /* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */ |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2143 | |
Tom Cosgrove | 2b17792 | 2022-09-15 14:07:18 +0100 | [diff] [blame] | 2144 | /* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */ |
| 2145 | ASSERT_ALLOC( a, bytes ); |
| 2146 | ASSERT_ALLOC( b, bytes ); |
| 2147 | ASSERT_ALLOC( x, bytes ); |
| 2148 | ASSERT_ALLOC( r, bytes ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2149 | |
| 2150 | /* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2151 | * processed by mbedtls_mpi_core_sub()) are little endian, we can just |
Tom Cosgrove | 2b17792 | 2022-09-15 14:07:18 +0100 | [diff] [blame] | 2152 | * copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC()) |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2153 | */ |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2154 | memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) ); |
| 2155 | memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) ); |
| 2156 | memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2157 | |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2158 | /* 1a) r = a - b => we should get the correct carry */ |
Tom Cosgrove | be7209d | 2022-09-15 14:32:38 +0100 | [diff] [blame] | 2159 | TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, b, limbs ) ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2160 | |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2161 | /* 1b) r = a - b => we should get the correct result */ |
| 2162 | ASSERT_COMPARE( r, bytes, x, bytes ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2163 | |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2164 | /* 2 and 3 test "r may be aliased to a or b" */ |
| 2165 | /* 2a) r = a; r -= b => we should get the correct carry (use r to avoid clobbering a) */ |
| 2166 | memcpy( r, a, bytes ); |
Tom Cosgrove | be7209d | 2022-09-15 14:32:38 +0100 | [diff] [blame] | 2167 | TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, r, b, limbs ) ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2168 | |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2169 | /* 2b) r -= b => we should get the correct result */ |
| 2170 | ASSERT_COMPARE( r, bytes, x, bytes ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2171 | |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2172 | /* 3a) r = b; r = a - r => we should get the correct carry (use r to avoid clobbering b) */ |
| 2173 | memcpy( r, b, bytes ); |
Tom Cosgrove | be7209d | 2022-09-15 14:32:38 +0100 | [diff] [blame] | 2174 | TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, r, limbs ) ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2175 | |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2176 | /* 3b) r = a - b => we should get the correct result */ |
| 2177 | ASSERT_COMPARE( r, bytes, x, bytes ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2178 | |
Tom Cosgrove | f2b3818 | 2022-09-20 09:08:31 +0100 | [diff] [blame] | 2179 | /* 4 tests "r may be aliased to [...] both" */ |
| 2180 | if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 ) |
| 2181 | { |
| 2182 | memcpy( r, b, bytes ); |
| 2183 | TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, r, r, limbs ) ); |
| 2184 | ASSERT_COMPARE( r, bytes, x, bytes ); |
| 2185 | } |
| 2186 | |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2187 | exit: |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2188 | mbedtls_free( a ); |
| 2189 | mbedtls_free( b ); |
| 2190 | mbedtls_free( x ); |
| 2191 | mbedtls_free( r ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2192 | |
Tom Cosgrove | a043aeb | 2022-09-02 10:59:59 +0100 | [diff] [blame] | 2193 | mbedtls_mpi_free( &A ); |
| 2194 | mbedtls_mpi_free( &B ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2195 | mbedtls_mpi_free( &X4 ); |
| 2196 | mbedtls_mpi_free( &X8 ); |
Tom Cosgrove | 2a65b85 | 2022-08-17 05:43:54 +0100 | [diff] [blame] | 2197 | } |
| 2198 | /* END_CASE */ |
| 2199 | |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2200 | /* BEGIN_CASE */ |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2201 | void mpi_core_mla( char * input_A, char * input_B, char * input_S, |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 2202 | char * input_X4, char * input_cy4, |
| 2203 | char * input_X8, char * input_cy8 ) |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2204 | { |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2205 | /* We are testing A += B * s; A, B are MPIs, s is a scalar. |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2206 | * |
Tom Cosgrove | 359feb0 | 2022-09-15 14:52:34 +0100 | [diff] [blame] | 2207 | * However, we encode s as an MPI in the .data file as the test framework |
| 2208 | * currently only supports `int`-typed scalars, and that doesn't cover the |
| 2209 | * full range of `mbedtls_mpi_uint`. |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2210 | * |
| 2211 | * We also have the different results for sizeof(mbedtls_mpi_uint) == 4 or 8. |
| 2212 | */ |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2213 | mbedtls_mpi A, B, S, X4, X8, cy4, cy8; |
| 2214 | mbedtls_mpi_uint *a = NULL; |
| 2215 | mbedtls_mpi_uint *x = NULL; |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2216 | |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2217 | mbedtls_mpi_init( &A ); |
| 2218 | mbedtls_mpi_init( &B ); |
| 2219 | mbedtls_mpi_init( &S ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2220 | mbedtls_mpi_init( &X4 ); |
| 2221 | mbedtls_mpi_init( &X8 ); |
| 2222 | mbedtls_mpi_init( &cy4 ); |
| 2223 | mbedtls_mpi_init( &cy8 ); |
| 2224 | |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2225 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) ); |
| 2226 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) ); |
| 2227 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &S, input_S ) ); |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2228 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) ); |
| 2229 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &cy4, input_cy4 ) ); |
| 2230 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) ); |
| 2231 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &cy8, input_cy8 ) ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2232 | |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2233 | /* The MPI encoding of scalar s must be only 1 limb */ |
| 2234 | TEST_EQUAL( 1, S.n ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2235 | |
| 2236 | /* We only need to work with X4 or X8, and cy4 or cy8, depending on sizeof(mbedtls_mpi_uint) */ |
| 2237 | mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8; |
| 2238 | mbedtls_mpi *cy = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &cy4 : &cy8; |
| 2239 | |
| 2240 | /* The carry should only have one limb */ |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2241 | TEST_EQUAL( 1, cy->n ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2242 | |
| 2243 | /* All of the inputs are +ve (or zero) */ |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2244 | TEST_EQUAL( 1, A.s ); |
| 2245 | TEST_EQUAL( 1, B.s ); |
| 2246 | TEST_EQUAL( 1, S.s ); |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2247 | TEST_EQUAL( 1, X->s ); |
| 2248 | TEST_EQUAL( 1, cy->s ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2249 | |
| 2250 | /* Get the (max) number of limbs we will need */ |
Tom Cosgrove | e2159f2 | 2022-09-15 14:40:10 +0100 | [diff] [blame] | 2251 | size_t limbs = MAX( A.n, B.n ); |
Tom Cosgrove | b0fb17a | 2022-09-01 15:04:43 +0100 | [diff] [blame] | 2252 | size_t bytes = limbs * sizeof(mbedtls_mpi_uint); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2253 | |
| 2254 | /* The result shouldn't have more limbs than the longest input */ |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 2255 | TEST_LE_U( X->n, limbs ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2256 | |
| 2257 | /* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */ |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2258 | |
Tom Cosgrove | 2b17792 | 2022-09-15 14:07:18 +0100 | [diff] [blame] | 2259 | /* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */ |
| 2260 | ASSERT_ALLOC( a, bytes ); |
| 2261 | ASSERT_ALLOC( x, bytes ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2262 | |
| 2263 | /* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2264 | * processed by mbedtls_mpi_core_mla()) are little endian, we can just |
Tom Cosgrove | 2b17792 | 2022-09-15 14:07:18 +0100 | [diff] [blame] | 2265 | * copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC()). |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2266 | */ |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2267 | memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) ); |
| 2268 | memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2269 | |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2270 | /* 1a) A += B * s => we should get the correct carry */ |
| 2271 | TEST_EQUAL( mbedtls_mpi_core_mla( a, limbs, B.p, B.n, *S.p ), *cy->p ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2272 | |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2273 | /* 1b) A += B * s => we should get the correct result */ |
| 2274 | ASSERT_COMPARE( a, bytes, x, bytes ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2275 | |
Tom Cosgrove | b0b77e1 | 2022-09-20 13:33:40 +0100 | [diff] [blame] | 2276 | if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 ) |
| 2277 | { |
| 2278 | /* Check when A and B are aliased */ |
| 2279 | memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) ); |
| 2280 | TEST_EQUAL( mbedtls_mpi_core_mla( a, limbs, a, limbs, *S.p ), *cy->p ); |
| 2281 | ASSERT_COMPARE( a, bytes, x, bytes ); |
| 2282 | } |
| 2283 | |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2284 | exit: |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2285 | mbedtls_free( a ); |
| 2286 | mbedtls_free( x ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2287 | |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2288 | mbedtls_mpi_free( &A ); |
| 2289 | mbedtls_mpi_free( &B ); |
| 2290 | mbedtls_mpi_free( &S ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2291 | mbedtls_mpi_free( &X4 ); |
| 2292 | mbedtls_mpi_free( &X8 ); |
Tom Cosgrove | 42dfac6 | 2022-09-02 11:16:39 +0100 | [diff] [blame] | 2293 | mbedtls_mpi_free( &cy4 ); |
| 2294 | mbedtls_mpi_free( &cy8 ); |
Tom Cosgrove | 659c84a | 2022-08-17 05:45:19 +0100 | [diff] [blame] | 2295 | } |
| 2296 | /* END_CASE */ |
| 2297 | |
Tom Cosgrove | 79b70f6 | 2022-08-17 06:17:00 +0100 | [diff] [blame] | 2298 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 2299 | void mpi_montg_init( char * input_N, char * input_mm ) |
Tom Cosgrove | 79b70f6 | 2022-08-17 06:17:00 +0100 | [diff] [blame] | 2300 | { |
| 2301 | mbedtls_mpi N, mm; |
| 2302 | |
| 2303 | mbedtls_mpi_init( &N ); |
| 2304 | mbedtls_mpi_init( &mm ); |
| 2305 | |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2306 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) ); |
| 2307 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &mm, input_mm ) ); |
Tom Cosgrove | 79b70f6 | 2022-08-17 06:17:00 +0100 | [diff] [blame] | 2308 | |
| 2309 | /* The MPI encoding of mm should be 1 limb (sizeof(mbedtls_mpi_uint) == 8) or |
| 2310 | * 2 limbs (sizeof(mbedtls_mpi_uint) == 4). |
| 2311 | * |
| 2312 | * The data file contains the expected result for sizeof(mbedtls_mpi_uint) == 8; |
| 2313 | * for sizeof(mbedtls_mpi_uint) == 4 it's just the LSW of this. |
| 2314 | */ |
Tom Cosgrove | b2c06f4 | 2022-08-24 17:45:58 +0100 | [diff] [blame] | 2315 | TEST_ASSERT( mm.n == 1 || mm.n == 2 ); |
Tom Cosgrove | 79b70f6 | 2022-08-17 06:17:00 +0100 | [diff] [blame] | 2316 | |
| 2317 | /* All of the inputs are +ve (or zero) */ |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2318 | TEST_EQUAL( 1, N.s ); |
| 2319 | TEST_EQUAL( 1, mm.s ); |
Tom Cosgrove | 79b70f6 | 2022-08-17 06:17:00 +0100 | [diff] [blame] | 2320 | |
Tom Cosgrove | b7438d1 | 2022-09-15 15:05:59 +0100 | [diff] [blame] | 2321 | /* mbedtls_mpi_core_montmul_init() only returns a result, no error possible */ |
| 2322 | mbedtls_mpi_uint result = mbedtls_mpi_core_montmul_init( N.p ); |
Tom Cosgrove | 79b70f6 | 2022-08-17 06:17:00 +0100 | [diff] [blame] | 2323 | |
| 2324 | /* Check we got the correct result */ |
| 2325 | TEST_EQUAL( result, mm.p[0] ); |
| 2326 | |
| 2327 | exit: |
| 2328 | mbedtls_mpi_free( &N ); |
| 2329 | mbedtls_mpi_free( &mm ); |
| 2330 | } |
| 2331 | /* END_CASE */ |
| 2332 | |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2333 | /* BEGIN_CASE */ |
Tom Cosgrove | 1b2947a | 2022-09-02 10:24:55 +0100 | [diff] [blame] | 2334 | void mpi_core_montmul( int limbs_AN4, int limbs_B4, |
| 2335 | int limbs_AN8, int limbs_B8, |
| 2336 | char * input_A, |
| 2337 | char * input_B, |
| 2338 | char * input_N, |
| 2339 | char * input_X4, |
| 2340 | char * input_X8 ) |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2341 | { |
Tom Cosgrove | 9384284 | 2022-08-05 16:59:43 +0100 | [diff] [blame] | 2342 | mbedtls_mpi A, B, N, X4, X8, T, R; |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2343 | |
| 2344 | mbedtls_mpi_init( &A ); |
| 2345 | mbedtls_mpi_init( &B ); |
| 2346 | mbedtls_mpi_init( &N ); |
| 2347 | mbedtls_mpi_init( &X4 ); /* expected result, sizeof(mbedtls_mpi_uint) == 4 */ |
| 2348 | mbedtls_mpi_init( &X8 ); /* expected result, sizeof(mbedtls_mpi_uint) == 8 */ |
| 2349 | mbedtls_mpi_init( &T ); |
Tom Cosgrove | 9384284 | 2022-08-05 16:59:43 +0100 | [diff] [blame] | 2350 | mbedtls_mpi_init( &R ); /* for the result */ |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2351 | |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2352 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) ); |
| 2353 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) ); |
| 2354 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) ); |
| 2355 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) ); |
| 2356 | TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) ); |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2357 | |
| 2358 | mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8; |
| 2359 | |
| 2360 | int limbs_AN = ( sizeof(mbedtls_mpi_uint) == 4 ) ? limbs_AN4 : limbs_AN8; |
| 2361 | int limbs_B = ( sizeof(mbedtls_mpi_uint) == 4 ) ? limbs_B4 : limbs_B8; |
| 2362 | |
Tom Cosgrove | 1feb5ac | 2022-09-15 14:22:35 +0100 | [diff] [blame] | 2363 | TEST_LE_U( A.n, (size_t)limbs_AN ); |
| 2364 | TEST_LE_U( X->n, (size_t)limbs_AN ); |
| 2365 | TEST_LE_U( B.n, (size_t)limbs_B ); |
| 2366 | TEST_LE_U( limbs_B, limbs_AN ); |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2367 | |
| 2368 | /* All of the inputs are +ve (or zero) */ |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2369 | TEST_EQUAL( 1, A.s ); |
| 2370 | TEST_EQUAL( 1, B.s ); |
| 2371 | TEST_EQUAL( 1, N.s ); |
| 2372 | TEST_EQUAL( 1, X->s ); |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2373 | |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2374 | TEST_EQUAL( 0, mbedtls_mpi_grow( &A, limbs_AN ) ); |
| 2375 | TEST_EQUAL( 0, mbedtls_mpi_grow( &N, limbs_AN ) ); |
| 2376 | TEST_EQUAL( 0, mbedtls_mpi_grow( X, limbs_AN ) ); |
| 2377 | TEST_EQUAL( 0, mbedtls_mpi_grow( &B, limbs_B ) ); |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2378 | |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2379 | TEST_EQUAL( 0, mbedtls_mpi_grow( &T, limbs_AN * 2 + 1 ) ); |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2380 | |
| 2381 | /* Calculate the Montgomery constant (this is unit tested separately) */ |
Tom Cosgrove | b7438d1 | 2022-09-15 15:05:59 +0100 | [diff] [blame] | 2382 | mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init( N.p ); |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2383 | |
Tom Cosgrove | 9339f05 | 2022-09-01 13:02:53 +0100 | [diff] [blame] | 2384 | TEST_EQUAL( 0, mbedtls_mpi_grow( &R, limbs_AN ) ); /* ensure it's got the right number of limbs */ |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2385 | |
Tom Cosgrove | 9384284 | 2022-08-05 16:59:43 +0100 | [diff] [blame] | 2386 | mbedtls_mpi_core_montmul( R.p, A.p, B.p, B.n, N.p, N.n, mm, T.p ); |
Tom Cosgrove | b0fb17a | 2022-09-01 15:04:43 +0100 | [diff] [blame] | 2387 | size_t bytes = N.n * sizeof(mbedtls_mpi_uint); |
| 2388 | ASSERT_COMPARE( R.p, bytes, X->p, bytes ); |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2389 | |
Tom Cosgrove | ea45c1d | 2022-09-20 13:17:51 +0100 | [diff] [blame] | 2390 | /* The output (R, above) may be aliased to A - use R to save the value of A */ |
| 2391 | |
| 2392 | memcpy( R.p, A.p, bytes ); |
| 2393 | |
| 2394 | mbedtls_mpi_core_montmul( A.p, A.p, B.p, B.n, N.p, N.n, mm, T.p ); |
| 2395 | ASSERT_COMPARE( A.p, bytes, X->p, bytes ); |
| 2396 | |
| 2397 | memcpy( A.p, R.p, bytes ); /* restore A */ |
| 2398 | |
| 2399 | /* The output may be aliased to N - use R to save the value of N */ |
| 2400 | |
| 2401 | memcpy( R.p, N.p, bytes ); |
| 2402 | |
| 2403 | mbedtls_mpi_core_montmul( N.p, A.p, B.p, B.n, N.p, N.n, mm, T.p ); |
| 2404 | ASSERT_COMPARE( N.p, bytes, X->p, bytes ); |
| 2405 | |
| 2406 | memcpy( N.p, R.p, bytes ); |
| 2407 | |
Tom Cosgrove | ea45c1d | 2022-09-20 13:17:51 +0100 | [diff] [blame] | 2408 | if (limbs_AN == limbs_B) |
| 2409 | { |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 2410 | /* Test when A aliased to B (requires A == B on input values) */ |
| 2411 | if ( memcmp( A.p, B.p, bytes ) == 0 ) |
| 2412 | { |
| 2413 | /* Test with A aliased to B and output, since this is permitted - |
| 2414 | * don't bother with yet another test with only A and B aliased */ |
| 2415 | |
| 2416 | mbedtls_mpi_core_montmul( B.p, B.p, B.p, B.n, N.p, N.n, mm, T.p ); |
| 2417 | ASSERT_COMPARE( B.p, bytes, X->p, bytes ); |
| 2418 | |
| 2419 | memcpy( B.p, A.p, bytes ); /* restore B from equal value A */ |
| 2420 | } |
| 2421 | |
| 2422 | /* The output may be aliased to B - last test, so we don't save B */ |
| 2423 | |
Tom Cosgrove | ea45c1d | 2022-09-20 13:17:51 +0100 | [diff] [blame] | 2424 | mbedtls_mpi_core_montmul( B.p, A.p, B.p, B.n, N.p, N.n, mm, T.p ); |
| 2425 | ASSERT_COMPARE( B.p, bytes, X->p, bytes ); |
| 2426 | } |
| 2427 | |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2428 | exit: |
| 2429 | mbedtls_mpi_free( &A ); |
| 2430 | mbedtls_mpi_free( &B ); |
| 2431 | mbedtls_mpi_free( &N ); |
| 2432 | mbedtls_mpi_free( &X4 ); |
| 2433 | mbedtls_mpi_free( &X8 ); |
| 2434 | mbedtls_mpi_free( &T ); |
Tom Cosgrove | 9384284 | 2022-08-05 16:59:43 +0100 | [diff] [blame] | 2435 | mbedtls_mpi_free( &R ); |
Tom Cosgrove | f334d96 | 2022-08-17 06:29:32 +0100 | [diff] [blame] | 2436 | } |
| 2437 | /* END_CASE */ |
| 2438 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2439 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 2440 | void mpi_selftest( ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 2441 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 2442 | TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 2443 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 2444 | /* END_CASE */ |