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/ecp.h" |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 3 | |
Manuel Pégourié-Gonnard | 6c7af4c | 2015-04-03 16:41:52 +0200 | [diff] [blame] | 4 | #define ECP_PF_UNKNOWN -1 |
Manuel Pégourié-Gonnard | 7a28e99 | 2018-10-16 11:22:45 +0200 | [diff] [blame] | 5 | |
| 6 | #define ECP_PT_RESET( x ) \ |
| 7 | mbedtls_ecp_point_free( x ); \ |
| 8 | mbedtls_ecp_point_init( x ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 9 | /* END_HEADER */ |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 10 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 11 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 12 | * depends_on:MBEDTLS_ECP_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 13 | * END_DEPENDENCIES |
| 14 | */ |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 15 | |
Hanno Becker | 57b684f | 2018-12-18 12:50:02 +0000 | [diff] [blame] | 16 | /* BEGIN_CASE */ |
| 17 | void ecp_valid_param( ) |
| 18 | { |
Hanno Becker | 807c107 | 2018-12-18 23:45:14 +0000 | [diff] [blame] | 19 | TEST_VALID_PARAM( mbedtls_ecp_group_free( NULL ) ); |
Hanno Becker | 57b684f | 2018-12-18 12:50:02 +0000 | [diff] [blame] | 20 | TEST_VALID_PARAM( mbedtls_ecp_keypair_free( NULL ) ); |
| 21 | TEST_VALID_PARAM( mbedtls_ecp_point_free( NULL ) ); |
| 22 | |
| 23 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 24 | TEST_VALID_PARAM( mbedtls_ecp_restart_free( NULL ) ); |
| 25 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 26 | |
| 27 | exit: |
| 28 | return; |
| 29 | } |
| 30 | /* END_CASE */ |
| 31 | |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 32 | /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ |
| 33 | void ecp_invalid_param( ) |
| 34 | { |
| 35 | mbedtls_ecp_group grp; |
| 36 | mbedtls_ecp_keypair kp; |
| 37 | mbedtls_ecp_point P; |
| 38 | mbedtls_mpi m; |
| 39 | const char *x = "deadbeef"; |
| 40 | int valid_fmt = MBEDTLS_ECP_PF_UNCOMPRESSED; |
| 41 | int invalid_fmt = 42; |
| 42 | size_t olen; |
| 43 | unsigned char buf[42] = { 0 }; |
| 44 | const unsigned char *null_buf = NULL; |
| 45 | mbedtls_ecp_group_id valid_group = MBEDTLS_ECP_DP_SECP192R1; |
Andrzej Kurek | f389629 | 2019-02-11 03:44:29 -0500 | [diff] [blame] | 46 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Hanno Becker | 0a4fa9b | 2018-12-18 23:45:29 +0000 | [diff] [blame] | 47 | mbedtls_ecp_restart_ctx restart_ctx; |
Andrzej Kurek | f389629 | 2019-02-11 03:44:29 -0500 | [diff] [blame] | 48 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 49 | |
| 50 | TEST_INVALID_PARAM( mbedtls_ecp_point_init( NULL ) ); |
| 51 | TEST_INVALID_PARAM( mbedtls_ecp_keypair_init( NULL ) ); |
Hanno Becker | 807c107 | 2018-12-18 23:45:14 +0000 | [diff] [blame] | 52 | TEST_INVALID_PARAM( mbedtls_ecp_group_init( NULL ) ); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 53 | |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 54 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 55 | TEST_INVALID_PARAM( mbedtls_ecp_restart_init( NULL ) ); |
Hanno Becker | 0a4fa9b | 2018-12-18 23:45:29 +0000 | [diff] [blame] | 56 | TEST_INVALID_PARAM( mbedtls_ecp_check_budget( NULL, &restart_ctx, 42 ) ); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 57 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 58 | |
| 59 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 60 | mbedtls_ecp_copy( NULL, &P ) ); |
| 61 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 62 | mbedtls_ecp_copy( &P, NULL ) ); |
| 63 | |
| 64 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 65 | mbedtls_ecp_group_copy( NULL, &grp ) ); |
| 66 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 67 | mbedtls_ecp_group_copy( &grp, NULL ) ); |
| 68 | |
| 69 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Hanno Becker | 549e455 | 2018-12-18 23:45:43 +0000 | [diff] [blame] | 70 | mbedtls_ecp_gen_privkey( NULL, |
| 71 | &m, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 72 | mbedtls_test_rnd_std_rand, |
Hanno Becker | 549e455 | 2018-12-18 23:45:43 +0000 | [diff] [blame] | 73 | NULL ) ); |
| 74 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 75 | mbedtls_ecp_gen_privkey( &grp, |
| 76 | NULL, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 77 | mbedtls_test_rnd_std_rand, |
Hanno Becker | 549e455 | 2018-12-18 23:45:43 +0000 | [diff] [blame] | 78 | NULL ) ); |
| 79 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 80 | mbedtls_ecp_gen_privkey( &grp, |
| 81 | &m, |
| 82 | NULL, |
| 83 | NULL ) ); |
| 84 | |
| 85 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 86 | mbedtls_ecp_set_zero( NULL ) ); |
| 87 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 88 | mbedtls_ecp_is_zero( NULL ) ); |
| 89 | |
| 90 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 91 | mbedtls_ecp_point_cmp( NULL, &P ) ); |
| 92 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 93 | mbedtls_ecp_point_cmp( &P, NULL ) ); |
| 94 | |
| 95 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 96 | mbedtls_ecp_point_read_string( NULL, 2, |
| 97 | x, x ) ); |
| 98 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 99 | mbedtls_ecp_point_read_string( &P, 2, |
| 100 | NULL, x ) ); |
| 101 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 102 | mbedtls_ecp_point_read_string( &P, 2, |
| 103 | x, NULL ) ); |
| 104 | |
| 105 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 106 | mbedtls_ecp_point_write_binary( NULL, &P, |
| 107 | valid_fmt, |
| 108 | &olen, |
| 109 | buf, sizeof( buf ) ) ); |
| 110 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 111 | mbedtls_ecp_point_write_binary( &grp, NULL, |
| 112 | valid_fmt, |
| 113 | &olen, |
| 114 | buf, sizeof( buf ) ) ); |
| 115 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 116 | mbedtls_ecp_point_write_binary( &grp, &P, |
| 117 | invalid_fmt, |
| 118 | &olen, |
| 119 | buf, sizeof( buf ) ) ); |
| 120 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 121 | mbedtls_ecp_point_write_binary( &grp, &P, |
| 122 | valid_fmt, |
| 123 | NULL, |
| 124 | buf, sizeof( buf ) ) ); |
| 125 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 126 | mbedtls_ecp_point_write_binary( &grp, &P, |
| 127 | valid_fmt, |
| 128 | &olen, |
| 129 | NULL, sizeof( buf ) ) ); |
| 130 | |
| 131 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 132 | mbedtls_ecp_point_read_binary( NULL, &P, buf, |
| 133 | sizeof( buf ) ) ); |
| 134 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 135 | mbedtls_ecp_point_read_binary( &grp, NULL, buf, |
| 136 | sizeof( buf ) ) ); |
| 137 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 138 | mbedtls_ecp_point_read_binary( &grp, &P, NULL, |
| 139 | sizeof( buf ) ) ); |
| 140 | |
| 141 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 142 | mbedtls_ecp_tls_read_point( NULL, &P, |
| 143 | (const unsigned char **) &buf, |
| 144 | sizeof( buf ) ) ); |
| 145 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 146 | mbedtls_ecp_tls_read_point( &grp, NULL, |
| 147 | (const unsigned char **) &buf, |
| 148 | sizeof( buf ) ) ); |
| 149 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 150 | mbedtls_ecp_tls_read_point( &grp, &P, &null_buf, |
| 151 | sizeof( buf ) ) ); |
| 152 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 153 | mbedtls_ecp_tls_read_point( &grp, &P, NULL, |
| 154 | sizeof( buf ) ) ); |
| 155 | |
| 156 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 157 | mbedtls_ecp_tls_write_point( NULL, &P, |
| 158 | valid_fmt, |
| 159 | &olen, |
| 160 | buf, |
| 161 | sizeof( buf ) ) ); |
| 162 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 163 | mbedtls_ecp_tls_write_point( &grp, NULL, |
| 164 | valid_fmt, |
| 165 | &olen, |
| 166 | buf, |
| 167 | sizeof( buf ) ) ); |
| 168 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 169 | mbedtls_ecp_tls_write_point( &grp, &P, |
| 170 | invalid_fmt, |
| 171 | &olen, |
| 172 | buf, |
| 173 | sizeof( buf ) ) ); |
| 174 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 175 | mbedtls_ecp_tls_write_point( &grp, &P, |
| 176 | valid_fmt, |
| 177 | NULL, |
| 178 | buf, |
| 179 | sizeof( buf ) ) ); |
| 180 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 181 | mbedtls_ecp_tls_write_point( &grp, &P, |
| 182 | valid_fmt, |
| 183 | &olen, |
| 184 | NULL, |
| 185 | sizeof( buf ) ) ); |
| 186 | |
| 187 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 188 | mbedtls_ecp_group_load( NULL, valid_group ) ); |
| 189 | |
| 190 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 191 | mbedtls_ecp_tls_read_group( NULL, |
| 192 | (const unsigned char **) &buf, |
| 193 | sizeof( buf ) ) ); |
| 194 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 195 | mbedtls_ecp_tls_read_group( &grp, NULL, |
| 196 | sizeof( buf ) ) ); |
| 197 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 198 | mbedtls_ecp_tls_read_group( &grp, &null_buf, |
| 199 | sizeof( buf ) ) ); |
| 200 | |
| 201 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 202 | mbedtls_ecp_tls_read_group_id( NULL, |
| 203 | (const unsigned char **) &buf, |
| 204 | sizeof( buf ) ) ); |
| 205 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 206 | mbedtls_ecp_tls_read_group_id( &valid_group, NULL, |
| 207 | sizeof( buf ) ) ); |
| 208 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 209 | mbedtls_ecp_tls_read_group_id( &valid_group, |
| 210 | &null_buf, |
| 211 | sizeof( buf ) ) ); |
| 212 | |
| 213 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 214 | mbedtls_ecp_tls_write_group( NULL, &olen, |
| 215 | buf, sizeof( buf ) ) ); |
| 216 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 217 | mbedtls_ecp_tls_write_group( &grp, NULL, |
| 218 | buf, sizeof( buf ) ) ); |
| 219 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 220 | mbedtls_ecp_tls_write_group( &grp, &olen, |
| 221 | NULL, sizeof( buf ) ) ); |
| 222 | |
| 223 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 224 | mbedtls_ecp_mul( NULL, &P, &m, &P, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 225 | mbedtls_test_rnd_std_rand, |
| 226 | NULL ) ); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 227 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 228 | mbedtls_ecp_mul( &grp, NULL, &m, &P, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 229 | mbedtls_test_rnd_std_rand, |
| 230 | NULL ) ); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 231 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 232 | mbedtls_ecp_mul( &grp, &P, NULL, &P, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 233 | mbedtls_test_rnd_std_rand, |
| 234 | NULL ) ); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 235 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 236 | mbedtls_ecp_mul( &grp, &P, &m, NULL, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 237 | mbedtls_test_rnd_std_rand, |
| 238 | NULL ) ); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 239 | |
| 240 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 241 | mbedtls_ecp_mul_restartable( NULL, &P, &m, &P, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 242 | mbedtls_test_rnd_std_rand, |
| 243 | NULL , NULL ) ); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 244 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 245 | mbedtls_ecp_mul_restartable( &grp, NULL, &m, &P, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 246 | mbedtls_test_rnd_std_rand, |
| 247 | NULL , NULL ) ); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 248 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 249 | mbedtls_ecp_mul_restartable( &grp, &P, NULL, &P, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 250 | mbedtls_test_rnd_std_rand, |
| 251 | NULL , NULL ) ); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 252 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 253 | mbedtls_ecp_mul_restartable( &grp, &P, &m, NULL, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 254 | mbedtls_test_rnd_std_rand, |
| 255 | NULL , NULL ) ); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 256 | |
| 257 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 258 | mbedtls_ecp_muladd( NULL, &P, &m, &P, |
| 259 | &m, &P ) ); |
| 260 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 261 | mbedtls_ecp_muladd( &grp, NULL, &m, &P, |
| 262 | &m, &P ) ); |
| 263 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 264 | mbedtls_ecp_muladd( &grp, &P, NULL, &P, |
| 265 | &m, &P ) ); |
| 266 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 267 | mbedtls_ecp_muladd( &grp, &P, &m, NULL, |
| 268 | &m, &P ) ); |
| 269 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 270 | mbedtls_ecp_muladd( &grp, &P, &m, &P, |
| 271 | NULL, &P ) ); |
| 272 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 273 | mbedtls_ecp_muladd( &grp, &P, &m, &P, |
| 274 | &m, NULL ) ); |
| 275 | |
| 276 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 277 | mbedtls_ecp_muladd_restartable( NULL, &P, &m, &P, |
| 278 | &m, &P, NULL ) ); |
| 279 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 280 | mbedtls_ecp_muladd_restartable( &grp, NULL, &m, &P, |
| 281 | &m, &P, NULL ) ); |
| 282 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 283 | mbedtls_ecp_muladd_restartable( &grp, &P, NULL, &P, |
| 284 | &m, &P, NULL ) ); |
| 285 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 286 | mbedtls_ecp_muladd_restartable( &grp, &P, &m, NULL, |
| 287 | &m, &P, NULL ) ); |
| 288 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 289 | mbedtls_ecp_muladd_restartable( &grp, &P, &m, &P, |
| 290 | NULL, &P, NULL ) ); |
| 291 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 292 | mbedtls_ecp_muladd_restartable( &grp, &P, &m, &P, |
| 293 | &m, NULL, NULL ) ); |
| 294 | |
| 295 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 296 | mbedtls_ecp_check_pubkey( NULL, &P ) ); |
| 297 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 298 | mbedtls_ecp_check_pubkey( &grp, NULL ) ); |
| 299 | |
| 300 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Hanno Becker | 1959535 | 2018-12-18 23:54:04 +0000 | [diff] [blame] | 301 | mbedtls_ecp_check_pub_priv( NULL, &kp ) ); |
| 302 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 303 | mbedtls_ecp_check_pub_priv( &kp, NULL ) ); |
| 304 | |
| 305 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 306 | mbedtls_ecp_check_privkey( NULL, &m ) ); |
| 307 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 308 | mbedtls_ecp_check_privkey( &grp, NULL ) ); |
| 309 | |
| 310 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 311 | mbedtls_ecp_gen_keypair_base( NULL, &P, &m, &P, |
| 312 | mbedtls_test_rnd_std_rand, NULL ) ); |
| 313 | |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 314 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 315 | mbedtls_ecp_gen_keypair_base( &grp, NULL, &m, &P, |
| 316 | mbedtls_test_rnd_std_rand, NULL ) ); |
| 317 | |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 318 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 319 | mbedtls_ecp_gen_keypair_base( &grp, &P, NULL, &P, |
| 320 | mbedtls_test_rnd_std_rand, NULL ) ); |
| 321 | |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 322 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 323 | mbedtls_ecp_gen_keypair_base( &grp, &P, &m, NULL, |
| 324 | mbedtls_test_rnd_std_rand, NULL ) ); |
| 325 | |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 326 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 327 | mbedtls_ecp_gen_keypair_base( &grp, &P, &m, &P, NULL, NULL ) ); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 328 | |
| 329 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 330 | mbedtls_ecp_gen_keypair( NULL, |
| 331 | &m, &P, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 332 | mbedtls_test_rnd_std_rand, |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 333 | NULL ) ); |
| 334 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 335 | mbedtls_ecp_gen_keypair( &grp, |
| 336 | NULL, &P, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 337 | mbedtls_test_rnd_std_rand, |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 338 | NULL ) ); |
| 339 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 340 | mbedtls_ecp_gen_keypair( &grp, |
| 341 | &m, NULL, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 342 | mbedtls_test_rnd_std_rand, |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 343 | NULL ) ); |
| 344 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 345 | mbedtls_ecp_gen_keypair( &grp, |
| 346 | &m, &P, |
| 347 | NULL, |
| 348 | NULL ) ); |
| 349 | |
| 350 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 351 | mbedtls_ecp_gen_key( valid_group, NULL, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 352 | mbedtls_test_rnd_std_rand, |
| 353 | NULL ) ); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 354 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 355 | mbedtls_ecp_gen_key( valid_group, &kp, |
| 356 | NULL, NULL ) ); |
| 357 | |
| 358 | exit: |
| 359 | return; |
| 360 | } |
| 361 | /* END_CASE */ |
| 362 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 363 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 364 | void mbedtls_ecp_curve_info( int id, int tls_id, int size, char * name ) |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 365 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 366 | const mbedtls_ecp_curve_info *by_id, *by_tls, *by_name; |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 368 | by_id = mbedtls_ecp_curve_info_from_grp_id( id ); |
| 369 | by_tls = mbedtls_ecp_curve_info_from_tls_id( tls_id ); |
| 370 | by_name = mbedtls_ecp_curve_info_from_name( name ); |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 371 | TEST_ASSERT( by_id != NULL ); |
| 372 | TEST_ASSERT( by_tls != NULL ); |
| 373 | TEST_ASSERT( by_name != NULL ); |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 374 | |
| 375 | TEST_ASSERT( by_id == by_tls ); |
| 376 | TEST_ASSERT( by_id == by_name ); |
| 377 | |
Manuel Pégourié-Gonnard | 797f48a | 2015-06-18 15:45:05 +0200 | [diff] [blame] | 378 | TEST_ASSERT( by_id->bit_size == size ); |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 379 | } |
| 380 | /* END_CASE */ |
| 381 | |
| 382 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 383 | void ecp_check_pub( int grp_id, char * x_hex, char * y_hex, char * z_hex, |
| 384 | int ret ) |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 385 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 386 | mbedtls_ecp_group grp; |
| 387 | mbedtls_ecp_point P; |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 389 | mbedtls_ecp_group_init( &grp ); |
| 390 | mbedtls_ecp_point_init( &P ); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 391 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 392 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, grp_id ) == 0 ); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 393 | |
Janos Follath | 28fff14 | 2017-01-27 15:51:14 +0000 | [diff] [blame] | 394 | TEST_ASSERT( mbedtls_mpi_read_string( &P.X, 16, x_hex ) == 0 ); |
| 395 | TEST_ASSERT( mbedtls_mpi_read_string( &P.Y, 16, y_hex ) == 0 ); |
| 396 | TEST_ASSERT( mbedtls_mpi_read_string( &P.Z, 16, z_hex ) == 0 ); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 397 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 398 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &P ) == ret ); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 399 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 400 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 401 | mbedtls_ecp_group_free( &grp ); |
| 402 | mbedtls_ecp_point_free( &P ); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 403 | } |
| 404 | /* END_CASE */ |
| 405 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 406 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 407 | void ecp_test_vect_restart( int id, |
| 408 | char *dA_str, char *xA_str, char *yA_str, |
| 409 | char *dB_str, char *xZ_str, char *yZ_str, |
| 410 | int max_ops, int min_restarts, int max_restarts ) |
| 411 | { |
| 412 | /* |
| 413 | * Test for early restart. Based on test vectors like ecp_test_vect(), |
| 414 | * but for the sake of simplicity only does half of each side. It's |
| 415 | * important to test both base point and random point, though, as memory |
| 416 | * management is different in each case. |
| 417 | * |
| 418 | * Don't try using too precise bounds for restarts as the exact number |
| 419 | * will depend on settings such as MBEDTLS_ECP_FIXED_POINT_OPTIM and |
| 420 | * MBEDTLS_ECP_WINDOW_SIZE, as well as implementation details that may |
| 421 | * change in the future. A factor 2 is a minimum safety margin. |
| 422 | * |
| 423 | * For reference, with mbed TLS 2.4 and default settings, for P-256: |
Manuel Pégourié-Gonnard | 9c5c78f | 2017-03-20 14:13:07 +0100 | [diff] [blame] | 424 | * - Random point mult: ~3250M |
| 425 | * - Cold base point mult: ~3300M |
| 426 | * - Hot base point mult: ~1100M |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 427 | * With MBEDTLS_ECP_WINDOW_SIZE set to 2 (minimum): |
Manuel Pégourié-Gonnard | 9c5c78f | 2017-03-20 14:13:07 +0100 | [diff] [blame] | 428 | * - Random point mult: ~3850M |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 429 | */ |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 430 | mbedtls_ecp_restart_ctx ctx; |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 431 | mbedtls_ecp_group grp; |
Manuel Pégourié-Gonnard | 7a28e99 | 2018-10-16 11:22:45 +0200 | [diff] [blame] | 432 | mbedtls_ecp_point R, P; |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 433 | mbedtls_mpi dA, xA, yA, dB, xZ, yZ; |
| 434 | int cnt_restarts; |
| 435 | int ret; |
| 436 | |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 437 | mbedtls_ecp_restart_init( &ctx ); |
Manuel Pégourié-Gonnard | 7a28e99 | 2018-10-16 11:22:45 +0200 | [diff] [blame] | 438 | mbedtls_ecp_group_init( &grp ); |
| 439 | mbedtls_ecp_point_init( &R ); mbedtls_ecp_point_init( &P ); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 440 | mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA ); mbedtls_mpi_init( &yA ); |
| 441 | mbedtls_mpi_init( &dB ); mbedtls_mpi_init( &xZ ); mbedtls_mpi_init( &yZ ); |
| 442 | |
| 443 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
| 444 | |
| 445 | TEST_ASSERT( mbedtls_mpi_read_string( &dA, 16, dA_str ) == 0 ); |
| 446 | TEST_ASSERT( mbedtls_mpi_read_string( &xA, 16, xA_str ) == 0 ); |
| 447 | TEST_ASSERT( mbedtls_mpi_read_string( &yA, 16, yA_str ) == 0 ); |
| 448 | |
| 449 | TEST_ASSERT( mbedtls_mpi_read_string( &dB, 16, dB_str ) == 0 ); |
| 450 | TEST_ASSERT( mbedtls_mpi_read_string( &xZ, 16, xZ_str ) == 0 ); |
| 451 | TEST_ASSERT( mbedtls_mpi_read_string( &yZ, 16, yZ_str ) == 0 ); |
| 452 | |
| 453 | mbedtls_ecp_set_max_ops( (unsigned) max_ops ); |
| 454 | |
| 455 | /* Base point case */ |
| 456 | cnt_restarts = 0; |
| 457 | do { |
Manuel Pégourié-Gonnard | 7a28e99 | 2018-10-16 11:22:45 +0200 | [diff] [blame] | 458 | ECP_PT_RESET( &R ); |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 459 | ret = mbedtls_ecp_mul_restartable( &grp, &R, &dA, &grp.G, NULL, NULL, &ctx ); |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 460 | } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts ); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 461 | |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 462 | TEST_ASSERT( ret == 0 ); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 463 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 ); |
| 464 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yA ) == 0 ); |
| 465 | |
| 466 | TEST_ASSERT( cnt_restarts >= min_restarts ); |
| 467 | TEST_ASSERT( cnt_restarts <= max_restarts ); |
| 468 | |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 469 | /* Non-base point case */ |
Manuel Pégourié-Gonnard | 7a28e99 | 2018-10-16 11:22:45 +0200 | [diff] [blame] | 470 | mbedtls_ecp_copy( &P, &R ); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 471 | cnt_restarts = 0; |
| 472 | do { |
Manuel Pégourié-Gonnard | 7a28e99 | 2018-10-16 11:22:45 +0200 | [diff] [blame] | 473 | ECP_PT_RESET( &R ); |
| 474 | ret = mbedtls_ecp_mul_restartable( &grp, &R, &dB, &P, NULL, NULL, &ctx ); |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 475 | } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts ); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 476 | |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 477 | TEST_ASSERT( ret == 0 ); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 478 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 ); |
| 479 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 ); |
| 480 | |
| 481 | TEST_ASSERT( cnt_restarts >= min_restarts ); |
| 482 | TEST_ASSERT( cnt_restarts <= max_restarts ); |
| 483 | |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 484 | /* Do we leak memory when aborting an operation? |
| 485 | * This test only makes sense when we actually restart */ |
| 486 | if( min_restarts > 0 ) |
| 487 | { |
Manuel Pégourié-Gonnard | 7a28e99 | 2018-10-16 11:22:45 +0200 | [diff] [blame] | 488 | ret = mbedtls_ecp_mul_restartable( &grp, &R, &dB, &P, NULL, NULL, &ctx ); |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 489 | TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); |
| 490 | } |
Manuel Pégourié-Gonnard | 77af79a | 2017-03-14 10:58:00 +0100 | [diff] [blame] | 491 | |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 492 | exit: |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 493 | mbedtls_ecp_restart_free( &ctx ); |
Manuel Pégourié-Gonnard | 7a28e99 | 2018-10-16 11:22:45 +0200 | [diff] [blame] | 494 | mbedtls_ecp_group_free( &grp ); |
| 495 | mbedtls_ecp_point_free( &R ); mbedtls_ecp_point_free( &P ); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 496 | mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA ); mbedtls_mpi_free( &yA ); |
| 497 | mbedtls_mpi_free( &dB ); mbedtls_mpi_free( &xZ ); mbedtls_mpi_free( &yZ ); |
| 498 | } |
| 499 | /* END_CASE */ |
| 500 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 501 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 502 | void ecp_muladd_restart( int id, char *xR_str, char *yR_str, |
| 503 | char *u1_str, char *u2_str, |
| 504 | char *xQ_str, char *yQ_str, |
| 505 | int max_ops, int min_restarts, int max_restarts ) |
| 506 | { |
| 507 | /* |
| 508 | * Compute R = u1 * G + u2 * Q |
| 509 | * (test vectors mostly taken from ECDSA intermediate results) |
| 510 | * |
| 511 | * See comments at the top of ecp_test_vect_restart() |
| 512 | */ |
| 513 | mbedtls_ecp_restart_ctx ctx; |
| 514 | mbedtls_ecp_group grp; |
| 515 | mbedtls_ecp_point R, Q; |
| 516 | mbedtls_mpi u1, u2, xR, yR; |
| 517 | int cnt_restarts; |
| 518 | int ret; |
| 519 | |
| 520 | mbedtls_ecp_restart_init( &ctx ); |
| 521 | mbedtls_ecp_group_init( &grp ); |
| 522 | mbedtls_ecp_point_init( &R ); |
| 523 | mbedtls_ecp_point_init( &Q ); |
| 524 | mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 ); |
| 525 | mbedtls_mpi_init( &xR ); mbedtls_mpi_init( &yR ); |
| 526 | |
| 527 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
| 528 | |
| 529 | TEST_ASSERT( mbedtls_mpi_read_string( &u1, 16, u1_str ) == 0 ); |
| 530 | TEST_ASSERT( mbedtls_mpi_read_string( &u2, 16, u2_str ) == 0 ); |
| 531 | TEST_ASSERT( mbedtls_mpi_read_string( &xR, 16, xR_str ) == 0 ); |
| 532 | TEST_ASSERT( mbedtls_mpi_read_string( &yR, 16, yR_str ) == 0 ); |
| 533 | |
| 534 | TEST_ASSERT( mbedtls_mpi_read_string( &Q.X, 16, xQ_str ) == 0 ); |
| 535 | TEST_ASSERT( mbedtls_mpi_read_string( &Q.Y, 16, yQ_str ) == 0 ); |
| 536 | TEST_ASSERT( mbedtls_mpi_lset( &Q.Z, 1 ) == 0 ); |
| 537 | |
| 538 | mbedtls_ecp_set_max_ops( (unsigned) max_ops ); |
| 539 | |
| 540 | cnt_restarts = 0; |
| 541 | do { |
Manuel Pégourié-Gonnard | 7a28e99 | 2018-10-16 11:22:45 +0200 | [diff] [blame] | 542 | ECP_PT_RESET( &R ); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 543 | ret = mbedtls_ecp_muladd_restartable( &grp, &R, |
| 544 | &u1, &grp.G, &u2, &Q, &ctx ); |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 545 | } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts ); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 546 | |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 547 | TEST_ASSERT( ret == 0 ); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 548 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xR ) == 0 ); |
| 549 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yR ) == 0 ); |
| 550 | |
| 551 | TEST_ASSERT( cnt_restarts >= min_restarts ); |
| 552 | TEST_ASSERT( cnt_restarts <= max_restarts ); |
| 553 | |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 554 | /* Do we leak memory when aborting an operation? |
| 555 | * This test only makes sense when we actually restart */ |
| 556 | if( min_restarts > 0 ) |
| 557 | { |
| 558 | ret = mbedtls_ecp_muladd_restartable( &grp, &R, |
| 559 | &u1, &grp.G, &u2, &Q, &ctx ); |
| 560 | TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); |
| 561 | } |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 562 | |
| 563 | exit: |
| 564 | mbedtls_ecp_restart_free( &ctx ); |
| 565 | mbedtls_ecp_group_free( &grp ); |
| 566 | mbedtls_ecp_point_free( &R ); |
| 567 | mbedtls_ecp_point_free( &Q ); |
| 568 | mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 ); |
| 569 | mbedtls_mpi_free( &xR ); mbedtls_mpi_free( &yR ); |
| 570 | } |
| 571 | /* END_CASE */ |
| 572 | |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 573 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 574 | void ecp_test_vect( int id, char * dA_str, char * xA_str, char * yA_str, |
| 575 | char * dB_str, char * xB_str, char * yB_str, |
| 576 | char * xZ_str, char * yZ_str ) |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 577 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 578 | mbedtls_ecp_group grp; |
| 579 | mbedtls_ecp_point R; |
| 580 | mbedtls_mpi dA, xA, yA, dB, xB, yB, xZ, yZ; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 581 | mbedtls_test_rnd_pseudo_info rnd_info; |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 582 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 583 | mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R ); |
| 584 | mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA ); mbedtls_mpi_init( &yA ); mbedtls_mpi_init( &dB ); |
| 585 | mbedtls_mpi_init( &xB ); mbedtls_mpi_init( &yB ); mbedtls_mpi_init( &xZ ); mbedtls_mpi_init( &yZ ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 586 | memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 587 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 588 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 589 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 590 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 ); |
Manuel Pégourié-Gonnard | 1c33057 | 2012-11-24 12:05:44 +0100 | [diff] [blame] | 591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 592 | TEST_ASSERT( mbedtls_mpi_read_string( &dA, 16, dA_str ) == 0 ); |
| 593 | TEST_ASSERT( mbedtls_mpi_read_string( &xA, 16, xA_str ) == 0 ); |
| 594 | TEST_ASSERT( mbedtls_mpi_read_string( &yA, 16, yA_str ) == 0 ); |
| 595 | TEST_ASSERT( mbedtls_mpi_read_string( &dB, 16, dB_str ) == 0 ); |
| 596 | TEST_ASSERT( mbedtls_mpi_read_string( &xB, 16, xB_str ) == 0 ); |
| 597 | TEST_ASSERT( mbedtls_mpi_read_string( &yB, 16, yB_str ) == 0 ); |
| 598 | TEST_ASSERT( mbedtls_mpi_read_string( &xZ, 16, xZ_str ) == 0 ); |
| 599 | TEST_ASSERT( mbedtls_mpi_read_string( &yZ, 16, yZ_str ) == 0 ); |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 600 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 601 | TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &grp.G, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 602 | &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 603 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 ); |
| 604 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yA ) == 0 ); |
| 605 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 ); |
| 606 | TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &R, NULL, NULL ) == 0 ); |
| 607 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 ); |
| 608 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 ); |
| 609 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 ); |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 610 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 611 | TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 ); |
| 612 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xB ) == 0 ); |
| 613 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yB ) == 0 ); |
| 614 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 ); |
| 615 | TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &R, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 616 | &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 617 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 ); |
| 618 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 ); |
| 619 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 ); |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 620 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 621 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 622 | mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R ); |
| 623 | mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA ); mbedtls_mpi_free( &yA ); mbedtls_mpi_free( &dB ); |
| 624 | mbedtls_mpi_free( &xB ); mbedtls_mpi_free( &yB ); mbedtls_mpi_free( &xZ ); mbedtls_mpi_free( &yZ ); |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 625 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 626 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 627 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 628 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 629 | void ecp_test_vec_x( int id, char * dA_hex, char * xA_hex, char * dB_hex, |
| 630 | char * xB_hex, char * xS_hex ) |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 631 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 632 | mbedtls_ecp_group grp; |
| 633 | mbedtls_ecp_point R; |
| 634 | mbedtls_mpi dA, xA, dB, xB, xS; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 635 | mbedtls_test_rnd_pseudo_info rnd_info; |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 637 | mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R ); |
| 638 | mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA ); |
| 639 | mbedtls_mpi_init( &dB ); mbedtls_mpi_init( &xB ); |
| 640 | mbedtls_mpi_init( &xS ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 641 | memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 642 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 643 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 645 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 ); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 646 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 647 | TEST_ASSERT( mbedtls_mpi_read_string( &dA, 16, dA_hex ) == 0 ); |
| 648 | TEST_ASSERT( mbedtls_mpi_read_string( &dB, 16, dB_hex ) == 0 ); |
| 649 | TEST_ASSERT( mbedtls_mpi_read_string( &xA, 16, xA_hex ) == 0 ); |
| 650 | TEST_ASSERT( mbedtls_mpi_read_string( &xB, 16, xB_hex ) == 0 ); |
| 651 | TEST_ASSERT( mbedtls_mpi_read_string( &xS, 16, xS_hex ) == 0 ); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 652 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 653 | TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &grp.G, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 654 | &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 655 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 ); |
| 656 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 ); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 657 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 658 | TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &R, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 659 | &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 660 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 ); |
| 661 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xS ) == 0 ); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 662 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 663 | TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 ); |
| 664 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 ); |
| 665 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xB ) == 0 ); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 666 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 667 | TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &R, NULL, NULL ) == 0 ); |
| 668 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 ); |
| 669 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xS ) == 0 ); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 670 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 671 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 672 | mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R ); |
| 673 | mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA ); |
| 674 | mbedtls_mpi_free( &dB ); mbedtls_mpi_free( &xB ); |
| 675 | mbedtls_mpi_free( &xS ); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 676 | } |
| 677 | /* END_CASE */ |
| 678 | |
| 679 | /* BEGIN_CASE */ |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 680 | void ecp_test_mul( int id, data_t * n_hex, |
| 681 | data_t * Px_hex, data_t * Py_hex, data_t * Pz_hex, |
| 682 | data_t * nPx_hex, data_t * nPy_hex, data_t * nPz_hex, |
| 683 | int expected_ret ) |
| 684 | { |
| 685 | mbedtls_ecp_group grp; |
| 686 | mbedtls_ecp_point P, nP, R; |
| 687 | mbedtls_mpi n; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 688 | mbedtls_test_rnd_pseudo_info rnd_info; |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 689 | |
| 690 | mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R ); |
| 691 | mbedtls_ecp_point_init( &P ); mbedtls_ecp_point_init( &nP ); |
| 692 | mbedtls_mpi_init( &n ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 693 | memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 694 | |
| 695 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
| 696 | |
| 697 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 ); |
| 698 | |
| 699 | TEST_ASSERT( mbedtls_mpi_read_binary( &n, n_hex->x, n_hex->len ) == 0 ); |
| 700 | |
| 701 | TEST_ASSERT( mbedtls_mpi_read_binary( &P.X, Px_hex->x, Px_hex->len ) == 0 ); |
| 702 | TEST_ASSERT( mbedtls_mpi_read_binary( &P.Y, Py_hex->x, Py_hex->len ) == 0 ); |
| 703 | TEST_ASSERT( mbedtls_mpi_read_binary( &P.Z, Pz_hex->x, Pz_hex->len ) == 0 ); |
| 704 | TEST_ASSERT( mbedtls_mpi_read_binary( &nP.X, nPx_hex->x, nPx_hex->len ) |
| 705 | == 0 ); |
| 706 | TEST_ASSERT( mbedtls_mpi_read_binary( &nP.Y, nPy_hex->x, nPy_hex->len ) |
| 707 | == 0 ); |
| 708 | TEST_ASSERT( mbedtls_mpi_read_binary( &nP.Z, nPz_hex->x, nPz_hex->len ) |
| 709 | == 0 ); |
| 710 | |
| 711 | TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &n, &P, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 712 | &mbedtls_test_rnd_pseudo_rand, &rnd_info ) |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 713 | == expected_ret ); |
| 714 | |
| 715 | if( expected_ret == 0 ) |
| 716 | { |
| 717 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &nP.X, &R.X ) == 0 ); |
| 718 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &nP.Y, &R.Y ) == 0 ); |
| 719 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &nP.Z, &R.Z ) == 0 ); |
| 720 | } |
| 721 | |
| 722 | exit: |
| 723 | mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R ); |
| 724 | mbedtls_ecp_point_free( &P ); mbedtls_ecp_point_free( &nP ); |
| 725 | mbedtls_mpi_free( &n ); |
| 726 | } |
| 727 | /* END_CASE */ |
| 728 | |
| 729 | /* BEGIN_CASE */ |
Jonas | 923d579 | 2020-05-13 14:22:45 +0900 | [diff] [blame] | 730 | void ecp_test_mul_rng( int id, data_t * d_hex) |
| 731 | { |
| 732 | mbedtls_ecp_group grp; |
| 733 | mbedtls_mpi d; |
| 734 | mbedtls_ecp_point Q; |
| 735 | |
| 736 | mbedtls_ecp_group_init( &grp ); mbedtls_mpi_init( &d ); |
| 737 | mbedtls_ecp_point_init( &Q ); |
| 738 | |
| 739 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
| 740 | |
| 741 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 ); |
| 742 | |
| 743 | TEST_ASSERT( mbedtls_mpi_read_binary( &d, d_hex->x, d_hex->len ) == 0 ); |
| 744 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 745 | TEST_ASSERT( mbedtls_ecp_mul( &grp, &Q, &d, &grp.G, |
| 746 | &mbedtls_test_rnd_zero_rand, NULL ) |
Jonas | 923d579 | 2020-05-13 14:22:45 +0900 | [diff] [blame] | 747 | == MBEDTLS_ERR_ECP_RANDOM_FAILED ); |
| 748 | |
| 749 | exit: |
| 750 | mbedtls_ecp_group_free( &grp ); mbedtls_mpi_free( &d ); |
| 751 | mbedtls_ecp_point_free( &Q ); |
| 752 | } |
| 753 | /* END_CASE */ |
| 754 | |
| 755 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 756 | void ecp_fast_mod( int id, char * N_str ) |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 757 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 758 | mbedtls_ecp_group grp; |
| 759 | mbedtls_mpi N, R; |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 761 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &R ); |
| 762 | mbedtls_ecp_group_init( &grp ); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 763 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 764 | TEST_ASSERT( mbedtls_mpi_read_string( &N, 16, N_str ) == 0 ); |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 765 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
Manuel Pégourié-Gonnard | e783f06 | 2013-10-21 14:52:21 +0200 | [diff] [blame] | 766 | TEST_ASSERT( grp.modp != NULL ); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 767 | |
| 768 | /* |
| 769 | * Store correct result before we touch N |
| 770 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 771 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &N, &grp.P ) == 0 ); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 772 | |
| 773 | TEST_ASSERT( grp.modp( &N ) == 0 ); |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 774 | TEST_ASSERT( mbedtls_mpi_bitlen( &N ) <= grp.pbits + 3 ); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 775 | |
| 776 | /* |
Paul Bakker | d8b0c5e | 2014-04-11 15:31:33 +0200 | [diff] [blame] | 777 | * Use mod rather than addition/subtraction in case previous test fails |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 778 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 779 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &N, &N, &grp.P ) == 0 ); |
| 780 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &R ) == 0 ); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 781 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 782 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 783 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &R ); |
| 784 | mbedtls_ecp_group_free( &grp ); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 785 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 786 | /* END_CASE */ |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 787 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 788 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 789 | void ecp_write_binary( int id, char * x, char * y, char * z, int format, |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 790 | data_t * out, int blen, int ret ) |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 791 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 792 | mbedtls_ecp_group grp; |
| 793 | mbedtls_ecp_point P; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 794 | unsigned char buf[256]; |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 795 | size_t olen; |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 796 | |
| 797 | memset( buf, 0, sizeof( buf ) ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 798 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 799 | mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 800 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 801 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +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( &P.X, 16, x ) == 0 ); |
| 804 | TEST_ASSERT( mbedtls_mpi_read_string( &P.Y, 16, y ) == 0 ); |
| 805 | TEST_ASSERT( mbedtls_mpi_read_string( &P.Z, 16, z ) == 0 ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 807 | TEST_ASSERT( mbedtls_ecp_point_write_binary( &grp, &P, format, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 808 | &olen, buf, blen ) == ret ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 809 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 810 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 811 | { |
Ronald Cron | de70b16 | 2020-06-10 11:03:08 +0200 | [diff] [blame] | 812 | TEST_ASSERT( mbedtls_test_hexcmp( buf, out->x, olen, out->len ) == 0 ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 813 | } |
| 814 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 815 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 817 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 818 | /* END_CASE */ |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 819 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 820 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 821 | void ecp_read_binary( int id, data_t * buf, char * x, char * y, char * z, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 822 | int ret ) |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 823 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 824 | mbedtls_ecp_group grp; |
| 825 | mbedtls_ecp_point P; |
| 826 | mbedtls_mpi X, Y, Z; |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 827 | |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 828 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 829 | mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P ); |
| 830 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 831 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 832 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 833 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 834 | TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x ) == 0 ); |
| 835 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y ) == 0 ); |
| 836 | TEST_ASSERT( mbedtls_mpi_read_string( &Z, 16, z ) == 0 ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 837 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 838 | TEST_ASSERT( mbedtls_ecp_point_read_binary( &grp, &P, buf->x, buf->len ) == ret ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 839 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 840 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 841 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 842 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.X, &X ) == 0 ); |
Janos Follath | df9295b | 2019-02-26 12:36:52 +0000 | [diff] [blame] | 843 | if( mbedtls_ecp_get_type( &grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY ) |
Janos Follath | 59b813c | 2019-02-13 10:44:06 +0000 | [diff] [blame] | 844 | { |
| 845 | TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, 0 ) == 0 ); |
| 846 | TEST_ASSERT( P.Y.p == NULL ); |
| 847 | TEST_ASSERT( mbedtls_mpi_cmp_int( &Z, 1 ) == 0 ); |
| 848 | TEST_ASSERT( mbedtls_mpi_cmp_int( &P.Z, 1 ) == 0 ); |
| 849 | } |
| 850 | else |
| 851 | { |
| 852 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Y, &Y ) == 0 ); |
| 853 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Z, &Z ) == 0 ); |
| 854 | } |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 855 | } |
| 856 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 857 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 858 | mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P ); |
| 859 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 860 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 861 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 862 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 863 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 864 | void mbedtls_ecp_tls_read_point( int id, data_t * buf, char * x, char * y, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 865 | char * z, int ret ) |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 866 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 867 | mbedtls_ecp_group grp; |
| 868 | mbedtls_ecp_point P; |
| 869 | mbedtls_mpi X, Y, Z; |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 870 | const unsigned char *vbuf = buf->x; |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 871 | |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 872 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 873 | mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P ); |
| 874 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 875 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 876 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 877 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 878 | TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x ) == 0 ); |
| 879 | TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y ) == 0 ); |
| 880 | TEST_ASSERT( mbedtls_mpi_read_string( &Z, 16, z ) == 0 ); |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 881 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 882 | TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &P, &vbuf, buf->len ) == ret ); |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 883 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 884 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 885 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 886 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.X, &X ) == 0 ); |
| 887 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Y, &Y ) == 0 ); |
| 888 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Z, &Z ) == 0 ); |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 889 | TEST_ASSERT( (uint32_t)( vbuf - buf->x ) == buf->len ); |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 890 | } |
| 891 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 892 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 893 | mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P ); |
| 894 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 895 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 896 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 897 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 898 | /* BEGIN_CASE */ |
| 899 | void ecp_tls_write_read_point( int id ) |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 900 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 901 | mbedtls_ecp_group grp; |
| 902 | mbedtls_ecp_point pt; |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 903 | unsigned char buf[256]; |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 904 | const unsigned char *vbuf; |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 905 | size_t olen; |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 906 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 907 | mbedtls_ecp_group_init( &grp ); |
| 908 | mbedtls_ecp_point_init( &pt ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 909 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 910 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 911 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 912 | memset( buf, 0x00, sizeof( buf ) ); vbuf = buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 913 | TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &grp.G, |
| 914 | MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 ); |
| 915 | TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) |
| 916 | == MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 917 | TEST_ASSERT( vbuf == buf + olen ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 918 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 919 | memset( buf, 0x00, sizeof( buf ) ); vbuf = buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 920 | TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &grp.G, |
| 921 | MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 ); |
| 922 | TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 ); |
| 923 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 ); |
| 924 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 ); |
| 925 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 926 | TEST_ASSERT( vbuf == buf + olen ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 927 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 928 | memset( buf, 0x00, sizeof( buf ) ); vbuf = buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 929 | TEST_ASSERT( mbedtls_ecp_set_zero( &pt ) == 0 ); |
| 930 | TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &pt, |
| 931 | MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 ); |
| 932 | TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 ); |
| 933 | TEST_ASSERT( mbedtls_ecp_is_zero( &pt ) ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 934 | TEST_ASSERT( vbuf == buf + olen ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 935 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 936 | memset( buf, 0x00, sizeof( buf ) ); vbuf = buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 937 | TEST_ASSERT( mbedtls_ecp_set_zero( &pt ) == 0 ); |
| 938 | TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &pt, |
| 939 | MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 ); |
| 940 | TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 ); |
| 941 | TEST_ASSERT( mbedtls_ecp_is_zero( &pt ) ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 942 | TEST_ASSERT( vbuf == buf + olen ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 943 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 944 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 945 | mbedtls_ecp_group_free( &grp ); |
| 946 | mbedtls_ecp_point_free( &pt ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 947 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 948 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 949 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 950 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 951 | void mbedtls_ecp_tls_read_group( data_t * buf, int result, int bits, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 952 | int record_len ) |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 953 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 954 | mbedtls_ecp_group grp; |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 955 | const unsigned char *vbuf = buf->x; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 956 | int ret; |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 957 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 958 | mbedtls_ecp_group_init( &grp ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 959 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 960 | ret = mbedtls_ecp_tls_read_group( &grp, &vbuf, buf->len ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 961 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 962 | TEST_ASSERT( ret == result ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 963 | if( ret == 0) |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 964 | { |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 965 | TEST_ASSERT( mbedtls_mpi_bitlen( &grp.P ) == (size_t) bits ); |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 966 | TEST_ASSERT( vbuf - buf->x == record_len); |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 967 | } |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 968 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 969 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 970 | mbedtls_ecp_group_free( &grp ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 971 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 972 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 973 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 974 | /* BEGIN_CASE */ |
| 975 | void ecp_tls_write_read_group( int id ) |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 976 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 977 | mbedtls_ecp_group grp1, grp2; |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 978 | unsigned char buf[10]; |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 979 | const unsigned char *vbuf = buf; |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 980 | size_t len; |
| 981 | int ret; |
| 982 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 983 | mbedtls_ecp_group_init( &grp1 ); |
| 984 | mbedtls_ecp_group_init( &grp2 ); |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 985 | memset( buf, 0x00, sizeof( buf ) ); |
| 986 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 987 | TEST_ASSERT( mbedtls_ecp_group_load( &grp1, id ) == 0 ); |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 988 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 989 | TEST_ASSERT( mbedtls_ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 ); |
| 990 | ret = mbedtls_ecp_tls_read_group( &grp2, &vbuf, len ); |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 991 | TEST_ASSERT( ret == 0 ); |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 992 | |
| 993 | if( ret == 0 ) |
| 994 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 995 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 ); |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 996 | TEST_ASSERT( grp1.id == grp2.id ); |
| 997 | } |
| 998 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 999 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1000 | mbedtls_ecp_group_free( &grp1 ); |
| 1001 | mbedtls_ecp_group_free( &grp2 ); |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 1002 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1003 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 1004 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1005 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1006 | void mbedtls_ecp_check_privkey( int id, char * key_hex, int ret ) |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1007 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1008 | mbedtls_ecp_group grp; |
| 1009 | mbedtls_mpi d; |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1010 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1011 | mbedtls_ecp_group_init( &grp ); |
| 1012 | mbedtls_mpi_init( &d ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1013 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 1014 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1015 | TEST_ASSERT( mbedtls_mpi_read_string( &d, 16, key_hex ) == 0 ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1016 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1017 | TEST_ASSERT( mbedtls_ecp_check_privkey( &grp, &d ) == ret ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1018 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1019 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1020 | mbedtls_ecp_group_free( &grp ); |
| 1021 | mbedtls_mpi_free( &d ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1022 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1023 | /* END_CASE */ |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1024 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1025 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1026 | void mbedtls_ecp_check_pub_priv( int id_pub, char * Qx_pub, char * Qy_pub, |
| 1027 | int id, char * d, char * Qx, char * Qy, |
| 1028 | int ret ) |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1029 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1030 | mbedtls_ecp_keypair pub, prv; |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1031 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1032 | mbedtls_ecp_keypair_init( &pub ); |
| 1033 | mbedtls_ecp_keypair_init( &prv ); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1034 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1035 | if( id_pub != MBEDTLS_ECP_DP_NONE ) |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 1036 | TEST_ASSERT( mbedtls_ecp_group_load( &pub.grp, id_pub ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1037 | TEST_ASSERT( mbedtls_ecp_point_read_string( &pub.Q, 16, Qx_pub, Qy_pub ) == 0 ); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1038 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1039 | if( id != MBEDTLS_ECP_DP_NONE ) |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 1040 | TEST_ASSERT( mbedtls_ecp_group_load( &prv.grp, id ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1041 | TEST_ASSERT( mbedtls_ecp_point_read_string( &prv.Q, 16, Qx, Qy ) == 0 ); |
| 1042 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.d, 16, d ) == 0 ); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1043 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1044 | TEST_ASSERT( mbedtls_ecp_check_pub_priv( &pub, &prv ) == ret ); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1045 | |
| 1046 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1047 | mbedtls_ecp_keypair_free( &pub ); |
| 1048 | mbedtls_ecp_keypair_free( &prv ); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1049 | } |
| 1050 | /* END_CASE */ |
| 1051 | |
| 1052 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1053 | void mbedtls_ecp_gen_keypair( int id ) |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1054 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1055 | mbedtls_ecp_group grp; |
| 1056 | mbedtls_ecp_point Q; |
| 1057 | mbedtls_mpi d; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1058 | mbedtls_test_rnd_pseudo_info rnd_info; |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1059 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1060 | mbedtls_ecp_group_init( &grp ); |
| 1061 | mbedtls_ecp_point_init( &Q ); |
| 1062 | mbedtls_mpi_init( &d ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1063 | memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1064 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 1065 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1066 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1067 | TEST_ASSERT( mbedtls_ecp_gen_keypair( &grp, &d, &Q, |
| 1068 | &mbedtls_test_rnd_pseudo_rand, |
| 1069 | &rnd_info ) == 0 ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1070 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1071 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &Q ) == 0 ); |
| 1072 | TEST_ASSERT( mbedtls_ecp_check_privkey( &grp, &d ) == 0 ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1073 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1074 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1075 | mbedtls_ecp_group_free( &grp ); |
| 1076 | mbedtls_ecp_point_free( &Q ); |
| 1077 | mbedtls_mpi_free( &d ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1078 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1079 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1080 | |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1081 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1082 | void mbedtls_ecp_gen_key( int id ) |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1083 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1084 | mbedtls_ecp_keypair key; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1085 | mbedtls_test_rnd_pseudo_info rnd_info; |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1086 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1087 | mbedtls_ecp_keypair_init( &key ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1088 | memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1089 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1090 | TEST_ASSERT( mbedtls_ecp_gen_key( id, &key, |
| 1091 | &mbedtls_test_rnd_pseudo_rand, |
| 1092 | &rnd_info ) == 0 ); |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1093 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1094 | TEST_ASSERT( mbedtls_ecp_check_pubkey( &key.grp, &key.Q ) == 0 ); |
| 1095 | TEST_ASSERT( mbedtls_ecp_check_privkey( &key.grp, &key.d ) == 0 ); |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1096 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1097 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1098 | mbedtls_ecp_keypair_free( &key ); |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1099 | } |
| 1100 | /* END_CASE */ |
| 1101 | |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1102 | /* BEGIN_CASE */ |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1103 | void mbedtls_ecp_read_key( int grp_id, data_t* in_key, int expected, int canonical ) |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1104 | { |
| 1105 | int ret = 0; |
| 1106 | mbedtls_ecp_keypair key; |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1107 | mbedtls_ecp_keypair key2; |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1108 | |
| 1109 | mbedtls_ecp_keypair_init( &key ); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1110 | mbedtls_ecp_keypair_init( &key2 ); |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1111 | |
| 1112 | ret = mbedtls_ecp_read_key( grp_id, &key, in_key->x, in_key->len ); |
| 1113 | TEST_ASSERT( ret == expected ); |
| 1114 | |
| 1115 | if( expected == 0 ) |
| 1116 | { |
| 1117 | ret = mbedtls_ecp_check_privkey( &key.grp, &key.d ); |
| 1118 | TEST_ASSERT( ret == 0 ); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1119 | |
| 1120 | if( canonical ) |
| 1121 | { |
| 1122 | unsigned char buf[MBEDTLS_ECP_MAX_BYTES]; |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1123 | |
Steven Cooreman | 0024df6 | 2020-07-13 10:59:40 +0200 | [diff] [blame] | 1124 | ret = mbedtls_ecp_write_key( &key, buf, in_key->len ); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1125 | TEST_ASSERT( ret == 0 ); |
| 1126 | |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1127 | ASSERT_COMPARE( in_key->x, in_key->len, |
Steven Cooreman | c9b7f78 | 2020-06-11 17:00:36 +0200 | [diff] [blame] | 1128 | buf, in_key->len ); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1129 | } |
| 1130 | else |
| 1131 | { |
| 1132 | unsigned char export1[MBEDTLS_ECP_MAX_BYTES]; |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1133 | unsigned char export2[MBEDTLS_ECP_MAX_BYTES]; |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1134 | |
Steven Cooreman | 0024df6 | 2020-07-13 10:59:40 +0200 | [diff] [blame] | 1135 | ret = mbedtls_ecp_write_key( &key, export1, in_key->len ); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1136 | TEST_ASSERT( ret == 0 ); |
| 1137 | |
| 1138 | ret = mbedtls_ecp_read_key( grp_id, &key2, export1, in_key->len ); |
| 1139 | TEST_ASSERT( ret == expected ); |
| 1140 | |
Steven Cooreman | 0024df6 | 2020-07-13 10:59:40 +0200 | [diff] [blame] | 1141 | ret = mbedtls_ecp_write_key( &key2, export2, in_key->len ); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1142 | TEST_ASSERT( ret == 0 ); |
| 1143 | |
Steven Cooreman | c9b7f78 | 2020-06-11 17:00:36 +0200 | [diff] [blame] | 1144 | ASSERT_COMPARE( export1, in_key->len, |
| 1145 | export2, in_key->len ); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1146 | } |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | exit: |
| 1150 | mbedtls_ecp_keypair_free( &key ); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1151 | mbedtls_ecp_keypair_free( &key2 ); |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1152 | } |
| 1153 | /* END_CASE */ |
| 1154 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1155 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1156 | void ecp_selftest( ) |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1157 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 1158 | TEST_ASSERT( mbedtls_ecp_self_test( 1 ) == 0 ); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1159 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1160 | /* END_CASE */ |