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/ecdsa.h" |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 3 | /* END_HEADER */ |
Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 4 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 5 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6 | * depends_on:MBEDTLS_ECDSA_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 7 | * END_DEPENDENCIES |
| 8 | */ |
Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 9 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 10 | /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ |
| 11 | void ecdsa_invalid_param( ) |
| 12 | { |
| 13 | mbedtls_ecdsa_context ctx; |
| 14 | mbedtls_ecp_keypair key; |
| 15 | mbedtls_ecp_group grp; |
| 16 | mbedtls_ecp_group_id valid_group = MBEDTLS_ECP_DP_SECP192R1; |
| 17 | mbedtls_ecp_point P; |
| 18 | mbedtls_md_type_t valid_md = MBEDTLS_MD_SHA256; |
| 19 | mbedtls_mpi m; |
| 20 | size_t slen; |
| 21 | unsigned char buf[42] = { 0 }; |
| 22 | |
| 23 | TEST_INVALID_PARAM( mbedtls_ecdsa_init( NULL ) ); |
| 24 | TEST_VALID_PARAM( mbedtls_ecdsa_free( NULL ) ); |
| 25 | |
| 26 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 27 | TEST_INVALID_PARAM( mbedtls_ecdsa_restart_init( NULL ) ); |
| 28 | TEST_VALID_PARAM( mbedtls_ecdsa_restart_free( NULL ) ); |
| 29 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 30 | |
| 31 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 32 | mbedtls_ecdsa_sign( NULL, &m, &m, &m, |
| 33 | buf, sizeof( buf ), |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 34 | mbedtls_test_rnd_std_rand, |
| 35 | NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 36 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 37 | mbedtls_ecdsa_sign( &grp, NULL, &m, &m, |
| 38 | buf, sizeof( buf ), |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 39 | mbedtls_test_rnd_std_rand, |
| 40 | NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 41 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 42 | mbedtls_ecdsa_sign( &grp, &m, NULL, &m, |
| 43 | buf, sizeof( buf ), |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 44 | mbedtls_test_rnd_std_rand, |
| 45 | NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 46 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 47 | mbedtls_ecdsa_sign( &grp, &m, &m, NULL, |
| 48 | buf, sizeof( buf ), |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 49 | mbedtls_test_rnd_std_rand, |
| 50 | NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 51 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 52 | mbedtls_ecdsa_sign( &grp, &m, &m, &m, |
| 53 | NULL, sizeof( buf ), |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 54 | mbedtls_test_rnd_std_rand, |
| 55 | NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 56 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 57 | mbedtls_ecdsa_sign( &grp, &m, &m, &m, |
| 58 | buf, sizeof( buf ), |
| 59 | NULL, NULL ) ); |
| 60 | |
| 61 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 62 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Janos Follath | 651eac8 | 2019-01-04 15:51:24 +0000 | [diff] [blame] | 63 | mbedtls_ecdsa_sign_det_ext( NULL, &m, &m, &m, |
| 64 | buf, sizeof( buf ), |
| 65 | valid_md, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 66 | mbedtls_test_rnd_std_rand, |
| 67 | NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 68 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Janos Follath | 651eac8 | 2019-01-04 15:51:24 +0000 | [diff] [blame] | 69 | mbedtls_ecdsa_sign_det_ext( &grp, NULL, &m, &m, |
| 70 | buf, sizeof( buf ), |
| 71 | valid_md, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 72 | mbedtls_test_rnd_std_rand, |
| 73 | NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 74 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Janos Follath | 651eac8 | 2019-01-04 15:51:24 +0000 | [diff] [blame] | 75 | mbedtls_ecdsa_sign_det_ext( &grp, &m, NULL, &m, |
| 76 | buf, sizeof( buf ), |
| 77 | valid_md, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 78 | mbedtls_test_rnd_std_rand, |
| 79 | NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 80 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Janos Follath | 651eac8 | 2019-01-04 15:51:24 +0000 | [diff] [blame] | 81 | mbedtls_ecdsa_sign_det_ext( &grp, &m, &m, NULL, |
| 82 | buf, sizeof( buf ), |
| 83 | valid_md, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 84 | mbedtls_test_rnd_std_rand, |
| 85 | NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 86 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Janos Follath | 651eac8 | 2019-01-04 15:51:24 +0000 | [diff] [blame] | 87 | mbedtls_ecdsa_sign_det_ext( &grp, &m, &m, &m, |
| 88 | NULL, sizeof( buf ), |
| 89 | valid_md, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 90 | mbedtls_test_rnd_std_rand, |
| 91 | NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 92 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
| 93 | |
| 94 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 95 | mbedtls_ecdsa_verify( NULL, |
| 96 | buf, sizeof( buf ), |
| 97 | &P, &m, &m ) ); |
| 98 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 99 | mbedtls_ecdsa_verify( &grp, |
| 100 | NULL, sizeof( buf ), |
| 101 | &P, &m, &m ) ); |
| 102 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 103 | mbedtls_ecdsa_verify( &grp, |
| 104 | buf, sizeof( buf ), |
| 105 | NULL, &m, &m ) ); |
| 106 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 107 | mbedtls_ecdsa_verify( &grp, |
| 108 | buf, sizeof( buf ), |
| 109 | &P, NULL, &m ) ); |
| 110 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 111 | mbedtls_ecdsa_verify( &grp, |
| 112 | buf, sizeof( buf ), |
| 113 | &P, &m, NULL ) ); |
| 114 | |
| 115 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 116 | mbedtls_ecdsa_write_signature( NULL, valid_md, buf, sizeof( buf ), |
| 117 | buf, &slen, mbedtls_test_rnd_std_rand, |
| 118 | NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 119 | |
| 120 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 121 | mbedtls_ecdsa_write_signature( &ctx, valid_md, NULL, sizeof( buf ), |
| 122 | buf, &slen, mbedtls_test_rnd_std_rand, |
| 123 | NULL ) ); |
| 124 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 125 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 126 | mbedtls_ecdsa_write_signature( &ctx, valid_md, buf, sizeof( buf ), |
| 127 | NULL, &slen, mbedtls_test_rnd_std_rand, |
| 128 | NULL ) ); |
| 129 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 130 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 131 | mbedtls_ecdsa_write_signature( &ctx, valid_md, buf, sizeof( buf ), |
| 132 | buf, NULL, mbedtls_test_rnd_std_rand, |
| 133 | NULL ) ); |
| 134 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 135 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 136 | mbedtls_ecdsa_write_signature_restartable( NULL, valid_md, buf, |
| 137 | sizeof( buf ), buf, &slen, |
| 138 | mbedtls_test_rnd_std_rand, |
| 139 | NULL, NULL ) ); |
| 140 | |
| 141 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 142 | mbedtls_ecdsa_write_signature_restartable( &ctx, valid_md, NULL, |
| 143 | sizeof( buf ), buf, &slen, |
| 144 | mbedtls_test_rnd_std_rand, |
| 145 | NULL, NULL ) ); |
| 146 | |
| 147 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 148 | mbedtls_ecdsa_write_signature_restartable( &ctx, valid_md, buf, |
| 149 | sizeof( buf ), NULL, &slen, |
| 150 | mbedtls_test_rnd_std_rand, |
| 151 | NULL, NULL ) ); |
| 152 | |
| 153 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 154 | mbedtls_ecdsa_write_signature_restartable( &ctx, valid_md, buf, |
| 155 | sizeof( buf ), buf, NULL, |
| 156 | mbedtls_test_rnd_std_rand, |
| 157 | NULL, NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 158 | |
| 159 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 160 | mbedtls_ecdsa_read_signature( NULL, |
| 161 | buf, sizeof( buf ), |
| 162 | buf, sizeof( buf ) ) ); |
| 163 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 164 | mbedtls_ecdsa_read_signature( &ctx, |
| 165 | NULL, sizeof( buf ), |
| 166 | buf, sizeof( buf ) ) ); |
| 167 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 168 | mbedtls_ecdsa_read_signature( &ctx, |
| 169 | buf, sizeof( buf ), |
| 170 | NULL, sizeof( buf ) ) ); |
| 171 | |
| 172 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 173 | mbedtls_ecdsa_read_signature_restartable( NULL, |
| 174 | buf, sizeof( buf ), |
| 175 | buf, sizeof( buf ), |
| 176 | NULL ) ); |
| 177 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 178 | mbedtls_ecdsa_read_signature_restartable( &ctx, |
| 179 | NULL, sizeof( buf ), |
| 180 | buf, sizeof( buf ), |
| 181 | NULL ) ); |
| 182 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 183 | mbedtls_ecdsa_read_signature_restartable( &ctx, |
| 184 | buf, sizeof( buf ), |
| 185 | NULL, sizeof( buf ), |
| 186 | NULL ) ); |
| 187 | |
| 188 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 189 | mbedtls_ecdsa_genkey( NULL, valid_group, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 190 | mbedtls_test_rnd_std_rand, |
| 191 | NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 192 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 193 | mbedtls_ecdsa_genkey( &ctx, valid_group, |
| 194 | NULL, NULL ) ); |
| 195 | |
| 196 | |
| 197 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 198 | mbedtls_ecdsa_from_keypair( NULL, &key ) ); |
| 199 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 200 | mbedtls_ecdsa_from_keypair( &ctx, NULL ) ); |
| 201 | |
| 202 | exit: |
| 203 | return; |
| 204 | } |
| 205 | /* END_CASE */ |
| 206 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 207 | /* BEGIN_CASE */ |
| 208 | void ecdsa_prim_random( int id ) |
Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 209 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | mbedtls_ecp_group grp; |
| 211 | mbedtls_ecp_point Q; |
| 212 | mbedtls_mpi d, r, s; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 213 | mbedtls_test_rnd_pseudo_info rnd_info; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 214 | unsigned char buf[MBEDTLS_MD_MAX_SIZE]; |
Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | mbedtls_ecp_group_init( &grp ); |
| 217 | mbedtls_ecp_point_init( &Q ); |
| 218 | mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 219 | memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Manuel Pégourié-Gonnard | 450a163 | 2013-01-27 09:08:18 +0100 | [diff] [blame] | 220 | memset( buf, 0, sizeof( buf ) ); |
Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 221 | |
| 222 | /* prepare material for signature */ |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 223 | TEST_ASSERT( mbedtls_test_rnd_pseudo_rand( &rnd_info, |
| 224 | buf, sizeof( buf ) ) == 0 ); |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 225 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 226 | TEST_ASSERT( mbedtls_ecp_gen_keypair( &grp, &d, &Q, |
| 227 | &mbedtls_test_rnd_pseudo_rand, |
| 228 | &rnd_info ) == 0 ); |
Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 229 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | TEST_ASSERT( mbedtls_ecdsa_sign( &grp, &r, &s, &d, buf, sizeof( buf ), |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 231 | &mbedtls_test_rnd_pseudo_rand, |
| 232 | &rnd_info ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | TEST_ASSERT( mbedtls_ecdsa_verify( &grp, buf, sizeof( buf ), &Q, &r, &s ) == 0 ); |
Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 234 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 235 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | mbedtls_ecp_group_free( &grp ); |
| 237 | mbedtls_ecp_point_free( &Q ); |
| 238 | mbedtls_mpi_free( &d ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s ); |
Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 239 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 240 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 241 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 242 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 243 | void ecdsa_prim_test_vectors( int id, char * d_str, char * xQ_str, |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 244 | char * yQ_str, data_t * rnd_buf, |
| 245 | data_t * hash, char * r_str, char * s_str, |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 246 | int result ) |
Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 247 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | mbedtls_ecp_group grp; |
| 249 | mbedtls_ecp_point Q; |
| 250 | mbedtls_mpi d, r, s, r_check, s_check; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 251 | mbedtls_test_rnd_buf_info rnd_info; |
Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 252 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | mbedtls_ecp_group_init( &grp ); |
| 254 | mbedtls_ecp_point_init( &Q ); |
| 255 | mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s ); |
| 256 | mbedtls_mpi_init( &r_check ); mbedtls_mpi_init( &s_check ); |
Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 257 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 258 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | TEST_ASSERT( mbedtls_ecp_point_read_string( &Q, 16, xQ_str, yQ_str ) == 0 ); |
| 260 | TEST_ASSERT( mbedtls_mpi_read_string( &d, 16, d_str ) == 0 ); |
| 261 | TEST_ASSERT( mbedtls_mpi_read_string( &r_check, 16, r_str ) == 0 ); |
| 262 | TEST_ASSERT( mbedtls_mpi_read_string( &s_check, 16, s_str ) == 0 ); |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 263 | rnd_info.buf = rnd_buf->x; |
| 264 | rnd_info.length = rnd_buf->len; |
Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 265 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 266 | /* Fix rnd_buf->x by shifting it left if necessary */ |
Manuel Pégourié-Gonnard | fae079e | 2014-01-06 11:00:07 +0100 | [diff] [blame] | 267 | if( grp.nbits % 8 != 0 ) |
| 268 | { |
| 269 | unsigned char shift = 8 - ( grp.nbits % 8 ); |
| 270 | size_t i; |
| 271 | |
| 272 | for( i = 0; i < rnd_info.length - 1; i++ ) |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 273 | rnd_buf->x[i] = rnd_buf->x[i] << shift | rnd_buf->x[i+1] >> ( 8 - shift ); |
Manuel Pégourié-Gonnard | fae079e | 2014-01-06 11:00:07 +0100 | [diff] [blame] | 274 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 275 | rnd_buf->x[rnd_info.length-1] <<= shift; |
Manuel Pégourié-Gonnard | fae079e | 2014-01-06 11:00:07 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 278 | TEST_ASSERT( mbedtls_ecdsa_sign( &grp, &r, &s, &d, hash->x, hash->len, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 279 | mbedtls_test_rnd_buffer_rand, &rnd_info ) == result ); |
Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 280 | |
Darryl Green | f5bcbed | 2017-11-17 17:09:31 +0000 | [diff] [blame] | 281 | if ( result == 0) |
| 282 | { |
| 283 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &r, &r_check ) == 0 ); |
| 284 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &s, &s_check ) == 0 ); |
Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 285 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 286 | TEST_ASSERT( mbedtls_ecdsa_verify( &grp, hash->x, hash->len, &Q, &r_check, &s_check ) == 0 ); |
Manuel Pégourié-Gonnard | d0a66cc | 2018-06-13 09:53:21 +0200 | [diff] [blame] | 287 | |
| 288 | TEST_ASSERT( mbedtls_mpi_sub_int( &r, &r, 1 ) == 0 ); |
| 289 | TEST_ASSERT( mbedtls_mpi_add_int( &s, &s, 1 ) == 0 ); |
| 290 | |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 291 | TEST_ASSERT( mbedtls_ecdsa_verify( &grp, hash->x, hash->len, |
Manuel Pégourié-Gonnard | d0a66cc | 2018-06-13 09:53:21 +0200 | [diff] [blame] | 292 | &Q, &r, &s_check ) == MBEDTLS_ERR_ECP_VERIFY_FAILED ); |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 293 | TEST_ASSERT( mbedtls_ecdsa_verify( &grp, hash->x, hash->len, |
Manuel Pégourié-Gonnard | d0a66cc | 2018-06-13 09:53:21 +0200 | [diff] [blame] | 294 | &Q, &r_check, &s ) == MBEDTLS_ERR_ECP_VERIFY_FAILED ); |
Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 295 | TEST_ASSERT( mbedtls_ecdsa_verify( &grp, hash->x, hash->len, |
Manuel Pégourié-Gonnard | d0a66cc | 2018-06-13 09:53:21 +0200 | [diff] [blame] | 296 | &grp.G, &r_check, &s_check ) == MBEDTLS_ERR_ECP_VERIFY_FAILED ); |
Darryl Green | f5bcbed | 2017-11-17 17:09:31 +0000 | [diff] [blame] | 297 | } |
Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 298 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 299 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 300 | mbedtls_ecp_group_free( &grp ); |
| 301 | mbedtls_ecp_point_free( &Q ); |
| 302 | mbedtls_mpi_free( &d ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s ); |
| 303 | mbedtls_mpi_free( &r_check ); mbedtls_mpi_free( &s_check ); |
Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 304 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 305 | /* END_CASE */ |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 306 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 307 | /* BEGIN_CASE depends_on:MBEDTLS_ECDSA_DETERMINISTIC */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 308 | void ecdsa_det_test_vectors( int id, char * d_str, int md_alg, char * msg, |
| 309 | char * r_str, char * s_str ) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 310 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 311 | mbedtls_ecp_group grp; |
| 312 | mbedtls_mpi d, r, s, r_check, s_check; |
| 313 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 314 | size_t hlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | const mbedtls_md_info_t *md_info; |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | mbedtls_ecp_group_init( &grp ); |
| 318 | mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s ); |
| 319 | mbedtls_mpi_init( &r_check ); mbedtls_mpi_init( &s_check ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 320 | memset( hash, 0, sizeof( hash ) ); |
| 321 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 322 | TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 323 | TEST_ASSERT( mbedtls_mpi_read_string( &d, 16, d_str ) == 0 ); |
| 324 | TEST_ASSERT( mbedtls_mpi_read_string( &r_check, 16, r_str ) == 0 ); |
| 325 | TEST_ASSERT( mbedtls_mpi_read_string( &s_check, 16, s_str ) == 0 ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 326 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 327 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 328 | TEST_ASSERT( md_info != NULL ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 329 | hlen = mbedtls_md_get_size( md_info ); |
Hanno Becker | 198611d | 2018-10-17 13:58:19 +0100 | [diff] [blame] | 330 | TEST_ASSERT( mbedtls_md( md_info, (const unsigned char *) msg, |
| 331 | strlen( msg ), hash ) == 0 ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 332 | |
Janos Follath | 651eac8 | 2019-01-04 15:51:24 +0000 | [diff] [blame] | 333 | TEST_ASSERT( |
| 334 | mbedtls_ecdsa_sign_det_ext( &grp, &r, &s, &d, hash, hlen, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 335 | md_alg, mbedtls_test_rnd_std_rand, |
| 336 | NULL ) |
Janos Follath | 651eac8 | 2019-01-04 15:51:24 +0000 | [diff] [blame] | 337 | == 0 ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 338 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 339 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &r, &r_check ) == 0 ); |
| 340 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &s, &s_check ) == 0 ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 341 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 342 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 343 | mbedtls_ecp_group_free( &grp ); |
| 344 | mbedtls_mpi_free( &d ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s ); |
| 345 | mbedtls_mpi_free( &r_check ); mbedtls_mpi_free( &s_check ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 346 | } |
| 347 | /* END_CASE */ |
| 348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 349 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 350 | void ecdsa_write_read_random( int id ) |
| 351 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | mbedtls_ecdsa_context ctx; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 353 | mbedtls_test_rnd_pseudo_info rnd_info; |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 354 | unsigned char hash[32]; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 355 | unsigned char sig[200]; |
| 356 | size_t sig_len, i; |
| 357 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 358 | mbedtls_ecdsa_init( &ctx ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 359 | memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 360 | memset( hash, 0, sizeof( hash ) ); |
| 361 | memset( sig, 0x2a, sizeof( sig ) ); |
| 362 | |
| 363 | /* prepare material for signature */ |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 364 | TEST_ASSERT( mbedtls_test_rnd_pseudo_rand( &rnd_info, |
| 365 | hash, sizeof( hash ) ) == 0 ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 366 | |
| 367 | /* generate signing key */ |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 368 | TEST_ASSERT( mbedtls_ecdsa_genkey( &ctx, id, |
| 369 | &mbedtls_test_rnd_pseudo_rand, |
| 370 | &rnd_info ) == 0 ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 371 | |
| 372 | /* generate and write signature, then read and verify it */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 373 | TEST_ASSERT( mbedtls_ecdsa_write_signature( &ctx, MBEDTLS_MD_SHA256, |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 374 | hash, sizeof( hash ), |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 375 | sig, &sig_len, &mbedtls_test_rnd_pseudo_rand, |
| 376 | &rnd_info ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 377 | TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ), |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 378 | sig, sig_len ) == 0 ); |
| 379 | |
| 380 | /* check we didn't write past the announced length */ |
| 381 | for( i = sig_len; i < sizeof( sig ); i++ ) |
| 382 | TEST_ASSERT( sig[i] == 0x2a ); |
| 383 | |
| 384 | /* try verification with invalid length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 385 | TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ), |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 386 | sig, sig_len - 1 ) != 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 387 | TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ), |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 388 | sig, sig_len + 1 ) != 0 ); |
| 389 | |
| 390 | /* try invalid sequence tag */ |
| 391 | sig[0]++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 392 | TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ), |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 393 | sig, sig_len ) != 0 ); |
| 394 | sig[0]--; |
| 395 | |
| 396 | /* try modifying r */ |
| 397 | sig[10]++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 398 | TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ), |
Manuel Pégourié-Gonnard | 1ed2505 | 2017-04-21 10:04:02 +0200 | [diff] [blame] | 399 | sig, sig_len ) == MBEDTLS_ERR_ECP_VERIFY_FAILED ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 400 | sig[10]--; |
| 401 | |
| 402 | /* try modifying s */ |
| 403 | sig[sig_len - 1]++; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ), |
Manuel Pégourié-Gonnard | 1ed2505 | 2017-04-21 10:04:02 +0200 | [diff] [blame] | 405 | sig, sig_len ) == MBEDTLS_ERR_ECP_VERIFY_FAILED ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 406 | sig[sig_len - 1]--; |
| 407 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 408 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 409 | mbedtls_ecdsa_free( &ctx ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 410 | } |
| 411 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 412 | |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 413 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 414 | void ecdsa_read_restart( int id, data_t *pk, data_t *hash, data_t *sig, |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 415 | int max_ops, int min_restart, int max_restart ) |
| 416 | { |
| 417 | mbedtls_ecdsa_context ctx; |
| 418 | mbedtls_ecdsa_restart_ctx rs_ctx; |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 419 | int ret, cnt_restart; |
| 420 | |
| 421 | mbedtls_ecdsa_init( &ctx ); |
| 422 | mbedtls_ecdsa_restart_init( &rs_ctx ); |
| 423 | |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 424 | TEST_ASSERT( mbedtls_ecp_group_load( &ctx.grp, id ) == 0 ); |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 425 | TEST_ASSERT( mbedtls_ecp_point_read_binary( &ctx.grp, &ctx.Q, |
| 426 | pk->x, pk->len ) == 0 ); |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 427 | |
| 428 | mbedtls_ecp_set_max_ops( max_ops ); |
| 429 | |
| 430 | cnt_restart = 0; |
| 431 | do { |
| 432 | ret = mbedtls_ecdsa_read_signature_restartable( &ctx, |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 433 | hash->x, hash->len, sig->x, sig->len, &rs_ctx ); |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 434 | } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart ); |
| 435 | |
| 436 | TEST_ASSERT( ret == 0 ); |
| 437 | TEST_ASSERT( cnt_restart >= min_restart ); |
| 438 | TEST_ASSERT( cnt_restart <= max_restart ); |
| 439 | |
| 440 | /* try modifying r */ |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 441 | |
| 442 | TEST_ASSERT( sig->len > 10 ); |
| 443 | sig->x[10]++; |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 444 | do { |
| 445 | ret = mbedtls_ecdsa_read_signature_restartable( &ctx, |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 446 | hash->x, hash->len, sig->x, sig->len, &rs_ctx ); |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 447 | } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); |
| 448 | TEST_ASSERT( ret == MBEDTLS_ERR_ECP_VERIFY_FAILED ); |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 449 | sig->x[10]--; |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 450 | |
| 451 | /* try modifying s */ |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 452 | sig->x[sig->len - 1]++; |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 453 | do { |
| 454 | ret = mbedtls_ecdsa_read_signature_restartable( &ctx, |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 455 | hash->x, hash->len, sig->x, sig->len, &rs_ctx ); |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 456 | } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); |
| 457 | TEST_ASSERT( ret == MBEDTLS_ERR_ECP_VERIFY_FAILED ); |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 458 | sig->x[sig->len - 1]--; |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 459 | |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 460 | /* Do we leak memory when aborting an operation? |
| 461 | * This test only makes sense when we actually restart */ |
| 462 | if( min_restart > 0 ) |
| 463 | { |
| 464 | ret = mbedtls_ecdsa_read_signature_restartable( &ctx, |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 465 | hash->x, hash->len, sig->x, sig->len, &rs_ctx ); |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 466 | TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); |
| 467 | } |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 468 | |
| 469 | exit: |
| 470 | mbedtls_ecdsa_free( &ctx ); |
| 471 | mbedtls_ecdsa_restart_free( &rs_ctx ); |
| 472 | } |
| 473 | /* END_CASE */ |
Manuel Pégourié-Gonnard | eb402f3 | 2017-04-25 10:57:30 +0200 | [diff] [blame] | 474 | |
| 475 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_DETERMINISTIC */ |
| 476 | void ecdsa_write_restart( int id, char *d_str, int md_alg, |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 477 | char *msg, data_t *sig_check, |
Manuel Pégourié-Gonnard | eb402f3 | 2017-04-25 10:57:30 +0200 | [diff] [blame] | 478 | int max_ops, int min_restart, int max_restart ) |
| 479 | { |
| 480 | int ret, cnt_restart; |
| 481 | mbedtls_ecdsa_restart_ctx rs_ctx; |
| 482 | mbedtls_ecdsa_context ctx; |
| 483 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
| 484 | unsigned char sig[MBEDTLS_ECDSA_MAX_LEN]; |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 485 | size_t hlen, slen; |
Manuel Pégourié-Gonnard | eb402f3 | 2017-04-25 10:57:30 +0200 | [diff] [blame] | 486 | const mbedtls_md_info_t *md_info; |
| 487 | |
| 488 | mbedtls_ecdsa_restart_init( &rs_ctx ); |
| 489 | mbedtls_ecdsa_init( &ctx ); |
| 490 | memset( hash, 0, sizeof( hash ) ); |
| 491 | memset( sig, 0, sizeof( sig ) ); |
Manuel Pégourié-Gonnard | eb402f3 | 2017-04-25 10:57:30 +0200 | [diff] [blame] | 492 | |
| 493 | TEST_ASSERT( mbedtls_ecp_group_load( &ctx.grp, id ) == 0 ); |
| 494 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.d, 16, d_str ) == 0 ); |
Manuel Pégourié-Gonnard | eb402f3 | 2017-04-25 10:57:30 +0200 | [diff] [blame] | 495 | |
| 496 | md_info = mbedtls_md_info_from_type( md_alg ); |
| 497 | TEST_ASSERT( md_info != NULL ); |
| 498 | |
| 499 | hlen = mbedtls_md_get_size( md_info ); |
Gilles Peskine | 84984ae | 2020-01-21 16:52:08 +0100 | [diff] [blame] | 500 | TEST_ASSERT( mbedtls_md( md_info, |
| 501 | (const unsigned char *) msg, strlen( msg ), |
| 502 | hash ) == 0 ); |
Manuel Pégourié-Gonnard | eb402f3 | 2017-04-25 10:57:30 +0200 | [diff] [blame] | 503 | |
| 504 | mbedtls_ecp_set_max_ops( max_ops ); |
| 505 | |
| 506 | slen = sizeof( sig ); |
| 507 | cnt_restart = 0; |
| 508 | do { |
| 509 | ret = mbedtls_ecdsa_write_signature_restartable( &ctx, |
| 510 | md_alg, hash, hlen, sig, &slen, NULL, NULL, &rs_ctx ); |
| 511 | } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart ); |
| 512 | |
| 513 | TEST_ASSERT( ret == 0 ); |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame^] | 514 | TEST_ASSERT( slen == sig_check->len ); |
| 515 | TEST_ASSERT( memcmp( sig, sig_check->x, slen ) == 0 ); |
Manuel Pégourié-Gonnard | eb402f3 | 2017-04-25 10:57:30 +0200 | [diff] [blame] | 516 | |
| 517 | TEST_ASSERT( cnt_restart >= min_restart ); |
| 518 | TEST_ASSERT( cnt_restart <= max_restart ); |
| 519 | |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 520 | /* Do we leak memory when aborting an operation? |
| 521 | * This test only makes sense when we actually restart */ |
| 522 | if( min_restart > 0 ) |
| 523 | { |
| 524 | ret = mbedtls_ecdsa_write_signature_restartable( &ctx, |
| 525 | md_alg, hash, hlen, sig, &slen, NULL, NULL, &rs_ctx ); |
| 526 | TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); |
| 527 | } |
Manuel Pégourié-Gonnard | eb402f3 | 2017-04-25 10:57:30 +0200 | [diff] [blame] | 528 | |
| 529 | exit: |
| 530 | mbedtls_ecdsa_restart_free( &rs_ctx ); |
| 531 | mbedtls_ecdsa_free( &ctx ); |
| 532 | } |
| 533 | /* END_CASE */ |