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 | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 4 | |
Chris Jones | e64a46f | 2020-12-03 17:44:03 +0000 | [diff] [blame] | 5 | #if MBEDTLS_MPI_MAX_BITS > 792 |
| 6 | #define MPI_MAX_BITS_LARGER_THAN_792 |
Chris Jones | 4592bd8 | 2020-12-03 14:24:33 +0000 | [diff] [blame] | 7 | #endif |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 8 | |
| 9 | typedef struct mbedtls_test_mpi_random |
| 10 | { |
| 11 | data_t *data; |
| 12 | size_t pos; |
| 13 | size_t chunk_len; |
| 14 | } mbedtls_test_mpi_random; |
| 15 | |
| 16 | /* |
| 17 | * This function is called by the Miller-Rabin primality test each time it |
| 18 | * chooses a random witness. The witnesses (or non-witnesses as provided by the |
| 19 | * test) are stored in the data member of the state structure. Each number is in |
| 20 | * the format that mbedtls_mpi_read_string understands and is chunk_len long. |
| 21 | */ |
| 22 | int mbedtls_test_mpi_miller_rabin_determinizer( void* state, |
| 23 | unsigned char* buf, |
| 24 | size_t len ) |
| 25 | { |
| 26 | mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state; |
| 27 | |
| 28 | if( random == NULL || random->data->x == NULL || buf == NULL ) |
| 29 | return( -1 ); |
| 30 | |
| 31 | if( random->pos + random->chunk_len > random->data->len |
| 32 | || random->chunk_len > len ) |
| 33 | { |
| 34 | return( -1 ); |
| 35 | } |
| 36 | |
| 37 | memset( buf, 0, len ); |
| 38 | |
| 39 | /* The witness is written to the end of the buffer, since the buffer is |
| 40 | * used as big endian, unsigned binary data in mbedtls_mpi_read_binary. |
| 41 | * Writing the witness to the start of the buffer would result in the |
| 42 | * buffer being 'witness 000...000', which would be treated as |
| 43 | * witness * 2^n for some n. */ |
| 44 | memcpy( buf + len - random->chunk_len, &random->data->x[random->pos], |
| 45 | random->chunk_len ); |
| 46 | |
| 47 | random->pos += random->chunk_len; |
| 48 | |
| 49 | return( 0 ); |
| 50 | } |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 51 | |
| 52 | /* Random generator that is told how many bytes to return. */ |
| 53 | static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len ) |
| 54 | { |
| 55 | size_t *bytes_left = state; |
| 56 | size_t i; |
| 57 | for( i = 0; i < len; i++ ) |
| 58 | { |
| 59 | if( *bytes_left == 0 ) |
| 60 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 61 | buf[i] = *bytes_left & 0xff; |
| 62 | --( *bytes_left ); |
| 63 | } |
| 64 | return( 0 ); |
| 65 | } |
| 66 | |
Gilles Peskine | eedefa5 | 2021-04-13 19:50:04 +0200 | [diff] [blame] | 67 | /* Test whether bytes represents (in big-endian base 256) a number b that |
| 68 | * is significantly above a power of 2. That is, b must not have a long run |
| 69 | * of unset bits after the most significant bit. |
| 70 | * |
| 71 | * Let n be the bit-size of b, i.e. the integer such that 2^n <= b < 2^{n+1}. |
| 72 | * This function returns 1 if, when drawing a number between 0 and b, |
| 73 | * the probability that this number is at least 2^n is not negligible. |
| 74 | * This probability is (b - 2^n) / b and this function checks that this |
| 75 | * number is above some threshold A. The threshold value is heuristic and |
| 76 | * based on the needs of mpi_random_many(). |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 77 | */ |
| 78 | static int is_significantly_above_a_power_of_2( data_t *bytes ) |
| 79 | { |
| 80 | const uint8_t *p = bytes->x; |
| 81 | size_t len = bytes->len; |
| 82 | unsigned x; |
Gilles Peskine | eedefa5 | 2021-04-13 19:50:04 +0200 | [diff] [blame] | 83 | |
| 84 | /* Skip leading null bytes */ |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 85 | while( len > 0 && p[0] == 0 ) |
| 86 | { |
| 87 | ++p; |
| 88 | --len; |
| 89 | } |
Gilles Peskine | eedefa5 | 2021-04-13 19:50:04 +0200 | [diff] [blame] | 90 | /* 0 is not significantly above a power of 2 */ |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 91 | if( len == 0 ) |
| 92 | return( 0 ); |
Gilles Peskine | eedefa5 | 2021-04-13 19:50:04 +0200 | [diff] [blame] | 93 | /* Extract the (up to) 2 most significant bytes */ |
| 94 | if( len == 1 ) |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 95 | x = p[0]; |
| 96 | else |
| 97 | x = ( p[0] << 8 ) | p[1]; |
| 98 | |
Gilles Peskine | eedefa5 | 2021-04-13 19:50:04 +0200 | [diff] [blame] | 99 | /* Shift the most significant bit of x to position 8 and mask it out */ |
| 100 | while( ( x & 0xfe00 ) != 0 ) |
| 101 | x >>= 1; |
| 102 | x &= 0x00ff; |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 103 | |
Gilles Peskine | eedefa5 | 2021-04-13 19:50:04 +0200 | [diff] [blame] | 104 | /* At this point, x = floor((b - 2^n) / 2^(n-8)). b is significantly above |
| 105 | * a power of 2 iff x is significantly above 0 compared to 2^8. |
| 106 | * Testing x >= 2^4 amounts to picking A = 1/16 in the function |
| 107 | * description above. */ |
| 108 | return( x >= 0x10 ); |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 111 | /* END_HEADER */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 112 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 113 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | * depends_on:MBEDTLS_BIGNUM_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 115 | * END_DEPENDENCIES |
| 116 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 117 | |
Hanno Becker | b48e1aa | 2018-12-18 23:25:01 +0000 | [diff] [blame] | 118 | /* BEGIN_CASE */ |
| 119 | void mpi_valid_param( ) |
| 120 | { |
| 121 | TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) ); |
| 122 | } |
| 123 | /* END_CASE */ |
| 124 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 125 | /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ |
| 126 | void mpi_invalid_param( ) |
| 127 | { |
| 128 | mbedtls_mpi X; |
| 129 | const char *s_in = "00101000101010"; |
| 130 | char s_out[16] = { 0 }; |
| 131 | unsigned char u_out[16] = { 0 }; |
| 132 | unsigned char u_in[16] = { 0 }; |
| 133 | size_t olen; |
| 134 | mbedtls_mpi_uint mpi_uint; |
| 135 | |
| 136 | TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 137 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 138 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 139 | mbedtls_mpi_grow( NULL, 42 ) ); |
| 140 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 141 | mbedtls_mpi_copy( NULL, &X ) ); |
| 142 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 143 | mbedtls_mpi_copy( &X, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 144 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 145 | TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) ); |
| 146 | TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 147 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 148 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 149 | mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) ); |
| 150 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 151 | mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 152 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 153 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 154 | mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) ); |
| 155 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 156 | mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 157 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 158 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 159 | mbedtls_mpi_lset( NULL, 42 ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 160 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 161 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 162 | mbedtls_mpi_get_bit( NULL, 42 ) ); |
| 163 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 164 | mbedtls_mpi_set_bit( NULL, 42, 0 ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 165 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 166 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 167 | mbedtls_mpi_read_string( NULL, 2, s_in ) ); |
| 168 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 169 | mbedtls_mpi_read_string( &X, 2, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 170 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 171 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 172 | mbedtls_mpi_write_string( NULL, 2, |
| 173 | s_out, sizeof( s_out ), |
| 174 | &olen ) ); |
| 175 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 176 | mbedtls_mpi_write_string( &X, 2, |
| 177 | NULL, sizeof( s_out ), |
| 178 | &olen ) ); |
| 179 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 180 | mbedtls_mpi_write_string( &X, 2, |
| 181 | s_out, sizeof( s_out ), |
| 182 | NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 183 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 184 | #if defined(MBEDTLS_FS_IO) |
| 185 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 186 | mbedtls_mpi_read_file( NULL, 2, stdin ) ); |
| 187 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 188 | mbedtls_mpi_read_file( &X, 2, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 189 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 190 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 191 | mbedtls_mpi_write_file( "", NULL, 2, NULL ) ); |
| 192 | #endif /* MBEDTLS_FS_IO */ |
| 193 | |
| 194 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 195 | mbedtls_mpi_read_binary( NULL, u_in, |
| 196 | sizeof( u_in ) ) ); |
| 197 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 198 | mbedtls_mpi_read_binary( &X, NULL, |
| 199 | sizeof( u_in ) ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 200 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 201 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 202 | mbedtls_mpi_write_binary( NULL, u_out, |
| 203 | sizeof( u_out ) ) ); |
| 204 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 205 | mbedtls_mpi_write_binary( &X, NULL, |
| 206 | sizeof( u_out ) ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 207 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 208 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 209 | mbedtls_mpi_shift_l( NULL, 42 ) ); |
| 210 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 211 | mbedtls_mpi_shift_r( NULL, 42 ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 212 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 213 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 214 | mbedtls_mpi_cmp_abs( NULL, &X ) ); |
| 215 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 216 | mbedtls_mpi_cmp_abs( &X, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 217 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 218 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 219 | mbedtls_mpi_cmp_mpi( NULL, &X ) ); |
| 220 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 221 | mbedtls_mpi_cmp_mpi( &X, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 222 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 223 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 224 | mbedtls_mpi_cmp_int( NULL, 42 ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 225 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 226 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 227 | mbedtls_mpi_add_abs( NULL, &X, &X ) ); |
| 228 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 229 | mbedtls_mpi_add_abs( &X, NULL, &X ) ); |
| 230 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 231 | mbedtls_mpi_add_abs( &X, &X, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 232 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 233 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 234 | mbedtls_mpi_sub_abs( NULL, &X, &X ) ); |
| 235 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 236 | mbedtls_mpi_sub_abs( &X, NULL, &X ) ); |
| 237 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 238 | mbedtls_mpi_sub_abs( &X, &X, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 239 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 240 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 241 | mbedtls_mpi_add_mpi( NULL, &X, &X ) ); |
| 242 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 243 | mbedtls_mpi_add_mpi( &X, NULL, &X ) ); |
| 244 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 245 | mbedtls_mpi_add_mpi( &X, &X, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 246 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 247 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 248 | mbedtls_mpi_sub_mpi( NULL, &X, &X ) ); |
| 249 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 250 | mbedtls_mpi_sub_mpi( &X, NULL, &X ) ); |
| 251 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 252 | mbedtls_mpi_sub_mpi( &X, &X, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 253 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 254 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 255 | mbedtls_mpi_add_int( NULL, &X, 42 ) ); |
| 256 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 257 | mbedtls_mpi_add_int( &X, NULL, 42 ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 258 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 259 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 260 | mbedtls_mpi_sub_int( NULL, &X, 42 ) ); |
| 261 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 262 | mbedtls_mpi_sub_int( &X, NULL, 42 ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 263 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 264 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 265 | mbedtls_mpi_mul_mpi( NULL, &X, &X ) ); |
| 266 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 267 | mbedtls_mpi_mul_mpi( &X, NULL, &X ) ); |
| 268 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 269 | mbedtls_mpi_mul_mpi( &X, &X, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 270 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 271 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 272 | mbedtls_mpi_mul_int( NULL, &X, 42 ) ); |
| 273 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 274 | mbedtls_mpi_mul_int( &X, NULL, 42 ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 275 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 276 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 277 | mbedtls_mpi_div_mpi( &X, &X, NULL, &X ) ); |
| 278 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 279 | mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 280 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 281 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 282 | mbedtls_mpi_div_int( &X, &X, NULL, 42 ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 283 | |
Hanno Becker | f25ee7f | 2018-12-19 16:51:02 +0000 | [diff] [blame] | 284 | TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) ); |
| 285 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 286 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 287 | mbedtls_mpi_mod_mpi( NULL, &X, &X ) ); |
| 288 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 289 | mbedtls_mpi_mod_mpi( &X, NULL, &X ) ); |
| 290 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 291 | mbedtls_mpi_mod_mpi( &X, &X, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 292 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 293 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 294 | mbedtls_mpi_mod_int( NULL, &X, 42 ) ); |
| 295 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 296 | mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 297 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 298 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 299 | mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) ); |
| 300 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 301 | mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) ); |
| 302 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 303 | mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) ); |
| 304 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 305 | mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 306 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 307 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 308 | mbedtls_mpi_fill_random( NULL, 42, |
| 309 | mbedtls_test_rnd_std_rand, |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 310 | NULL ) ); |
| 311 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 312 | mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 313 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 314 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 315 | mbedtls_mpi_gcd( NULL, &X, &X ) ); |
| 316 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 317 | mbedtls_mpi_gcd( &X, NULL, &X ) ); |
| 318 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 319 | mbedtls_mpi_gcd( &X, &X, NULL ) ); |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 320 | |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 321 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 322 | mbedtls_mpi_inv_mod( NULL, &X, &X ) ); |
| 323 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
| 324 | mbedtls_mpi_inv_mod( &X, NULL, &X ) ); |
| 325 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, |
Hanno Becker | e118504 | 2018-12-13 14:31:46 +0000 | [diff] [blame] | 326 | mbedtls_mpi_inv_mod( &X, &X, NULL ) ); |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 327 | |
| 328 | exit: |
| 329 | return; |
Hanno Becker | afb607b | 2018-12-11 14:27:08 +0000 | [diff] [blame] | 330 | } |
| 331 | /* END_CASE */ |
| 332 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 333 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 334 | void mpi_null( ) |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 335 | { |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 336 | mbedtls_mpi X, Y, Z; |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 337 | |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 338 | mbedtls_mpi_init( &X ); |
| 339 | mbedtls_mpi_init( &Y ); |
| 340 | mbedtls_mpi_init( &Z ); |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 341 | |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 342 | TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 ); |
| 343 | TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 ); |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 344 | TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 ); |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 345 | TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 ); |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 346 | |
| 347 | exit: |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 348 | mbedtls_mpi_free( &X ); |
Manuel Pégourié-Gonnard | 770b5e1 | 2015-04-29 17:02:01 +0200 | [diff] [blame] | 349 | } |
| 350 | /* END_CASE */ |
| 351 | |
| 352 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 353 | void mpi_read_write_string( int radix_X, char * input_X, int radix_A, |
| 354 | char * input_A, int output_size, int result_read, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 355 | int result_write ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 356 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 357 | mbedtls_mpi X; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 358 | char str[1000]; |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 359 | size_t len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 360 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 361 | mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 362 | |
Janos Follath | 04dadb7 | 2019-03-06 12:29:37 +0000 | [diff] [blame] | 363 | memset( str, '!', sizeof( str ) ); |
| 364 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 365 | 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] | 366 | if( result_read == 0 ) |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 367 | { |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 368 | 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] | 369 | if( result_write == 0 ) |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 370 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 371 | TEST_ASSERT( strcasecmp( str, input_A ) == 0 ); |
Janos Follath | 04dadb7 | 2019-03-06 12:29:37 +0000 | [diff] [blame] | 372 | TEST_ASSERT( str[len] == '!' ); |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 375 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 376 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 377 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 378 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 379 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 380 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 381 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 382 | void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 383 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 384 | mbedtls_mpi X; |
Janos Follath | e5670f2 | 2019-02-25 16:11:58 +0000 | [diff] [blame] | 385 | char str[1000]; |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 386 | size_t len; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 387 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 388 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 389 | |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 390 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 391 | TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 ); |
Janos Follath | e5670f2 | 2019-02-25 16:11:58 +0000 | [diff] [blame] | 392 | TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 393 | TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 394 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 395 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 396 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 397 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 398 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 399 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 400 | /* BEGIN_CASE */ |
Janos Follath | a778a94 | 2019-02-13 10:28:28 +0000 | [diff] [blame] | 401 | void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A ) |
| 402 | { |
| 403 | mbedtls_mpi X; |
Janos Follath | e5670f2 | 2019-02-25 16:11:58 +0000 | [diff] [blame] | 404 | char str[1000]; |
Janos Follath | a778a94 | 2019-02-13 10:28:28 +0000 | [diff] [blame] | 405 | size_t len; |
| 406 | |
| 407 | mbedtls_mpi_init( &X ); |
| 408 | |
| 409 | |
| 410 | TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 ); |
Janos Follath | e5670f2 | 2019-02-25 16:11:58 +0000 | [diff] [blame] | 411 | TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 ); |
Janos Follath | a778a94 | 2019-02-13 10:28:28 +0000 | [diff] [blame] | 412 | TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 ); |
| 413 | |
| 414 | exit: |
| 415 | mbedtls_mpi_free( &X ); |
| 416 | } |
| 417 | /* END_CASE */ |
| 418 | |
| 419 | /* BEGIN_CASE */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 420 | void mbedtls_mpi_write_binary( int radix_X, char * input_X, |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 421 | data_t * input_A, int output_size, |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 422 | int result ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 423 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 424 | mbedtls_mpi X; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 425 | unsigned char buf[1000]; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 426 | size_t buflen; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 427 | |
| 428 | memset( buf, 0x00, 1000 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 429 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 430 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | e670f90 | 2015-10-30 09:23:19 +0100 | [diff] [blame] | 433 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 434 | buflen = mbedtls_mpi_size( &X ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 435 | if( buflen > (size_t) output_size ) |
| 436 | buflen = (size_t) output_size; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 438 | TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 439 | if( result == 0) |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 440 | { |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 441 | |
Ronald Cron | 2dbba99 | 2020-06-10 11:42:32 +0200 | [diff] [blame] | 442 | TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x, |
| 443 | buflen, input_A->len ) == 0 ); |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 444 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 445 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 446 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 447 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 448 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 449 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 450 | |
Janos Follath | e344d0f | 2019-02-19 16:17:40 +0000 | [diff] [blame] | 451 | /* BEGIN_CASE */ |
| 452 | void mbedtls_mpi_write_binary_le( int radix_X, char * input_X, |
| 453 | data_t * input_A, int output_size, |
| 454 | int result ) |
| 455 | { |
| 456 | mbedtls_mpi X; |
| 457 | unsigned char buf[1000]; |
| 458 | size_t buflen; |
| 459 | |
| 460 | memset( buf, 0x00, 1000 ); |
| 461 | |
| 462 | mbedtls_mpi_init( &X ); |
| 463 | |
| 464 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 465 | |
| 466 | buflen = mbedtls_mpi_size( &X ); |
| 467 | if( buflen > (size_t) output_size ) |
| 468 | buflen = (size_t) output_size; |
| 469 | |
| 470 | TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result ); |
| 471 | if( result == 0) |
| 472 | { |
| 473 | |
Ronald Cron | 2dbba99 | 2020-06-10 11:42:32 +0200 | [diff] [blame] | 474 | TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x, |
| 475 | buflen, input_A->len ) == 0 ); |
Janos Follath | e344d0f | 2019-02-19 16:17:40 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | exit: |
| 479 | mbedtls_mpi_free( &X ); |
| 480 | } |
| 481 | /* END_CASE */ |
| 482 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 483 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 484 | void mbedtls_mpi_read_file( int radix_X, char * input_file, |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 485 | data_t * input_A, int result ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 486 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 487 | mbedtls_mpi X; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 488 | unsigned char buf[1000]; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 489 | size_t buflen; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 490 | FILE *file; |
Manuel Pégourié-Gonnard | e43187d | 2015-02-14 16:01:34 +0000 | [diff] [blame] | 491 | int ret; |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 492 | |
| 493 | memset( buf, 0x00, 1000 ); |
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_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 496 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 497 | file = fopen( input_file, "r" ); |
Paul Bakker | 8a0c0a9 | 2014-04-17 16:08:20 +0200 | [diff] [blame] | 498 | TEST_ASSERT( file != NULL ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 499 | ret = mbedtls_mpi_read_file( &X, radix_X, file ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 500 | fclose(file); |
Manuel Pégourié-Gonnard | e43187d | 2015-02-14 16:01:34 +0000 | [diff] [blame] | 501 | TEST_ASSERT( ret == result ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 502 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 503 | if( result == 0 ) |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 504 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 505 | buflen = mbedtls_mpi_size( &X ); |
| 506 | TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 507 | |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 508 | |
Ronald Cron | 2dbba99 | 2020-06-10 11:42:32 +0200 | [diff] [blame] | 509 | TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x, |
| 510 | buflen, input_A->len ) == 0 ); |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 511 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 512 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 513 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 514 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 515 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 516 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 517 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 518 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 519 | void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix, |
| 520 | char * output_file ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 521 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 522 | mbedtls_mpi X, Y; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 523 | FILE *file_out, *file_in; |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 524 | int ret; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 525 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 526 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 527 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 528 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 529 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 530 | file_out = fopen( output_file, "w" ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 531 | TEST_ASSERT( file_out != NULL ); |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 532 | ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 533 | fclose(file_out); |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 534 | TEST_ASSERT( ret == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 535 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 536 | file_in = fopen( output_file, "r" ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 537 | TEST_ASSERT( file_in != NULL ); |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 538 | ret = mbedtls_mpi_read_file( &Y, output_radix, file_in ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 539 | fclose(file_in); |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 540 | TEST_ASSERT( ret == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 541 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 543 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 544 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 545 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 546 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 547 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 548 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 549 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 550 | void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val ) |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 551 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 552 | mbedtls_mpi X; |
| 553 | mbedtls_mpi_init( &X ); |
| 554 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 555 | TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 556 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 557 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 558 | mbedtls_mpi_free( &X ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 559 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 560 | /* END_CASE */ |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 561 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 562 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 563 | void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val, |
| 564 | int radix_Y, char * output_Y, int result ) |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 565 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 566 | mbedtls_mpi X, Y; |
| 567 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 568 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 569 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 570 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 ); |
Paul Bakker | ec5ceb6 | 2016-07-14 12:47:07 +0100 | [diff] [blame] | 571 | TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result ); |
| 572 | |
| 573 | if( result == 0 ) |
| 574 | { |
| 575 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); |
| 576 | } |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 577 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 578 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 579 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 580 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 581 | /* END_CASE */ |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 582 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 583 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 584 | void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 585 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 586 | mbedtls_mpi X; |
| 587 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 588 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 589 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 590 | TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 591 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 592 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 593 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 594 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 595 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 596 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 597 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 598 | void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 599 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 600 | mbedtls_mpi X; |
| 601 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 602 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 603 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 604 | TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 605 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 606 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 607 | mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 608 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 609 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 610 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 611 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 612 | void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y, |
| 613 | char * input_Y, int radix_A, char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 614 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 615 | mbedtls_mpi A, X, Y, Z; |
| 616 | 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] | 617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 618 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 619 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); |
| 620 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 621 | TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 ); |
| 622 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 623 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 624 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | 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] | 626 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 627 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 628 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 629 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 631 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 632 | mbedtls_mpi X; |
| 633 | mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 634 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0); |
| 636 | TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 637 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 638 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 639 | mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 640 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 641 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 642 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 643 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 644 | void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y, |
| 645 | char * input_Y, int input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 646 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 647 | mbedtls_mpi X, Y; |
| 648 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 650 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 651 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); |
| 652 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 653 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 654 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 655 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 656 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 657 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 658 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 659 | /* BEGIN_CASE */ |
Janos Follath | b7e1b49 | 2019-10-14 09:21:49 +0100 | [diff] [blame] | 660 | void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X, |
| 661 | int size_Y, char * input_Y, |
Janos Follath | 0e5532d | 2019-10-11 14:21:53 +0100 | [diff] [blame] | 662 | int input_ret, int input_err ) |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 663 | { |
Gilles Peskine | 0deccf1 | 2020-09-02 15:18:07 +0200 | [diff] [blame] | 664 | unsigned ret = -1; |
Janos Follath | 0e5532d | 2019-10-11 14:21:53 +0100 | [diff] [blame] | 665 | unsigned input_uret = input_ret; |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 666 | mbedtls_mpi X, Y; |
| 667 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
| 668 | |
Janos Follath | b7e1b49 | 2019-10-14 09:21:49 +0100 | [diff] [blame] | 669 | TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 ); |
| 670 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 ); |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 671 | |
Gilles Peskine | 9018b11 | 2020-01-21 16:30:53 +0100 | [diff] [blame] | 672 | TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 ); |
| 673 | TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 ); |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 674 | |
Janos Follath | 0e5532d | 2019-10-11 14:21:53 +0100 | [diff] [blame] | 675 | TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err ); |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 676 | if( input_err == 0 ) |
Janos Follath | 0e5532d | 2019-10-11 14:21:53 +0100 | [diff] [blame] | 677 | TEST_ASSERT( ret == input_uret ); |
Janos Follath | 385d5b8 | 2019-09-11 16:07:14 +0100 | [diff] [blame] | 678 | |
| 679 | exit: |
| 680 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
| 681 | } |
| 682 | /* END_CASE */ |
| 683 | |
| 684 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 685 | void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y, |
| 686 | char * input_Y, int input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 687 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | mbedtls_mpi X, Y; |
| 689 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 691 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 692 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); |
| 693 | TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A ); |
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( &X ); mbedtls_mpi_free( &Y ); |
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 */ |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 701 | void mbedtls_mpi_copy_sint( int input_X, int input_Y ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 702 | { |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 703 | mbedtls_mpi X, Y; |
| 704 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
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 ); |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 707 | TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 ); |
| 708 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 709 | TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 ); |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 710 | TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 ); |
| 711 | TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 712 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 713 | exit: |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 714 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
| 715 | } |
| 716 | /* END_CASE */ |
| 717 | |
| 718 | /* BEGIN_CASE */ |
| 719 | void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y ) |
| 720 | { |
| 721 | mbedtls_mpi X, Y, X0; |
| 722 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 ); |
| 723 | |
Gilles Peskine | 9a6ecee | 2020-02-03 16:15:47 +0100 | [diff] [blame] | 724 | TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 ); |
| 725 | TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 ); |
| 726 | TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 ); |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 727 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 ); |
| 728 | |
| 729 | TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 ); |
| 730 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 ); |
| 731 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 ); |
| 732 | |
| 733 | exit: |
| 734 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 735 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 736 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 737 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 738 | /* BEGIN_CASE */ |
| 739 | void mpi_copy_self( int input_X ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 740 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | mbedtls_mpi X; |
| 742 | mbedtls_mpi_init( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 743 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 744 | TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 ); |
| 745 | TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 ); |
| 746 | TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 747 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 748 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 749 | mbedtls_mpi_free( &X ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 750 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 751 | /* END_CASE */ |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 752 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 753 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 754 | void mbedtls_mpi_shrink( int before, int used, int min, int after ) |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 755 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 756 | mbedtls_mpi X; |
| 757 | mbedtls_mpi_init( &X ); |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 758 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 759 | TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 ); |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 760 | TEST_ASSERT( used <= before ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 761 | memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) ); |
| 762 | TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 ); |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 763 | TEST_ASSERT( X.n == (size_t) after ); |
| 764 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 765 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 766 | mbedtls_mpi_free( &X ); |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 767 | } |
| 768 | /* END_CASE */ |
| 769 | |
| 770 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 771 | void mbedtls_mpi_safe_cond_assign( int x_sign, char * x_str, int y_sign, |
| 772 | char * y_str ) |
Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 773 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 774 | mbedtls_mpi X, Y, XX; |
| 775 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX ); |
Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 777 | TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 ); |
Manuel Pégourié-Gonnard | 3e3d2b8 | 2013-11-21 21:12:26 +0100 | [diff] [blame] | 778 | X.s = x_sign; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 779 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 ); |
Manuel Pégourié-Gonnard | 3e3d2b8 | 2013-11-21 21:12:26 +0100 | [diff] [blame] | 780 | Y.s = y_sign; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 781 | TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 ); |
Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 782 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 783 | TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 ); |
| 784 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 ); |
Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 785 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 786 | TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 ); |
| 787 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); |
Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 788 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 789 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 790 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX ); |
Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 791 | } |
| 792 | /* END_CASE */ |
| 793 | |
| 794 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 795 | void mbedtls_mpi_safe_cond_swap( int x_sign, char * x_str, int y_sign, |
| 796 | char * y_str ) |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 797 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 798 | mbedtls_mpi X, Y, XX, YY; |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 800 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
| 801 | mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY ); |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 802 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 803 | TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 ); |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 804 | X.s = x_sign; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 805 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 ); |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 806 | Y.s = y_sign; |
| 807 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 808 | TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 ); |
| 809 | TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 ); |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 810 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 811 | TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 ); |
| 812 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 ); |
| 813 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 ); |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 814 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 815 | TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 ); |
| 816 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 ); |
| 817 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 ); |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 818 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 819 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 820 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
| 821 | mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY ); |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 822 | } |
| 823 | /* END_CASE */ |
| 824 | |
| 825 | /* BEGIN_CASE */ |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 826 | void mbedtls_mpi_swap_sint( int input_X, int input_Y ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 827 | { |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 828 | mbedtls_mpi X, Y; |
| 829 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 830 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 831 | TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 ); |
| 832 | TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 ); |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 833 | TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 ); |
| 834 | TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 ); |
| 835 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 836 | mbedtls_mpi_swap( &X, &Y ); |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 837 | TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 ); |
| 838 | TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 839 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 840 | exit: |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 841 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
| 842 | } |
| 843 | /* END_CASE */ |
| 844 | |
| 845 | /* BEGIN_CASE */ |
| 846 | void mbedtls_mpi_swap_binary( data_t *input_X, data_t *input_Y ) |
| 847 | { |
| 848 | mbedtls_mpi X, Y, X0, Y0; |
| 849 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); |
| 850 | mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 ); |
| 851 | |
Gilles Peskine | 9a6ecee | 2020-02-03 16:15:47 +0100 | [diff] [blame] | 852 | TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 ); |
| 853 | TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 ); |
| 854 | TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 ); |
| 855 | TEST_ASSERT( mbedtls_mpi_read_binary( &Y0, input_Y->x, input_Y->len ) == 0 ); |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 856 | |
| 857 | mbedtls_mpi_swap( &X, &Y ); |
| 858 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 ); |
| 859 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 ); |
| 860 | |
| 861 | exit: |
| 862 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); |
| 863 | mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 ); |
| 864 | } |
| 865 | /* END_CASE */ |
| 866 | |
| 867 | /* BEGIN_CASE */ |
| 868 | void mpi_swap_self( data_t *input_X ) |
| 869 | { |
| 870 | mbedtls_mpi X, X0; |
| 871 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 ); |
| 872 | |
Gilles Peskine | 9a6ecee | 2020-02-03 16:15:47 +0100 | [diff] [blame] | 873 | TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 ); |
| 874 | TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 ); |
Gilles Peskine | 7428b45 | 2020-01-20 21:01:51 +0100 | [diff] [blame] | 875 | |
| 876 | mbedtls_mpi_swap( &X, &X ); |
| 877 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 ); |
| 878 | |
| 879 | exit: |
| 880 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 881 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 882 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 883 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 884 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 885 | void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y, |
| 886 | char * input_Y, int radix_A, char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 887 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 888 | mbedtls_mpi X, Y, Z, A; |
| 889 | 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] | 890 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 891 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 892 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); |
| 893 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 894 | TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 ); |
| 895 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 896 | |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 897 | /* result == first operand */ |
| 898 | TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 ); |
| 899 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
| 900 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 901 | |
| 902 | /* result == second operand */ |
| 903 | TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 ); |
| 904 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); |
| 905 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 906 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 907 | 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] | 908 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 909 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 910 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 911 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 912 | void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A, |
| 913 | char * input_A ) |
Janos Follath | 044a86b | 2015-10-25 10:58:03 +0100 | [diff] [blame] | 914 | { |
| 915 | mbedtls_mpi X, A; |
| 916 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); |
| 917 | |
Janos Follath | 044a86b | 2015-10-25 10:58:03 +0100 | [diff] [blame] | 918 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
Janos Follath | 6cbacec | 2015-10-25 12:29:13 +0100 | [diff] [blame] | 919 | |
| 920 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 921 | TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 ); |
| 922 | TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 ); |
| 923 | |
| 924 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 925 | TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 ); |
| 926 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
| 927 | |
| 928 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
Janos Follath | 044a86b | 2015-10-25 10:58:03 +0100 | [diff] [blame] | 929 | TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 ); |
| 930 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
| 931 | |
| 932 | exit: |
| 933 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); |
| 934 | } |
| 935 | /* END_CASE */ |
| 936 | |
| 937 | |
| 938 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 939 | void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y, |
| 940 | char * input_Y, int radix_A, char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 941 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 942 | mbedtls_mpi X, Y, Z, A; |
| 943 | 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] | 944 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 945 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 946 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); |
| 947 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 948 | TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 ); |
| 949 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 950 | |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 951 | /* result == first operand */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 952 | TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 ); |
| 953 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 954 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 955 | |
| 956 | /* result == second operand */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 957 | TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 ); |
| 958 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 959 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 960 | exit: |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 961 | 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] | 962 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 963 | /* END_CASE */ |
Paul Bakker | ba48cb2 | 2009-07-12 11:01:32 +0000 | [diff] [blame] | 964 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 965 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 966 | void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y, |
| 967 | int radix_A, char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 968 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 969 | mbedtls_mpi X, Z, A; |
| 970 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 972 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 973 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 974 | TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 ); |
| 975 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 976 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 977 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 978 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 979 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 980 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 981 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 982 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 983 | void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y, |
| 984 | char * input_Y, int radix_A, char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 985 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 986 | mbedtls_mpi X, Y, Z, A; |
| 987 | 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] | 988 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 989 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 990 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); |
| 991 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 992 | TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 ); |
| 993 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 994 | |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 995 | /* result == first operand */ |
| 996 | TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 ); |
| 997 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
| 998 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 999 | |
| 1000 | /* result == second operand */ |
| 1001 | TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 ); |
| 1002 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); |
| 1003 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1004 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1005 | 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] | 1006 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1007 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1008 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1009 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1010 | void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y, |
| 1011 | char * input_Y, int radix_A, char * input_A, |
| 1012 | int sub_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1013 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1014 | mbedtls_mpi X, Y, Z, A; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1015 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1016 | 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] | 1017 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1018 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 1019 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); |
| 1020 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
Manuel Pégourié-Gonnard | e670f90 | 2015-10-30 09:23:19 +0100 | [diff] [blame] | 1021 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1022 | res = mbedtls_mpi_sub_abs( &Z, &X, &Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1023 | TEST_ASSERT( res == sub_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1024 | if( res == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1025 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1026 | |
Gilles Peskine | 56f943a | 2020-07-23 01:18:11 +0200 | [diff] [blame] | 1027 | /* result == first operand */ |
| 1028 | TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result ); |
| 1029 | if( sub_result == 0 ) |
| 1030 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
| 1031 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 1032 | |
| 1033 | /* result == second operand */ |
| 1034 | TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result ); |
| 1035 | if( sub_result == 0 ) |
| 1036 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 ); |
| 1037 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1038 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1039 | 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] | 1040 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1041 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1042 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1043 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1044 | void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y, |
| 1045 | int radix_A, char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1046 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1047 | mbedtls_mpi X, Z, A; |
| 1048 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1049 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1050 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 1051 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 1052 | TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 ); |
| 1053 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1054 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1055 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1056 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1057 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1058 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1059 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1060 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1061 | void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y, |
| 1062 | char * input_Y, int radix_A, char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1063 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1064 | mbedtls_mpi X, Y, Z, A; |
| 1065 | 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] | 1066 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1067 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 1068 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); |
| 1069 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 1070 | TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 ); |
| 1071 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1072 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1073 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1074 | 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] | 1075 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1076 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1077 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1078 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1079 | void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y, |
| 1080 | int radix_A, char * input_A, |
| 1081 | char * result_comparison ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1082 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1083 | mbedtls_mpi X, Z, A; |
| 1084 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1085 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1086 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 1087 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 1088 | TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1089 | if( strcmp( result_comparison, "==" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1090 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1091 | else if( strcmp( result_comparison, "!=" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1092 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1093 | else |
| 1094 | TEST_ASSERT( "unknown operator" == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1095 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1096 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1097 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1098 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1099 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1100 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1101 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1102 | void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y, |
| 1103 | char * input_Y, int radix_A, char * input_A, |
| 1104 | int radix_B, char * input_B, int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1105 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1106 | mbedtls_mpi X, Y, Q, R, A, B; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1107 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1108 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); |
| 1109 | mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1111 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 1112 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); |
| 1113 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 1114 | TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 ); |
| 1115 | res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1116 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1117 | if( res == 0 ) |
| 1118 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1119 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 ); |
| 1120 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1121 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1122 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1123 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1124 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); |
| 1125 | mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1126 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1127 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1128 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1129 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1130 | void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y, |
| 1131 | int radix_A, char * input_A, int radix_B, |
| 1132 | char * input_B, int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1133 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1134 | mbedtls_mpi X, Q, R, A, B; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1135 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1136 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A ); |
| 1137 | mbedtls_mpi_init( &B ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1139 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 1140 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 1141 | TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 ); |
| 1142 | res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1143 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1144 | if( res == 0 ) |
| 1145 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1146 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 ); |
| 1147 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1148 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1149 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1150 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1151 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A ); |
| 1152 | mbedtls_mpi_free( &B ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1153 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1154 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1155 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1156 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1157 | void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y, |
| 1158 | char * input_Y, int radix_A, char * input_A, |
| 1159 | int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1160 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1161 | mbedtls_mpi X, Y, A; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1162 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1163 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1164 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1165 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 1166 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); |
| 1167 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 1168 | res = mbedtls_mpi_mod_mpi( &X, &X, &Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1169 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1170 | if( res == 0 ) |
| 1171 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1172 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1173 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1174 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1175 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1176 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1177 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1178 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1179 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1180 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1181 | void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y, |
| 1182 | int input_A, int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1183 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1184 | mbedtls_mpi X; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1185 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1186 | mbedtls_mpi_uint r; |
| 1187 | mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1189 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 1190 | res = mbedtls_mpi_mod_int( &r, &X, input_Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1191 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1192 | if( res == 0 ) |
| 1193 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1194 | TEST_ASSERT( r == (mbedtls_mpi_uint) input_A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1195 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1196 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1197 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1198 | mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1199 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1200 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1201 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1202 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1203 | void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E, |
| 1204 | char * input_E, int radix_N, char * input_N, |
| 1205 | int radix_RR, char * input_RR, int radix_X, |
| 1206 | char * input_X, int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1207 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1208 | mbedtls_mpi A, E, N, RR, Z, X; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1209 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1210 | mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); |
| 1211 | mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1212 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1213 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 1214 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 1215 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 1216 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1217 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1218 | if( strlen( input_RR ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1219 | TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1220 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1221 | res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1222 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1223 | if( res == 0 ) |
| 1224 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1225 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1226 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1227 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1228 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1229 | mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); |
| 1230 | mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1231 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1232 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1233 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1234 | /* BEGIN_CASE */ |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1235 | void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes, |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1236 | int radix_RR, char * input_RR, int exp_result ) |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1237 | { |
| 1238 | mbedtls_mpi A, E, N, RR, Z; |
| 1239 | mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); |
| 1240 | mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); |
| 1241 | |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1242 | /* Set A to 2^(A_bytes - 1) + 1 */ |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1243 | TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1244 | TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1245 | TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 ); |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1246 | |
| 1247 | /* Set E to 2^(E_bytes - 1) + 1 */ |
| 1248 | TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 ); |
| 1249 | TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1250 | TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 ); |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1251 | |
| 1252 | /* Set N to 2^(N_bytes - 1) + 1 */ |
| 1253 | TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 ); |
| 1254 | TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 ); |
Chris Jones | d10b331 | 2020-12-02 10:41:50 +0000 | [diff] [blame] | 1255 | TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 ); |
| 1256 | |
| 1257 | if( strlen( input_RR ) ) |
| 1258 | TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 ); |
| 1259 | |
Chris Jones | aa850cd | 2020-12-03 11:35:41 +0000 | [diff] [blame] | 1260 | 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] | 1261 | |
| 1262 | exit: |
| 1263 | mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); |
| 1264 | mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); |
| 1265 | } |
| 1266 | /* END_CASE */ |
| 1267 | |
| 1268 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1269 | void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y, |
| 1270 | char * input_Y, int radix_A, char * input_A, |
| 1271 | int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1272 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1273 | mbedtls_mpi X, Y, Z, A; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1274 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1275 | 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] | 1276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1277 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 1278 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 ); |
| 1279 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 1280 | res = mbedtls_mpi_inv_mod( &Z, &X, &Y ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1281 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1282 | if( res == 0 ) |
| 1283 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1284 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1285 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1286 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1287 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1288 | 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] | 1289 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1290 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1291 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1292 | /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1293 | void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1294 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1295 | mbedtls_mpi X; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1296 | int res; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1297 | mbedtls_mpi_init( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1298 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1299 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1300 | 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] | 1301 | TEST_ASSERT( res == div_result ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1302 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1303 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1304 | mbedtls_mpi_free( &X ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1305 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1306 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1307 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1308 | /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 1309 | void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses, |
Darryl Green | ac2ead0 | 2018-10-02 15:30:39 +0100 | [diff] [blame] | 1310 | int chunk_len, int rounds ) |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 1311 | { |
| 1312 | mbedtls_mpi X; |
| 1313 | int res; |
| 1314 | mbedtls_test_mpi_random rand; |
| 1315 | |
| 1316 | mbedtls_mpi_init( &X ); |
| 1317 | rand.data = witnesses; |
| 1318 | rand.pos = 0; |
| 1319 | rand.chunk_len = chunk_len; |
| 1320 | |
| 1321 | 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] | 1322 | res = mbedtls_mpi_is_prime_ext( &X, rounds - 1, |
| 1323 | mbedtls_test_mpi_miller_rabin_determinizer, |
| 1324 | &rand ); |
| 1325 | TEST_ASSERT( res == 0 ); |
| 1326 | |
| 1327 | rand.data = witnesses; |
| 1328 | rand.pos = 0; |
| 1329 | rand.chunk_len = chunk_len; |
| 1330 | |
Janos Follath | a0b67c2 | 2018-09-18 14:48:23 +0100 | [diff] [blame] | 1331 | res = mbedtls_mpi_is_prime_ext( &X, rounds, |
| 1332 | mbedtls_test_mpi_miller_rabin_determinizer, |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 1333 | &rand ); |
Darryl Green | ac2ead0 | 2018-10-02 15:30:39 +0100 | [diff] [blame] | 1334 | TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE ); |
Janos Follath | 64eca05 | 2018-09-05 17:04:49 +0100 | [diff] [blame] | 1335 | |
| 1336 | exit: |
| 1337 | mbedtls_mpi_free( &X ); |
| 1338 | } |
| 1339 | /* END_CASE */ |
| 1340 | |
| 1341 | /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */ |
Janos Follath | a3cb7eb | 2018-08-14 15:31:54 +0100 | [diff] [blame] | 1342 | void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret ) |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1343 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1344 | mbedtls_mpi X; |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1345 | int my_ret; |
| 1346 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1347 | mbedtls_mpi_init( &X ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1348 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1349 | my_ret = mbedtls_mpi_gen_prime( &X, bits, flags, |
| 1350 | mbedtls_test_rnd_std_rand, NULL ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1351 | TEST_ASSERT( my_ret == ref_ret ); |
| 1352 | |
| 1353 | if( ref_ret == 0 ) |
| 1354 | { |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 1355 | size_t actual_bits = mbedtls_mpi_bitlen( &X ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1356 | |
| 1357 | TEST_ASSERT( actual_bits >= (size_t) bits ); |
| 1358 | TEST_ASSERT( actual_bits <= (size_t) bits + 1 ); |
| 1359 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1360 | TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, |
| 1361 | mbedtls_test_rnd_std_rand, |
| 1362 | NULL ) == 0 ); |
Janos Follath | a3cb7eb | 2018-08-14 15:31:54 +0100 | [diff] [blame] | 1363 | if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1364 | { |
Hanno Becker | d4d6057 | 2018-01-10 07:12:01 +0000 | [diff] [blame] | 1365 | /* X = ( X - 1 ) / 2 */ |
| 1366 | TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 ); |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1367 | TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40, |
| 1368 | mbedtls_test_rnd_std_rand, |
| 1369 | NULL ) == 0 ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1373 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1374 | mbedtls_mpi_free( &X ); |
Manuel Pégourié-Gonnard | 15f58a8 | 2014-06-16 17:12:40 +0200 | [diff] [blame] | 1375 | } |
| 1376 | /* END_CASE */ |
| 1377 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1378 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1379 | void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X, |
| 1380 | int radix_A, char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1381 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1382 | mbedtls_mpi X, A; |
| 1383 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1384 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1385 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 1386 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 1387 | TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 ); |
| 1388 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1389 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1390 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1391 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1392 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1393 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1394 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1395 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1396 | void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X, |
| 1397 | int radix_A, char * input_A ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1398 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1399 | mbedtls_mpi X, A; |
| 1400 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1401 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1402 | TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); |
| 1403 | TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 ); |
| 1404 | TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 ); |
| 1405 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1406 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1407 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1408 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1409 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1410 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1411 | |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 1412 | /* BEGIN_CASE */ |
Gilles Peskine | 422e867 | 2021-04-02 00:02:27 +0200 | [diff] [blame] | 1413 | void mpi_fill_random( int wanted_bytes, int rng_bytes, |
| 1414 | int before, int expected_ret ) |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 1415 | { |
| 1416 | mbedtls_mpi X; |
| 1417 | int ret; |
| 1418 | size_t bytes_left = rng_bytes; |
| 1419 | mbedtls_mpi_init( &X ); |
| 1420 | |
Gilles Peskine | 422e867 | 2021-04-02 00:02:27 +0200 | [diff] [blame] | 1421 | if( before != 0 ) |
| 1422 | { |
| 1423 | /* Set X to sign(before) * 2^(|before|-1) */ |
| 1424 | TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 ); |
| 1425 | if( before < 0 ) |
| 1426 | before = - before; |
| 1427 | TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 ); |
| 1428 | } |
| 1429 | |
Gilles Peskine | 3cb1e29 | 2020-11-25 15:37:20 +0100 | [diff] [blame] | 1430 | ret = mbedtls_mpi_fill_random( &X, wanted_bytes, |
| 1431 | f_rng_bytes_left, &bytes_left ); |
| 1432 | TEST_ASSERT( ret == expected_ret ); |
| 1433 | |
| 1434 | if( expected_ret == 0 ) |
| 1435 | { |
| 1436 | /* mbedtls_mpi_fill_random is documented to use bytes from the RNG |
| 1437 | * as a big-endian representation of the number. We know when |
| 1438 | * our RNG function returns null bytes, so we know how many |
| 1439 | * leading zero bytes the number has. */ |
| 1440 | size_t leading_zeros = 0; |
| 1441 | if( wanted_bytes > 0 && rng_bytes % 256 == 0 ) |
| 1442 | leading_zeros = 1; |
| 1443 | TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros == |
| 1444 | (size_t) wanted_bytes ); |
| 1445 | TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes ); |
| 1446 | } |
| 1447 | |
| 1448 | exit: |
| 1449 | mbedtls_mpi_free( &X ); |
| 1450 | } |
| 1451 | /* END_CASE */ |
| 1452 | |
Gilles Peskine | 02ac93a | 2021-03-29 22:02:55 +0200 | [diff] [blame] | 1453 | /* BEGIN_CASE */ |
| 1454 | void mpi_random_many( int min, data_t *bound_bytes, int iterations ) |
| 1455 | { |
| 1456 | /* Generate numbers in the range 1..bound-1. Do it iterations times. |
| 1457 | * This function assumes that the value of bound is at least 2 and |
| 1458 | * that iterations is large enough that a one-in-2^iterations chance |
| 1459 | * effectively never occurs. |
| 1460 | */ |
| 1461 | |
| 1462 | mbedtls_mpi upper_bound; |
| 1463 | size_t n_bits; |
| 1464 | mbedtls_mpi result; |
| 1465 | size_t b; |
| 1466 | /* If upper_bound is small, stats[b] is the number of times the value b |
| 1467 | * has been generated. Otherwise stats[b] is the number of times a |
| 1468 | * value with bit b set has been generated. */ |
| 1469 | size_t *stats = NULL; |
| 1470 | size_t stats_len; |
| 1471 | int full_stats; |
| 1472 | size_t i; |
| 1473 | |
| 1474 | mbedtls_mpi_init( &upper_bound ); |
| 1475 | mbedtls_mpi_init( &result ); |
| 1476 | |
| 1477 | TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound, |
| 1478 | bound_bytes->x, bound_bytes->len ) ); |
| 1479 | n_bits = mbedtls_mpi_bitlen( &upper_bound ); |
| 1480 | /* Consider a bound "small" if it's less than 2^5. This value is chosen |
| 1481 | * to be small enough that the probability of missing one value is |
| 1482 | * negligible given the number of iterations. It must be less than |
| 1483 | * 256 because some of the code below assumes that "small" values |
| 1484 | * fit in a byte. */ |
| 1485 | if( n_bits <= 5 ) |
| 1486 | { |
| 1487 | full_stats = 1; |
| 1488 | stats_len = bound_bytes->x[bound_bytes->len - 1]; |
| 1489 | } |
| 1490 | else |
| 1491 | { |
| 1492 | full_stats = 0; |
| 1493 | stats_len = n_bits; |
| 1494 | } |
| 1495 | ASSERT_ALLOC( stats, stats_len ); |
| 1496 | |
| 1497 | for( i = 0; i < (size_t) iterations; i++ ) |
| 1498 | { |
| 1499 | mbedtls_test_set_step( i ); |
| 1500 | TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound, |
| 1501 | mbedtls_test_rnd_std_rand, NULL ) ); |
| 1502 | |
| 1503 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 ); |
| 1504 | TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 ); |
| 1505 | if( full_stats ) |
| 1506 | { |
| 1507 | uint8_t value; |
| 1508 | TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) ); |
| 1509 | TEST_ASSERT( value < stats_len ); |
| 1510 | ++stats[value]; |
| 1511 | } |
| 1512 | else |
| 1513 | { |
| 1514 | for( b = 0; b < n_bits; b++ ) |
| 1515 | stats[b] += mbedtls_mpi_get_bit( &result, b ); |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | if( full_stats ) |
| 1520 | { |
| 1521 | for( b = 1; b < stats_len; b++ ) |
| 1522 | { |
| 1523 | mbedtls_test_set_step( 1000000 + b ); |
| 1524 | /* Assert that each value has been reached at least once. |
| 1525 | * This is almost guaranteed if the iteration count is large |
| 1526 | * enough. This is a very crude way of checking the distribution. |
| 1527 | */ |
| 1528 | TEST_ASSERT( stats[b] > 0 ); |
| 1529 | } |
| 1530 | } |
| 1531 | else |
| 1532 | { |
| 1533 | for( b = 0; b < n_bits; b++ ) |
| 1534 | { |
| 1535 | mbedtls_test_set_step( 1000000 + b ); |
| 1536 | /* Assert that each bit has been set in at least one result and |
| 1537 | * clear in at least one result. Provided that iterations is not |
| 1538 | * too small, it would be extremely unlikely for this not to be |
| 1539 | * the case if the results are uniformly distributed. |
| 1540 | * |
| 1541 | * As an exception, the top bit may legitimately never be set |
| 1542 | * if bound is a power of 2 or only slightly above. |
| 1543 | */ |
| 1544 | if( b != n_bits - 1 || |
| 1545 | is_significantly_above_a_power_of_2( bound_bytes ) ) |
| 1546 | { |
| 1547 | TEST_ASSERT( stats[b] > 0 ); |
| 1548 | } |
| 1549 | TEST_ASSERT( stats[b] < (size_t) iterations ); |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | exit: |
| 1554 | mbedtls_mpi_free( &upper_bound ); |
| 1555 | mbedtls_mpi_free( &result ); |
| 1556 | mbedtls_free( stats ); |
| 1557 | } |
| 1558 | /* END_CASE */ |
| 1559 | |
Gilles Peskine | 1e918f4 | 2021-03-29 22:14:51 +0200 | [diff] [blame] | 1560 | /* BEGIN_CASE */ |
Gilles Peskine | 422e867 | 2021-04-02 00:02:27 +0200 | [diff] [blame] | 1561 | 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] | 1562 | { |
| 1563 | mbedtls_mpi upper_bound; |
| 1564 | mbedtls_mpi result; |
| 1565 | |
| 1566 | mbedtls_mpi_init( &upper_bound ); |
| 1567 | mbedtls_mpi_init( &result ); |
| 1568 | |
Gilles Peskine | 422e867 | 2021-04-02 00:02:27 +0200 | [diff] [blame] | 1569 | if( before != 0 ) |
| 1570 | { |
| 1571 | /* Set result to sign(before) * 2^(|before|-1) */ |
| 1572 | TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 ); |
| 1573 | if( before < 0 ) |
| 1574 | before = - before; |
| 1575 | TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 ); |
| 1576 | } |
| 1577 | |
Gilles Peskine | 1a7df4e | 2021-04-01 15:57:18 +0200 | [diff] [blame] | 1578 | TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) ); |
| 1579 | TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound, |
| 1580 | bound_bytes->x, bound_bytes->len ) ); |
| 1581 | TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound, |
| 1582 | mbedtls_test_rnd_std_rand, NULL ) ); |
| 1583 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 ); |
| 1584 | TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 ); |
| 1585 | |
| 1586 | exit: |
| 1587 | mbedtls_mpi_free( &upper_bound ); |
| 1588 | mbedtls_mpi_free( &result ); |
| 1589 | } |
| 1590 | /* END_CASE */ |
| 1591 | |
| 1592 | /* BEGIN_CASE */ |
Gilles Peskine | 1e918f4 | 2021-03-29 22:14:51 +0200 | [diff] [blame] | 1593 | void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret ) |
| 1594 | { |
| 1595 | mbedtls_mpi upper_bound; |
| 1596 | mbedtls_mpi result; |
| 1597 | int actual_ret; |
| 1598 | |
| 1599 | mbedtls_mpi_init( &upper_bound ); |
| 1600 | mbedtls_mpi_init( &result ); |
| 1601 | |
| 1602 | TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound, |
| 1603 | bound_bytes->x, bound_bytes->len ) ); |
| 1604 | actual_ret = mbedtls_mpi_random( &result, min, &upper_bound, |
| 1605 | mbedtls_test_rnd_std_rand, NULL ); |
| 1606 | TEST_EQUAL( expected_ret, actual_ret ); |
| 1607 | |
| 1608 | exit: |
| 1609 | mbedtls_mpi_free( &upper_bound ); |
| 1610 | mbedtls_mpi_free( &result ); |
| 1611 | } |
| 1612 | /* END_CASE */ |
| 1613 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1614 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1615 | void mpi_selftest( ) |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 1616 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 1617 | TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 ); |
Paul Bakker | e896fea | 2009-07-06 06:40:23 +0000 | [diff] [blame] | 1618 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1619 | /* END_CASE */ |