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