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" |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 3 | #include "mbedtls/ecdsa.h" |
| 4 | #include "mbedtls/ecdh.h" |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 5 | |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 6 | #include "ecp_invasive.h" |
| 7 | |
| 8 | #if defined(MBEDTLS_TEST_HOOKS) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 9 | (defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \ |
| 10 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ |
| 11 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)) |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 12 | #define HAVE_FIX_NEGATIVE |
| 13 | #endif |
| 14 | |
Manuel Pégourié-Gonnard | 6c7af4c | 2015-04-03 16:41:52 +0200 | [diff] [blame] | 15 | #define ECP_PF_UNKNOWN -1 |
Manuel Pégourié-Gonnard | 7a28e99 | 2018-10-16 11:22:45 +0200 | [diff] [blame] | 16 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 17 | #define ECP_PT_RESET(x) \ |
| 18 | mbedtls_ecp_point_free(x); \ |
| 19 | mbedtls_ecp_point_init(x); |
Gilles Peskine | 6373fab | 2021-03-29 21:32:16 +0200 | [diff] [blame] | 20 | |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 21 | /* Auxiliary function to compare two mbedtls_ecp_group objects. */ |
Dave Rodgman | 0fd07d5 | 2024-01-04 11:37:35 +0000 | [diff] [blame] | 22 | MBEDTLS_MAYBE_UNUSED |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 23 | inline static int mbedtls_ecp_group_cmp(mbedtls_ecp_group *grp1, |
| 24 | mbedtls_ecp_group *grp2) |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 25 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 26 | if (mbedtls_mpi_cmp_mpi(&grp1->P, &grp2->P) != 0) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 27 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 28 | } |
| 29 | if (mbedtls_mpi_cmp_mpi(&grp1->A, &grp2->A) != 0) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 30 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 31 | } |
| 32 | if (mbedtls_mpi_cmp_mpi(&grp1->B, &grp2->B) != 0) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 33 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 34 | } |
| 35 | if (mbedtls_mpi_cmp_mpi(&grp1->N, &grp2->N) != 0) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 36 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 37 | } |
| 38 | if (mbedtls_ecp_point_cmp(&grp1->G, &grp2->G) != 0) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 39 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 40 | } |
| 41 | if (grp1->id != grp2->id) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 42 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 43 | } |
| 44 | if (grp1->pbits != grp2->pbits) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 45 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 46 | } |
| 47 | if (grp1->nbits != grp2->nbits) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 48 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 49 | } |
| 50 | if (grp1->h != grp2->h) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 51 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | } |
| 53 | if (grp1->modp != grp2->modp) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 54 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 55 | } |
| 56 | if (grp1->t_pre != grp2->t_pre) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 57 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 58 | } |
| 59 | if (grp1->t_post != grp2->t_post) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 60 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 61 | } |
| 62 | if (grp1->t_data != grp2->t_data) { |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 63 | return 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 64 | } |
Gowtham Suresh Kumar | 21f2b7a | 2023-07-10 22:50:29 +0100 | [diff] [blame] | 65 | /* Here we should not compare T and T_size as the value of T is |
| 66 | * always NULL for Montgomery curves and for Weierstrass curves |
| 67 | * it will be NULL until ecp_mul is called. After calling ecp_mul, |
| 68 | * the value will be unique (dynamically allocated). |
| 69 | */ |
Werner Lewis | 938dc19 | 2022-08-15 12:56:12 +0100 | [diff] [blame] | 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 74 | /* END_HEADER */ |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 75 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 76 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 77 | * depends_on:MBEDTLS_ECP_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 78 | * END_DEPENDENCIES |
| 79 | */ |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 80 | |
Hanno Becker | 57b684f | 2018-12-18 12:50:02 +0000 | [diff] [blame] | 81 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 82 | void ecp_valid_param() |
Hanno Becker | 57b684f | 2018-12-18 12:50:02 +0000 | [diff] [blame] | 83 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | TEST_VALID_PARAM(mbedtls_ecp_group_free(NULL)); |
| 85 | TEST_VALID_PARAM(mbedtls_ecp_keypair_free(NULL)); |
| 86 | TEST_VALID_PARAM(mbedtls_ecp_point_free(NULL)); |
Hanno Becker | 57b684f | 2018-12-18 12:50:02 +0000 | [diff] [blame] | 87 | |
| 88 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 89 | TEST_VALID_PARAM(mbedtls_ecp_restart_free(NULL)); |
Hanno Becker | 57b684f | 2018-12-18 12:50:02 +0000 | [diff] [blame] | 90 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 91 | |
| 92 | exit: |
| 93 | return; |
| 94 | } |
| 95 | /* END_CASE */ |
| 96 | |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 97 | /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 98 | void ecp_invalid_param() |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 99 | { |
| 100 | mbedtls_ecp_group grp; |
| 101 | mbedtls_ecp_keypair kp; |
| 102 | mbedtls_ecp_point P; |
| 103 | mbedtls_mpi m; |
| 104 | const char *x = "deadbeef"; |
| 105 | int valid_fmt = MBEDTLS_ECP_PF_UNCOMPRESSED; |
| 106 | int invalid_fmt = 42; |
| 107 | size_t olen; |
| 108 | unsigned char buf[42] = { 0 }; |
| 109 | const unsigned char *null_buf = NULL; |
| 110 | mbedtls_ecp_group_id valid_group = MBEDTLS_ECP_DP_SECP192R1; |
Andrzej Kurek | f389629 | 2019-02-11 03:44:29 -0500 | [diff] [blame] | 111 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Hanno Becker | 0a4fa9b | 2018-12-18 23:45:29 +0000 | [diff] [blame] | 112 | mbedtls_ecp_restart_ctx restart_ctx; |
Andrzej Kurek | f389629 | 2019-02-11 03:44:29 -0500 | [diff] [blame] | 113 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 114 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | mbedtls_ecp_group_init(&grp); |
| 116 | mbedtls_ecp_point_init(&P); |
Gabor Mezei | 92ca1bc | 2022-09-23 15:25:27 +0200 | [diff] [blame] | 117 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 118 | TEST_INVALID_PARAM(mbedtls_ecp_point_init(NULL)); |
| 119 | TEST_INVALID_PARAM(mbedtls_ecp_keypair_init(NULL)); |
| 120 | TEST_INVALID_PARAM(mbedtls_ecp_group_init(NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 121 | |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 122 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 123 | TEST_INVALID_PARAM(mbedtls_ecp_restart_init(NULL)); |
| 124 | TEST_INVALID_PARAM(mbedtls_ecp_check_budget(NULL, &restart_ctx, 42)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 125 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 126 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 127 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 128 | mbedtls_ecp_copy(NULL, &P)); |
| 129 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 130 | mbedtls_ecp_copy(&P, NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 131 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 132 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 133 | mbedtls_ecp_group_copy(NULL, &grp)); |
| 134 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 135 | mbedtls_ecp_group_copy(&grp, NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 136 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 137 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 138 | mbedtls_ecp_gen_privkey(NULL, |
| 139 | &m, |
| 140 | mbedtls_test_rnd_std_rand, |
| 141 | NULL)); |
| 142 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 143 | mbedtls_ecp_gen_privkey(&grp, |
| 144 | NULL, |
| 145 | mbedtls_test_rnd_std_rand, |
| 146 | NULL)); |
| 147 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 148 | mbedtls_ecp_gen_privkey(&grp, |
| 149 | &m, |
| 150 | NULL, |
| 151 | NULL)); |
Hanno Becker | 549e455 | 2018-12-18 23:45:43 +0000 | [diff] [blame] | 152 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 153 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 154 | mbedtls_ecp_set_zero(NULL)); |
| 155 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 156 | mbedtls_ecp_is_zero(NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 157 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 158 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 159 | mbedtls_ecp_point_cmp(NULL, &P)); |
| 160 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 161 | mbedtls_ecp_point_cmp(&P, NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 162 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 163 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 164 | mbedtls_ecp_point_read_string(NULL, 2, |
| 165 | x, x)); |
| 166 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 167 | mbedtls_ecp_point_read_string(&P, 2, |
| 168 | NULL, x)); |
| 169 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 170 | mbedtls_ecp_point_read_string(&P, 2, |
| 171 | x, NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 172 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 173 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 174 | mbedtls_ecp_point_write_binary(NULL, &P, |
| 175 | valid_fmt, |
| 176 | &olen, |
| 177 | buf, sizeof(buf))); |
| 178 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 179 | mbedtls_ecp_point_write_binary(&grp, NULL, |
| 180 | valid_fmt, |
| 181 | &olen, |
| 182 | buf, sizeof(buf))); |
| 183 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 184 | mbedtls_ecp_point_write_binary(&grp, &P, |
| 185 | invalid_fmt, |
| 186 | &olen, |
| 187 | buf, sizeof(buf))); |
| 188 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 189 | mbedtls_ecp_point_write_binary(&grp, &P, |
| 190 | valid_fmt, |
| 191 | NULL, |
| 192 | buf, sizeof(buf))); |
| 193 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 194 | mbedtls_ecp_point_write_binary(&grp, &P, |
| 195 | valid_fmt, |
| 196 | &olen, |
| 197 | NULL, sizeof(buf))); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 198 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 199 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 200 | mbedtls_ecp_point_read_binary(NULL, &P, buf, |
| 201 | sizeof(buf))); |
| 202 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 203 | mbedtls_ecp_point_read_binary(&grp, NULL, buf, |
| 204 | sizeof(buf))); |
| 205 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 206 | mbedtls_ecp_point_read_binary(&grp, &P, NULL, |
| 207 | sizeof(buf))); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 208 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 209 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 210 | mbedtls_ecp_tls_read_point(NULL, &P, |
| 211 | (const unsigned char **) &buf, |
| 212 | sizeof(buf))); |
| 213 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 214 | mbedtls_ecp_tls_read_point(&grp, NULL, |
| 215 | (const unsigned char **) &buf, |
| 216 | sizeof(buf))); |
| 217 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 218 | mbedtls_ecp_tls_read_point(&grp, &P, &null_buf, |
| 219 | sizeof(buf))); |
| 220 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 221 | mbedtls_ecp_tls_read_point(&grp, &P, NULL, |
| 222 | sizeof(buf))); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 223 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 224 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 225 | mbedtls_ecp_tls_write_point(NULL, &P, |
| 226 | valid_fmt, |
| 227 | &olen, |
| 228 | buf, |
| 229 | sizeof(buf))); |
| 230 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 231 | mbedtls_ecp_tls_write_point(&grp, NULL, |
| 232 | valid_fmt, |
| 233 | &olen, |
| 234 | buf, |
| 235 | sizeof(buf))); |
| 236 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 237 | mbedtls_ecp_tls_write_point(&grp, &P, |
| 238 | invalid_fmt, |
| 239 | &olen, |
| 240 | buf, |
| 241 | sizeof(buf))); |
| 242 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 243 | mbedtls_ecp_tls_write_point(&grp, &P, |
| 244 | valid_fmt, |
| 245 | NULL, |
| 246 | buf, |
| 247 | sizeof(buf))); |
| 248 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 249 | mbedtls_ecp_tls_write_point(&grp, &P, |
| 250 | valid_fmt, |
| 251 | &olen, |
| 252 | NULL, |
| 253 | sizeof(buf))); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 254 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 255 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 256 | mbedtls_ecp_group_load(NULL, valid_group)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 257 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 259 | mbedtls_ecp_tls_read_group(NULL, |
| 260 | (const unsigned char **) &buf, |
| 261 | sizeof(buf))); |
| 262 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 263 | mbedtls_ecp_tls_read_group(&grp, NULL, |
| 264 | sizeof(buf))); |
| 265 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 266 | mbedtls_ecp_tls_read_group(&grp, &null_buf, |
| 267 | sizeof(buf))); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 268 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 269 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 270 | mbedtls_ecp_tls_read_group_id(NULL, |
| 271 | (const unsigned char **) &buf, |
| 272 | sizeof(buf))); |
| 273 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 274 | mbedtls_ecp_tls_read_group_id(&valid_group, NULL, |
| 275 | sizeof(buf))); |
| 276 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 277 | mbedtls_ecp_tls_read_group_id(&valid_group, |
| 278 | &null_buf, |
| 279 | sizeof(buf))); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 280 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 281 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 282 | mbedtls_ecp_tls_write_group(NULL, &olen, |
| 283 | buf, sizeof(buf))); |
| 284 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 285 | mbedtls_ecp_tls_write_group(&grp, NULL, |
| 286 | buf, sizeof(buf))); |
| 287 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 288 | mbedtls_ecp_tls_write_group(&grp, &olen, |
| 289 | NULL, sizeof(buf))); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 290 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 291 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 292 | mbedtls_ecp_mul(NULL, &P, &m, &P, |
| 293 | mbedtls_test_rnd_std_rand, |
| 294 | NULL)); |
| 295 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 296 | mbedtls_ecp_mul(&grp, NULL, &m, &P, |
| 297 | mbedtls_test_rnd_std_rand, |
| 298 | NULL)); |
| 299 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 300 | mbedtls_ecp_mul(&grp, &P, NULL, &P, |
| 301 | mbedtls_test_rnd_std_rand, |
| 302 | NULL)); |
| 303 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 304 | mbedtls_ecp_mul(&grp, &P, &m, NULL, |
| 305 | mbedtls_test_rnd_std_rand, |
| 306 | NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 307 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 308 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 309 | mbedtls_ecp_mul_restartable(NULL, &P, &m, &P, |
| 310 | mbedtls_test_rnd_std_rand, |
| 311 | NULL, NULL)); |
| 312 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 313 | mbedtls_ecp_mul_restartable(&grp, NULL, &m, &P, |
| 314 | mbedtls_test_rnd_std_rand, |
| 315 | NULL, NULL)); |
| 316 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 317 | mbedtls_ecp_mul_restartable(&grp, &P, NULL, &P, |
| 318 | mbedtls_test_rnd_std_rand, |
| 319 | NULL, NULL)); |
| 320 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 321 | mbedtls_ecp_mul_restartable(&grp, &P, &m, NULL, |
| 322 | mbedtls_test_rnd_std_rand, |
| 323 | NULL, NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 324 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 325 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 326 | mbedtls_ecp_muladd(NULL, &P, &m, &P, |
| 327 | &m, &P)); |
| 328 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 329 | mbedtls_ecp_muladd(&grp, NULL, &m, &P, |
| 330 | &m, &P)); |
| 331 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 332 | mbedtls_ecp_muladd(&grp, &P, NULL, &P, |
| 333 | &m, &P)); |
| 334 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 335 | mbedtls_ecp_muladd(&grp, &P, &m, NULL, |
| 336 | &m, &P)); |
| 337 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 338 | mbedtls_ecp_muladd(&grp, &P, &m, &P, |
| 339 | NULL, &P)); |
| 340 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 341 | mbedtls_ecp_muladd(&grp, &P, &m, &P, |
| 342 | &m, NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 343 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 344 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 345 | mbedtls_ecp_muladd_restartable(NULL, &P, &m, &P, |
| 346 | &m, &P, NULL)); |
| 347 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 348 | mbedtls_ecp_muladd_restartable(&grp, NULL, &m, &P, |
| 349 | &m, &P, NULL)); |
| 350 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 351 | mbedtls_ecp_muladd_restartable(&grp, &P, NULL, &P, |
| 352 | &m, &P, NULL)); |
| 353 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 354 | mbedtls_ecp_muladd_restartable(&grp, &P, &m, NULL, |
| 355 | &m, &P, NULL)); |
| 356 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 357 | mbedtls_ecp_muladd_restartable(&grp, &P, &m, &P, |
| 358 | NULL, &P, NULL)); |
| 359 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 360 | mbedtls_ecp_muladd_restartable(&grp, &P, &m, &P, |
| 361 | &m, NULL, NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 362 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 363 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 364 | mbedtls_ecp_check_pubkey(NULL, &P)); |
| 365 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 366 | mbedtls_ecp_check_pubkey(&grp, NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 367 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 368 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 369 | mbedtls_ecp_check_pub_priv(NULL, &kp)); |
| 370 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 371 | mbedtls_ecp_check_pub_priv(&kp, NULL)); |
Hanno Becker | 1959535 | 2018-12-18 23:54:04 +0000 | [diff] [blame] | 372 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 373 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 374 | mbedtls_ecp_check_privkey(NULL, &m)); |
| 375 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 376 | mbedtls_ecp_check_privkey(&grp, NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 377 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 378 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 379 | mbedtls_ecp_gen_keypair_base(NULL, &P, &m, &P, |
| 380 | mbedtls_test_rnd_std_rand, NULL)); |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 381 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 382 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 383 | mbedtls_ecp_gen_keypair_base(&grp, NULL, &m, &P, |
| 384 | mbedtls_test_rnd_std_rand, NULL)); |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 385 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 386 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 387 | mbedtls_ecp_gen_keypair_base(&grp, &P, NULL, &P, |
| 388 | mbedtls_test_rnd_std_rand, NULL)); |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 389 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 390 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 391 | mbedtls_ecp_gen_keypair_base(&grp, &P, &m, NULL, |
| 392 | mbedtls_test_rnd_std_rand, NULL)); |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 393 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 394 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 395 | mbedtls_ecp_gen_keypair_base(&grp, &P, &m, &P, NULL, NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 396 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 397 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 398 | mbedtls_ecp_gen_keypair(NULL, |
| 399 | &m, &P, |
| 400 | mbedtls_test_rnd_std_rand, |
| 401 | NULL)); |
| 402 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 403 | mbedtls_ecp_gen_keypair(&grp, |
| 404 | NULL, &P, |
| 405 | mbedtls_test_rnd_std_rand, |
| 406 | NULL)); |
| 407 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 408 | mbedtls_ecp_gen_keypair(&grp, |
| 409 | &m, NULL, |
| 410 | mbedtls_test_rnd_std_rand, |
| 411 | NULL)); |
| 412 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 413 | mbedtls_ecp_gen_keypair(&grp, |
| 414 | &m, &P, |
| 415 | NULL, |
| 416 | NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 417 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 418 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 419 | mbedtls_ecp_gen_key(valid_group, NULL, |
| 420 | mbedtls_test_rnd_std_rand, |
| 421 | NULL)); |
| 422 | TEST_INVALID_PARAM_RET(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 423 | mbedtls_ecp_gen_key(valid_group, &kp, |
| 424 | NULL, NULL)); |
Hanno Becker | 12dff03 | 2018-12-14 15:08:13 +0000 | [diff] [blame] | 425 | |
| 426 | exit: |
| 427 | return; |
| 428 | } |
| 429 | /* END_CASE */ |
| 430 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 431 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 432 | 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] | 433 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 434 | 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] | 435 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 436 | by_id = mbedtls_ecp_curve_info_from_grp_id(id); |
| 437 | by_tls = mbedtls_ecp_curve_info_from_tls_id(tls_id); |
| 438 | by_name = mbedtls_ecp_curve_info_from_name(name); |
| 439 | TEST_ASSERT(by_id != NULL); |
| 440 | TEST_ASSERT(by_tls != NULL); |
| 441 | TEST_ASSERT(by_name != NULL); |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 442 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 443 | TEST_ASSERT(by_id == by_tls); |
| 444 | TEST_ASSERT(by_id == by_name); |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 445 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 446 | TEST_ASSERT(by_id->bit_size == size); |
| 447 | TEST_ASSERT(size <= MBEDTLS_ECP_MAX_BITS); |
| 448 | TEST_ASSERT(size <= MBEDTLS_ECP_MAX_BYTES * 8); |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 449 | } |
| 450 | /* END_CASE */ |
| 451 | |
| 452 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 453 | void ecp_check_pub(int grp_id, char *x_hex, char *y_hex, char *z_hex, |
| 454 | int ret) |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 455 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 456 | mbedtls_ecp_group grp; |
| 457 | mbedtls_ecp_point P; |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 458 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 459 | mbedtls_ecp_group_init(&grp); |
| 460 | mbedtls_ecp_point_init(&P); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 461 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 462 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, grp_id) == 0); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 463 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 464 | TEST_ASSERT(mbedtls_test_read_mpi(&P.X, x_hex) == 0); |
| 465 | TEST_ASSERT(mbedtls_test_read_mpi(&P.Y, y_hex) == 0); |
| 466 | TEST_ASSERT(mbedtls_test_read_mpi(&P.Z, z_hex) == 0); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 467 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 468 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &P) == ret); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 469 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 470 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 471 | mbedtls_ecp_group_free(&grp); |
| 472 | mbedtls_ecp_point_free(&P); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 473 | } |
| 474 | /* END_CASE */ |
| 475 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 476 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 477 | void ecp_test_vect_restart(int id, |
| 478 | char *dA_str, char *xA_str, char *yA_str, |
| 479 | char *dB_str, char *xZ_str, char *yZ_str, |
| 480 | int max_ops, int min_restarts, int max_restarts) |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 481 | { |
| 482 | /* |
| 483 | * Test for early restart. Based on test vectors like ecp_test_vect(), |
| 484 | * but for the sake of simplicity only does half of each side. It's |
| 485 | * important to test both base point and random point, though, as memory |
| 486 | * management is different in each case. |
| 487 | * |
| 488 | * Don't try using too precise bounds for restarts as the exact number |
| 489 | * will depend on settings such as MBEDTLS_ECP_FIXED_POINT_OPTIM and |
| 490 | * MBEDTLS_ECP_WINDOW_SIZE, as well as implementation details that may |
| 491 | * change in the future. A factor 2 is a minimum safety margin. |
| 492 | * |
Gilles Peskine | f08ca83 | 2023-09-12 19:21:54 +0200 | [diff] [blame] | 493 | * 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] | 494 | * - Random point mult: ~3250M |
| 495 | * - Cold base point mult: ~3300M |
| 496 | * - Hot base point mult: ~1100M |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 497 | * With MBEDTLS_ECP_WINDOW_SIZE set to 2 (minimum): |
Manuel Pégourié-Gonnard | 9c5c78f | 2017-03-20 14:13:07 +0100 | [diff] [blame] | 498 | * - Random point mult: ~3850M |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 499 | */ |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 500 | mbedtls_ecp_restart_ctx ctx; |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 501 | mbedtls_ecp_group grp; |
Manuel Pégourié-Gonnard | 7a28e99 | 2018-10-16 11:22:45 +0200 | [diff] [blame] | 502 | mbedtls_ecp_point R, P; |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 503 | mbedtls_mpi dA, xA, yA, dB, xZ, yZ; |
| 504 | int cnt_restarts; |
| 505 | int ret; |
| 506 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 507 | mbedtls_ecp_restart_init(&ctx); |
| 508 | mbedtls_ecp_group_init(&grp); |
| 509 | mbedtls_ecp_point_init(&R); mbedtls_ecp_point_init(&P); |
| 510 | mbedtls_mpi_init(&dA); mbedtls_mpi_init(&xA); mbedtls_mpi_init(&yA); |
| 511 | mbedtls_mpi_init(&dB); mbedtls_mpi_init(&xZ); mbedtls_mpi_init(&yZ); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 512 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 513 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 514 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 515 | TEST_ASSERT(mbedtls_test_read_mpi(&dA, dA_str) == 0); |
| 516 | TEST_ASSERT(mbedtls_test_read_mpi(&xA, xA_str) == 0); |
| 517 | TEST_ASSERT(mbedtls_test_read_mpi(&yA, yA_str) == 0); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 518 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 519 | TEST_ASSERT(mbedtls_test_read_mpi(&dB, dB_str) == 0); |
| 520 | TEST_ASSERT(mbedtls_test_read_mpi(&xZ, xZ_str) == 0); |
| 521 | TEST_ASSERT(mbedtls_test_read_mpi(&yZ, yZ_str) == 0); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 522 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 523 | mbedtls_ecp_set_max_ops((unsigned) max_ops); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 524 | |
| 525 | /* Base point case */ |
| 526 | cnt_restarts = 0; |
| 527 | do { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 528 | ECP_PT_RESET(&R); |
| 529 | ret = mbedtls_ecp_mul_restartable(&grp, &R, &dA, &grp.G, NULL, NULL, &ctx); |
| 530 | } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 531 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 532 | TEST_ASSERT(ret == 0); |
| 533 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xA) == 0); |
| 534 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yA) == 0); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 535 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 536 | TEST_ASSERT(cnt_restarts >= min_restarts); |
| 537 | TEST_ASSERT(cnt_restarts <= max_restarts); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 538 | |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 539 | /* Non-base point case */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 540 | mbedtls_ecp_copy(&P, &R); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 541 | cnt_restarts = 0; |
| 542 | do { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 543 | ECP_PT_RESET(&R); |
| 544 | ret = mbedtls_ecp_mul_restartable(&grp, &R, &dB, &P, NULL, NULL, &ctx); |
| 545 | } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 546 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 547 | TEST_ASSERT(ret == 0); |
| 548 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xZ) == 0); |
| 549 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yZ) == 0); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 550 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 551 | TEST_ASSERT(cnt_restarts >= min_restarts); |
| 552 | TEST_ASSERT(cnt_restarts <= max_restarts); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 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 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 556 | if (min_restarts > 0) { |
| 557 | ret = mbedtls_ecp_mul_restartable(&grp, &R, &dB, &P, NULL, NULL, &ctx); |
| 558 | TEST_ASSERT(ret == MBEDTLS_ERR_ECP_IN_PROGRESS); |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 559 | } |
Manuel Pégourié-Gonnard | 77af79a | 2017-03-14 10:58:00 +0100 | [diff] [blame] | 560 | |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 561 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 562 | mbedtls_ecp_restart_free(&ctx); |
| 563 | mbedtls_ecp_group_free(&grp); |
| 564 | mbedtls_ecp_point_free(&R); mbedtls_ecp_point_free(&P); |
| 565 | mbedtls_mpi_free(&dA); mbedtls_mpi_free(&xA); mbedtls_mpi_free(&yA); |
| 566 | mbedtls_mpi_free(&dB); mbedtls_mpi_free(&xZ); mbedtls_mpi_free(&yZ); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 567 | } |
| 568 | /* END_CASE */ |
| 569 | |
Manuel Pégourié-Gonnard | df31076 | 2022-12-06 12:14:49 +0100 | [diff] [blame] | 570 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 571 | void ecp_muladd_restart(int id, char *xR_str, char *yR_str, |
| 572 | char *u1_str, char *u2_str, |
| 573 | char *xQ_str, char *yQ_str, |
| 574 | int max_ops, int min_restarts, int max_restarts) |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 575 | { |
| 576 | /* |
| 577 | * Compute R = u1 * G + u2 * Q |
| 578 | * (test vectors mostly taken from ECDSA intermediate results) |
| 579 | * |
| 580 | * See comments at the top of ecp_test_vect_restart() |
| 581 | */ |
| 582 | mbedtls_ecp_restart_ctx ctx; |
| 583 | mbedtls_ecp_group grp; |
| 584 | mbedtls_ecp_point R, Q; |
| 585 | mbedtls_mpi u1, u2, xR, yR; |
| 586 | int cnt_restarts; |
| 587 | int ret; |
| 588 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 589 | mbedtls_ecp_restart_init(&ctx); |
| 590 | mbedtls_ecp_group_init(&grp); |
| 591 | mbedtls_ecp_point_init(&R); |
| 592 | mbedtls_ecp_point_init(&Q); |
| 593 | mbedtls_mpi_init(&u1); mbedtls_mpi_init(&u2); |
| 594 | mbedtls_mpi_init(&xR); mbedtls_mpi_init(&yR); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 595 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 596 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 597 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 598 | TEST_ASSERT(mbedtls_test_read_mpi(&u1, u1_str) == 0); |
| 599 | TEST_ASSERT(mbedtls_test_read_mpi(&u2, u2_str) == 0); |
| 600 | TEST_ASSERT(mbedtls_test_read_mpi(&xR, xR_str) == 0); |
| 601 | TEST_ASSERT(mbedtls_test_read_mpi(&yR, yR_str) == 0); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 602 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 603 | TEST_ASSERT(mbedtls_test_read_mpi(&Q.X, xQ_str) == 0); |
| 604 | TEST_ASSERT(mbedtls_test_read_mpi(&Q.Y, yQ_str) == 0); |
| 605 | TEST_ASSERT(mbedtls_mpi_lset(&Q.Z, 1) == 0); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 606 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 607 | mbedtls_ecp_set_max_ops((unsigned) max_ops); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 608 | |
| 609 | cnt_restarts = 0; |
| 610 | do { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 611 | ECP_PT_RESET(&R); |
| 612 | ret = mbedtls_ecp_muladd_restartable(&grp, &R, |
| 613 | &u1, &grp.G, &u2, &Q, &ctx); |
| 614 | } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 615 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 616 | TEST_ASSERT(ret == 0); |
| 617 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xR) == 0); |
| 618 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yR) == 0); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 619 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 620 | TEST_ASSERT(cnt_restarts >= min_restarts); |
| 621 | TEST_ASSERT(cnt_restarts <= max_restarts); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 622 | |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 623 | /* Do we leak memory when aborting an operation? |
| 624 | * This test only makes sense when we actually restart */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 625 | if (min_restarts > 0) { |
| 626 | ret = mbedtls_ecp_muladd_restartable(&grp, &R, |
| 627 | &u1, &grp.G, &u2, &Q, &ctx); |
| 628 | TEST_ASSERT(ret == MBEDTLS_ERR_ECP_IN_PROGRESS); |
Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 629 | } |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 630 | |
| 631 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 632 | mbedtls_ecp_restart_free(&ctx); |
| 633 | mbedtls_ecp_group_free(&grp); |
| 634 | mbedtls_ecp_point_free(&R); |
| 635 | mbedtls_ecp_point_free(&Q); |
| 636 | mbedtls_mpi_free(&u1); mbedtls_mpi_free(&u2); |
| 637 | mbedtls_mpi_free(&xR); mbedtls_mpi_free(&yR); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 638 | } |
| 639 | /* END_CASE */ |
| 640 | |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 641 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 642 | void ecp_test_vect(int id, char *dA_str, char *xA_str, char *yA_str, |
| 643 | char *dB_str, char *xB_str, char *yB_str, |
| 644 | char *xZ_str, char *yZ_str) |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 645 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 646 | mbedtls_ecp_group grp; |
| 647 | mbedtls_ecp_point R; |
| 648 | mbedtls_mpi dA, xA, yA, dB, xB, yB, xZ, yZ; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 649 | mbedtls_test_rnd_pseudo_info rnd_info; |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 650 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 651 | mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&R); |
| 652 | mbedtls_mpi_init(&dA); mbedtls_mpi_init(&xA); mbedtls_mpi_init(&yA); mbedtls_mpi_init(&dB); |
| 653 | mbedtls_mpi_init(&xB); mbedtls_mpi_init(&yB); mbedtls_mpi_init(&xZ); mbedtls_mpi_init(&yZ); |
| 654 | memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info)); |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 655 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 656 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 657 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 658 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &grp.G) == 0); |
Manuel Pégourié-Gonnard | 1c33057 | 2012-11-24 12:05:44 +0100 | [diff] [blame] | 659 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 660 | TEST_ASSERT(mbedtls_test_read_mpi(&dA, dA_str) == 0); |
| 661 | TEST_ASSERT(mbedtls_test_read_mpi(&xA, xA_str) == 0); |
| 662 | TEST_ASSERT(mbedtls_test_read_mpi(&yA, yA_str) == 0); |
| 663 | TEST_ASSERT(mbedtls_test_read_mpi(&dB, dB_str) == 0); |
| 664 | TEST_ASSERT(mbedtls_test_read_mpi(&xB, xB_str) == 0); |
| 665 | TEST_ASSERT(mbedtls_test_read_mpi(&yB, yB_str) == 0); |
| 666 | TEST_ASSERT(mbedtls_test_read_mpi(&xZ, xZ_str) == 0); |
| 667 | TEST_ASSERT(mbedtls_test_read_mpi(&yZ, yZ_str) == 0); |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 668 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 669 | TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dA, &grp.G, |
| 670 | &mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0); |
| 671 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xA) == 0); |
| 672 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yA) == 0); |
| 673 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0); |
| 674 | TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dB, &R, NULL, NULL) == 0); |
| 675 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xZ) == 0); |
| 676 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yZ) == 0); |
| 677 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0); |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 678 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 679 | TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dB, &grp.G, NULL, NULL) == 0); |
| 680 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xB) == 0); |
| 681 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yB) == 0); |
| 682 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0); |
| 683 | TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dA, &R, |
| 684 | &mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0); |
| 685 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xZ) == 0); |
| 686 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yZ) == 0); |
| 687 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0); |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 688 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 689 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 690 | mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&R); |
| 691 | mbedtls_mpi_free(&dA); mbedtls_mpi_free(&xA); mbedtls_mpi_free(&yA); mbedtls_mpi_free(&dB); |
| 692 | 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] | 693 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 694 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 695 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 696 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 697 | void ecp_test_vec_x(int id, char *dA_hex, char *xA_hex, char *dB_hex, |
| 698 | char *xB_hex, char *xS_hex) |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 699 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 700 | mbedtls_ecp_group grp; |
| 701 | mbedtls_ecp_point R; |
| 702 | mbedtls_mpi dA, xA, dB, xB, xS; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 703 | mbedtls_test_rnd_pseudo_info rnd_info; |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 704 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 705 | mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&R); |
| 706 | mbedtls_mpi_init(&dA); mbedtls_mpi_init(&xA); |
| 707 | mbedtls_mpi_init(&dB); mbedtls_mpi_init(&xB); |
| 708 | mbedtls_mpi_init(&xS); |
| 709 | memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info)); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 710 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 711 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 712 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 713 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &grp.G) == 0); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 714 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 715 | TEST_ASSERT(mbedtls_test_read_mpi(&dA, dA_hex) == 0); |
| 716 | TEST_ASSERT(mbedtls_test_read_mpi(&dB, dB_hex) == 0); |
| 717 | TEST_ASSERT(mbedtls_test_read_mpi(&xA, xA_hex) == 0); |
| 718 | TEST_ASSERT(mbedtls_test_read_mpi(&xB, xB_hex) == 0); |
| 719 | TEST_ASSERT(mbedtls_test_read_mpi(&xS, xS_hex) == 0); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 720 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 721 | TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dA, &grp.G, |
| 722 | &mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0); |
| 723 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0); |
| 724 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xA) == 0); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 725 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 726 | TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dB, &R, |
| 727 | &mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0); |
| 728 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0); |
| 729 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xS) == 0); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 730 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 731 | TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dB, &grp.G, NULL, NULL) == 0); |
| 732 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0); |
| 733 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xB) == 0); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 734 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 735 | TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dA, &R, NULL, NULL) == 0); |
| 736 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0); |
| 737 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xS) == 0); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 738 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 739 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 740 | mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&R); |
| 741 | mbedtls_mpi_free(&dA); mbedtls_mpi_free(&xA); |
| 742 | mbedtls_mpi_free(&dB); mbedtls_mpi_free(&xB); |
| 743 | mbedtls_mpi_free(&xS); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 744 | } |
| 745 | /* END_CASE */ |
| 746 | |
| 747 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 748 | void ecp_test_mul(int id, data_t *n_hex, |
| 749 | data_t *Px_hex, data_t *Py_hex, data_t *Pz_hex, |
| 750 | data_t *nPx_hex, data_t *nPy_hex, data_t *nPz_hex, |
| 751 | int expected_ret) |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 752 | { |
| 753 | mbedtls_ecp_group grp; |
| 754 | mbedtls_ecp_point P, nP, R; |
| 755 | mbedtls_mpi n; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 756 | mbedtls_test_rnd_pseudo_info rnd_info; |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 757 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 758 | mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&R); |
| 759 | mbedtls_ecp_point_init(&P); mbedtls_ecp_point_init(&nP); |
| 760 | mbedtls_mpi_init(&n); |
| 761 | memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info)); |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 762 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 763 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 764 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 765 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &grp.G) == 0); |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 766 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 767 | TEST_ASSERT(mbedtls_mpi_read_binary(&n, n_hex->x, n_hex->len) == 0); |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 768 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 769 | TEST_ASSERT(mbedtls_mpi_read_binary(&P.X, Px_hex->x, Px_hex->len) == 0); |
| 770 | TEST_ASSERT(mbedtls_mpi_read_binary(&P.Y, Py_hex->x, Py_hex->len) == 0); |
| 771 | TEST_ASSERT(mbedtls_mpi_read_binary(&P.Z, Pz_hex->x, Pz_hex->len) == 0); |
| 772 | TEST_ASSERT(mbedtls_mpi_read_binary(&nP.X, nPx_hex->x, nPx_hex->len) |
| 773 | == 0); |
| 774 | TEST_ASSERT(mbedtls_mpi_read_binary(&nP.Y, nPy_hex->x, nPy_hex->len) |
| 775 | == 0); |
| 776 | TEST_ASSERT(mbedtls_mpi_read_binary(&nP.Z, nPz_hex->x, nPz_hex->len) |
| 777 | == 0); |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 778 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 779 | TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &n, &P, |
| 780 | &mbedtls_test_rnd_pseudo_rand, &rnd_info) |
| 781 | == expected_ret); |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 782 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 783 | if (expected_ret == 0) { |
| 784 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&nP.X, &R.X) == 0); |
| 785 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&nP.Y, &R.Y) == 0); |
| 786 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&nP.Z, &R.Z) == 0); |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 790 | mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&R); |
| 791 | mbedtls_ecp_point_free(&P); mbedtls_ecp_point_free(&nP); |
| 792 | mbedtls_mpi_free(&n); |
Janos Follath | 182b0b9 | 2019-04-26 14:28:19 +0100 | [diff] [blame] | 793 | } |
| 794 | /* END_CASE */ |
| 795 | |
| 796 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 797 | void ecp_test_mul_rng(int id, data_t *d_hex) |
Jonas | 923d579 | 2020-05-13 14:22:45 +0900 | [diff] [blame] | 798 | { |
| 799 | mbedtls_ecp_group grp; |
| 800 | mbedtls_mpi d; |
| 801 | mbedtls_ecp_point Q; |
| 802 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 803 | mbedtls_ecp_group_init(&grp); mbedtls_mpi_init(&d); |
| 804 | mbedtls_ecp_point_init(&Q); |
Jonas | 923d579 | 2020-05-13 14:22:45 +0900 | [diff] [blame] | 805 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 806 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
Jonas | 923d579 | 2020-05-13 14:22:45 +0900 | [diff] [blame] | 807 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 808 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &grp.G) == 0); |
Jonas | 923d579 | 2020-05-13 14:22:45 +0900 | [diff] [blame] | 809 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 810 | TEST_ASSERT(mbedtls_mpi_read_binary(&d, d_hex->x, d_hex->len) == 0); |
Jonas | 923d579 | 2020-05-13 14:22:45 +0900 | [diff] [blame] | 811 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 812 | TEST_ASSERT(mbedtls_ecp_mul(&grp, &Q, &d, &grp.G, |
| 813 | &mbedtls_test_rnd_zero_rand, NULL) |
| 814 | == MBEDTLS_ERR_ECP_RANDOM_FAILED); |
Jonas | 923d579 | 2020-05-13 14:22:45 +0900 | [diff] [blame] | 815 | |
| 816 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 817 | mbedtls_ecp_group_free(&grp); mbedtls_mpi_free(&d); |
| 818 | mbedtls_ecp_point_free(&Q); |
Jonas | 923d579 | 2020-05-13 14:22:45 +0900 | [diff] [blame] | 819 | } |
| 820 | /* END_CASE */ |
| 821 | |
Gilles Peskine | ca91ee4 | 2021-04-03 18:31:01 +0200 | [diff] [blame] | 822 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 823 | void ecp_muladd(int id, |
| 824 | data_t *u1_bin, data_t *P1_bin, |
| 825 | data_t *u2_bin, data_t *P2_bin, |
| 826 | data_t *expected_result) |
Gilles Peskine | ca91ee4 | 2021-04-03 18:31:01 +0200 | [diff] [blame] | 827 | { |
| 828 | /* Compute R = u1 * P1 + u2 * P2 */ |
| 829 | mbedtls_ecp_group grp; |
| 830 | mbedtls_ecp_point P1, P2, R; |
| 831 | mbedtls_mpi u1, u2; |
| 832 | uint8_t actual_result[MBEDTLS_ECP_MAX_PT_LEN]; |
| 833 | size_t len; |
| 834 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 835 | mbedtls_ecp_group_init(&grp); |
| 836 | mbedtls_ecp_point_init(&P1); |
| 837 | mbedtls_ecp_point_init(&P2); |
| 838 | mbedtls_ecp_point_init(&R); |
| 839 | mbedtls_mpi_init(&u1); |
| 840 | mbedtls_mpi_init(&u2); |
Gilles Peskine | ca91ee4 | 2021-04-03 18:31:01 +0200 | [diff] [blame] | 841 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 842 | TEST_EQUAL(0, mbedtls_ecp_group_load(&grp, id)); |
| 843 | TEST_EQUAL(0, mbedtls_mpi_read_binary(&u1, u1_bin->x, u1_bin->len)); |
| 844 | TEST_EQUAL(0, mbedtls_mpi_read_binary(&u2, u2_bin->x, u2_bin->len)); |
| 845 | TEST_EQUAL(0, mbedtls_ecp_point_read_binary(&grp, &P1, |
| 846 | P1_bin->x, P1_bin->len)); |
| 847 | TEST_EQUAL(0, mbedtls_ecp_point_read_binary(&grp, &P2, |
| 848 | P2_bin->x, P2_bin->len)); |
Gilles Peskine | ca91ee4 | 2021-04-03 18:31:01 +0200 | [diff] [blame] | 849 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 850 | TEST_EQUAL(0, mbedtls_ecp_muladd(&grp, &R, &u1, &P1, &u2, &P2)); |
| 851 | TEST_EQUAL(0, mbedtls_ecp_point_write_binary( |
| 852 | &grp, &R, MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 853 | &len, actual_result, sizeof(actual_result))); |
| 854 | TEST_ASSERT(len <= MBEDTLS_ECP_MAX_PT_LEN); |
Gilles Peskine | ca91ee4 | 2021-04-03 18:31:01 +0200 | [diff] [blame] | 855 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 856 | TEST_MEMORY_COMPARE(expected_result->x, expected_result->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 857 | actual_result, len); |
Gilles Peskine | ca91ee4 | 2021-04-03 18:31:01 +0200 | [diff] [blame] | 858 | |
| 859 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 860 | mbedtls_ecp_group_free(&grp); |
| 861 | mbedtls_ecp_point_free(&P1); |
| 862 | mbedtls_ecp_point_free(&P2); |
| 863 | mbedtls_ecp_point_free(&R); |
| 864 | mbedtls_mpi_free(&u1); |
| 865 | mbedtls_mpi_free(&u2); |
Gilles Peskine | ca91ee4 | 2021-04-03 18:31:01 +0200 | [diff] [blame] | 866 | } |
| 867 | /* END_CASE */ |
| 868 | |
Jonas | 923d579 | 2020-05-13 14:22:45 +0900 | [diff] [blame] | 869 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 870 | void ecp_fast_mod(int id, char *N_str) |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 871 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 872 | mbedtls_ecp_group grp; |
| 873 | mbedtls_mpi N, R; |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 874 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 875 | mbedtls_mpi_init(&N); mbedtls_mpi_init(&R); |
| 876 | mbedtls_ecp_group_init(&grp); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 877 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 878 | TEST_ASSERT(mbedtls_test_read_mpi(&N, N_str) == 0); |
| 879 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
| 880 | TEST_ASSERT(grp.modp != NULL); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 881 | |
| 882 | /* |
| 883 | * Store correct result before we touch N |
| 884 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 885 | 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] | 886 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 887 | TEST_ASSERT(grp.modp(&N) == 0); |
| 888 | TEST_ASSERT(mbedtls_mpi_bitlen(&N) <= grp.pbits + 3); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 889 | |
| 890 | /* |
Paul Bakker | d8b0c5e | 2014-04-11 15:31:33 +0200 | [diff] [blame] | 891 | * 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] | 892 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 893 | TEST_ASSERT(mbedtls_mpi_mod_mpi(&N, &N, &grp.P) == 0); |
| 894 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&N, &R) == 0); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 895 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 896 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 897 | mbedtls_mpi_free(&N); mbedtls_mpi_free(&R); |
| 898 | mbedtls_ecp_group_free(&grp); |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 899 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 900 | /* END_CASE */ |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 901 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 902 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 903 | void ecp_write_binary(int id, char *x, char *y, char *z, int format, |
| 904 | data_t *out, int blen, int ret) |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 905 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 906 | mbedtls_ecp_group grp; |
| 907 | mbedtls_ecp_point P; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 908 | unsigned char buf[256]; |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 909 | size_t olen; |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 910 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 911 | memset(buf, 0, sizeof(buf)); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 912 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 913 | mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&P); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 914 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 915 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 916 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 917 | TEST_ASSERT(mbedtls_test_read_mpi(&P.X, x) == 0); |
| 918 | TEST_ASSERT(mbedtls_test_read_mpi(&P.Y, y) == 0); |
| 919 | TEST_ASSERT(mbedtls_test_read_mpi(&P.Z, z) == 0); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 920 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 921 | TEST_ASSERT(mbedtls_ecp_point_write_binary(&grp, &P, format, |
| 922 | &olen, buf, blen) == ret); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 923 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 924 | if (ret == 0) { |
| 925 | TEST_ASSERT(olen <= MBEDTLS_ECP_MAX_PT_LEN); |
| 926 | 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] | 927 | } |
| 928 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 929 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 930 | mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&P); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 931 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 932 | /* END_CASE */ |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 933 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 934 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 935 | void ecp_read_binary(int id, data_t *buf, char *x, char *y, char *z, |
| 936 | int ret) |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 937 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 938 | mbedtls_ecp_group grp; |
| 939 | mbedtls_ecp_point P; |
| 940 | mbedtls_mpi X, Y, Z; |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 941 | |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 942 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 943 | mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&P); |
| 944 | 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] | 945 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 946 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 947 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 948 | TEST_ASSERT(mbedtls_test_read_mpi(&X, x) == 0); |
| 949 | TEST_ASSERT(mbedtls_test_read_mpi(&Y, y) == 0); |
| 950 | TEST_ASSERT(mbedtls_test_read_mpi(&Z, z) == 0); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 951 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 952 | 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] | 953 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 954 | if (ret == 0) { |
| 955 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.X, &X) == 0); |
| 956 | if (mbedtls_ecp_get_type(&grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { |
| 957 | TEST_ASSERT(mbedtls_mpi_cmp_int(&Y, 0) == 0); |
| 958 | TEST_ASSERT(P.Y.p == NULL); |
| 959 | TEST_ASSERT(mbedtls_mpi_cmp_int(&Z, 1) == 0); |
| 960 | TEST_ASSERT(mbedtls_mpi_cmp_int(&P.Z, 1) == 0); |
| 961 | } else { |
| 962 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.Y, &Y) == 0); |
| 963 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.Z, &Z) == 0); |
Janos Follath | 59b813c | 2019-02-13 10:44:06 +0000 | [diff] [blame] | 964 | } |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 965 | } |
| 966 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 967 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 968 | mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&P); |
| 969 | 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] | 970 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 971 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 972 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 973 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 974 | void mbedtls_ecp_tls_read_point(int id, data_t *buf, char *x, char *y, |
| 975 | char *z, int ret) |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 976 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 977 | mbedtls_ecp_group grp; |
| 978 | mbedtls_ecp_point P; |
| 979 | mbedtls_mpi X, Y, Z; |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 980 | const unsigned char *vbuf = buf->x; |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 981 | |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 982 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 983 | mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&P); |
| 984 | 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] | 985 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 986 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 987 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 988 | TEST_ASSERT(mbedtls_test_read_mpi(&X, x) == 0); |
| 989 | TEST_ASSERT(mbedtls_test_read_mpi(&Y, y) == 0); |
| 990 | TEST_ASSERT(mbedtls_test_read_mpi(&Z, z) == 0); |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 991 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 992 | 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] | 993 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 994 | if (ret == 0) { |
| 995 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.X, &X) == 0); |
| 996 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.Y, &Y) == 0); |
| 997 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.Z, &Z) == 0); |
| 998 | TEST_ASSERT((uint32_t) (vbuf - buf->x) == buf->len); |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1001 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1002 | mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&P); |
| 1003 | 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] | 1004 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1005 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 1006 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1007 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1008 | void ecp_tls_write_read_point(int id) |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1009 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1010 | mbedtls_ecp_group grp; |
| 1011 | mbedtls_ecp_point pt; |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1012 | unsigned char buf[256]; |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 1013 | const unsigned char *vbuf; |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 1014 | size_t olen; |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1015 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1016 | mbedtls_ecp_group_init(&grp); |
| 1017 | mbedtls_ecp_point_init(&pt); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1018 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1019 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1020 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1021 | memset(buf, 0x00, sizeof(buf)); vbuf = buf; |
| 1022 | TEST_ASSERT(mbedtls_ecp_tls_write_point(&grp, &grp.G, |
| 1023 | MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256) == 0); |
| 1024 | TEST_ASSERT(mbedtls_ecp_tls_read_point(&grp, &pt, &vbuf, olen) |
| 1025 | == MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE); |
| 1026 | TEST_ASSERT(vbuf == buf + olen); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1027 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1028 | memset(buf, 0x00, sizeof(buf)); vbuf = buf; |
| 1029 | TEST_ASSERT(mbedtls_ecp_tls_write_point(&grp, &grp.G, |
| 1030 | MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256) == 0); |
| 1031 | TEST_ASSERT(mbedtls_ecp_tls_read_point(&grp, &pt, &vbuf, olen) == 0); |
| 1032 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp.G.X, &pt.X) == 0); |
| 1033 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp.G.Y, &pt.Y) == 0); |
| 1034 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp.G.Z, &pt.Z) == 0); |
| 1035 | TEST_ASSERT(vbuf == buf + olen); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1036 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1037 | memset(buf, 0x00, sizeof(buf)); vbuf = buf; |
| 1038 | TEST_ASSERT(mbedtls_ecp_set_zero(&pt) == 0); |
| 1039 | TEST_ASSERT(mbedtls_ecp_tls_write_point(&grp, &pt, |
| 1040 | MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256) == 0); |
| 1041 | TEST_ASSERT(mbedtls_ecp_tls_read_point(&grp, &pt, &vbuf, olen) == 0); |
| 1042 | TEST_ASSERT(mbedtls_ecp_is_zero(&pt)); |
| 1043 | TEST_ASSERT(vbuf == buf + olen); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1044 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1045 | memset(buf, 0x00, sizeof(buf)); vbuf = buf; |
| 1046 | TEST_ASSERT(mbedtls_ecp_set_zero(&pt) == 0); |
| 1047 | TEST_ASSERT(mbedtls_ecp_tls_write_point(&grp, &pt, |
| 1048 | MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256) == 0); |
| 1049 | TEST_ASSERT(mbedtls_ecp_tls_read_point(&grp, &pt, &vbuf, olen) == 0); |
| 1050 | TEST_ASSERT(mbedtls_ecp_is_zero(&pt)); |
| 1051 | TEST_ASSERT(vbuf == buf + olen); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1052 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1053 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1054 | mbedtls_ecp_group_free(&grp); |
| 1055 | mbedtls_ecp_point_free(&pt); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1056 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1057 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1058 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1059 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1060 | void mbedtls_ecp_tls_read_group(data_t *buf, int result, int bits, |
| 1061 | int record_len) |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1062 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1063 | mbedtls_ecp_group grp; |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1064 | const unsigned char *vbuf = buf->x; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1065 | int ret; |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1066 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1067 | mbedtls_ecp_group_init(&grp); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1068 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1069 | ret = mbedtls_ecp_tls_read_group(&grp, &vbuf, buf->len); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1070 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1071 | TEST_ASSERT(ret == result); |
| 1072 | if (ret == 0) { |
| 1073 | TEST_ASSERT(mbedtls_mpi_bitlen(&grp.P) == (size_t) bits); |
| 1074 | TEST_ASSERT(vbuf - buf->x == record_len); |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 1075 | } |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1076 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1077 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1078 | mbedtls_ecp_group_free(&grp); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1079 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1080 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 1081 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1082 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1083 | void ecp_tls_write_read_group(int id) |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 1084 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1085 | mbedtls_ecp_group grp1, grp2; |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 1086 | unsigned char buf[10]; |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 1087 | const unsigned char *vbuf = buf; |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 1088 | size_t len; |
| 1089 | int ret; |
| 1090 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1091 | mbedtls_ecp_group_init(&grp1); |
| 1092 | mbedtls_ecp_group_init(&grp2); |
| 1093 | memset(buf, 0x00, sizeof(buf)); |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 1094 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1095 | TEST_ASSERT(mbedtls_ecp_group_load(&grp1, id) == 0); |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 1096 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1097 | TEST_ASSERT(mbedtls_ecp_tls_write_group(&grp1, &len, buf, 10) == 0); |
| 1098 | ret = mbedtls_ecp_tls_read_group(&grp2, &vbuf, len); |
| 1099 | TEST_ASSERT(ret == 0); |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 1100 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1101 | if (ret == 0) { |
| 1102 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp1.N, &grp2.N) == 0); |
| 1103 | TEST_ASSERT(grp1.id == grp2.id); |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1106 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1107 | mbedtls_ecp_group_free(&grp1); |
| 1108 | mbedtls_ecp_group_free(&grp2); |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 1109 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1110 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 1111 | |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1112 | /* BEGIN_CASE depends_on:MBEDTLS_ECDH_C:MBEDTLS_ECDSA_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1113 | void mbedtls_ecp_group_metadata(int id, int bit_size, int crv_type, |
| 1114 | char *P, char *A, char *B, |
| 1115 | char *G_x, char *G_y, char *N, |
| 1116 | int tls_id) |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1117 | { |
| 1118 | mbedtls_ecp_group grp, grp_read, grp_cpy; |
| 1119 | const mbedtls_ecp_group_id *g_id; |
Werner Lewis | 9a3463c | 2022-09-20 10:00:07 +0100 | [diff] [blame] | 1120 | mbedtls_ecp_group_id read_g_id; |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1121 | const mbedtls_ecp_curve_info *crv, *crv_tls_id, *crv_name; |
| 1122 | |
| 1123 | mbedtls_mpi exp_P, exp_A, exp_B, exp_G_x, exp_G_y, exp_N; |
| 1124 | |
| 1125 | unsigned char buf[3], ecparameters[3] = { 3, 0, tls_id }; |
| 1126 | const unsigned char *vbuf = buf; |
| 1127 | size_t olen; |
| 1128 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1129 | mbedtls_ecp_group_init(&grp); |
| 1130 | mbedtls_ecp_group_init(&grp_read); |
| 1131 | mbedtls_ecp_group_init(&grp_cpy); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1132 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1133 | mbedtls_mpi_init(&exp_P); |
| 1134 | mbedtls_mpi_init(&exp_A); |
| 1135 | mbedtls_mpi_init(&exp_B); |
| 1136 | mbedtls_mpi_init(&exp_G_x); |
| 1137 | mbedtls_mpi_init(&exp_G_y); |
| 1138 | mbedtls_mpi_init(&exp_N); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1139 | |
| 1140 | // Read expected parameters |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1141 | TEST_EQUAL(mbedtls_test_read_mpi(&exp_P, P), 0); |
| 1142 | TEST_EQUAL(mbedtls_test_read_mpi(&exp_A, A), 0); |
| 1143 | TEST_EQUAL(mbedtls_test_read_mpi(&exp_G_x, G_x), 0); |
| 1144 | TEST_EQUAL(mbedtls_test_read_mpi(&exp_N, N), 0); |
| 1145 | TEST_EQUAL(mbedtls_test_read_mpi(&exp_B, B), 0); |
| 1146 | TEST_EQUAL(mbedtls_test_read_mpi(&exp_G_y, G_y), 0); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1147 | |
Werner Lewis | 505a050 | 2022-08-25 10:29:19 +0100 | [diff] [blame] | 1148 | // Convert exp_A to internal representation (A+2)/4 |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1149 | if (crv_type == MBEDTLS_ECP_TYPE_MONTGOMERY) { |
| 1150 | TEST_EQUAL(mbedtls_mpi_add_int(&exp_A, &exp_A, 2), 0); |
| 1151 | TEST_EQUAL(mbedtls_mpi_div_int(&exp_A, NULL, &exp_A, 4), 0); |
Werner Lewis | 505a050 | 2022-08-25 10:29:19 +0100 | [diff] [blame] | 1152 | } |
| 1153 | |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1154 | // Load group |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1155 | TEST_EQUAL(mbedtls_ecp_group_load(&grp, id), 0); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1156 | |
| 1157 | // Compare group with expected parameters |
| 1158 | // A is NULL for SECPxxxR1 curves |
| 1159 | // B and G_y are NULL for curve25519 and curve448 |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1160 | TEST_EQUAL(mbedtls_mpi_cmp_mpi(&exp_P, &grp.P), 0); |
| 1161 | if (*A != 0) { |
| 1162 | TEST_EQUAL(mbedtls_mpi_cmp_mpi(&exp_A, &grp.A), 0); |
| 1163 | } |
| 1164 | if (*B != 0) { |
| 1165 | TEST_EQUAL(mbedtls_mpi_cmp_mpi(&exp_B, &grp.B), 0); |
| 1166 | } |
| 1167 | TEST_EQUAL(mbedtls_mpi_cmp_mpi(&exp_G_x, &grp.G.X), 0); |
| 1168 | if (*G_y != 0) { |
| 1169 | TEST_EQUAL(mbedtls_mpi_cmp_mpi(&exp_G_y, &grp.G.Y), 0); |
| 1170 | } |
| 1171 | TEST_EQUAL(mbedtls_mpi_cmp_mpi(&exp_N, &grp.N), 0); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1172 | |
| 1173 | // Load curve info and compare with known values |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1174 | crv = mbedtls_ecp_curve_info_from_grp_id(id); |
| 1175 | TEST_EQUAL(crv->grp_id, id); |
| 1176 | TEST_EQUAL(crv->bit_size, bit_size); |
| 1177 | TEST_EQUAL(crv->tls_id, tls_id); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1178 | |
| 1179 | // Load curve from TLS ID and name, and compare IDs |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1180 | crv_tls_id = mbedtls_ecp_curve_info_from_tls_id(crv->tls_id); |
| 1181 | crv_name = mbedtls_ecp_curve_info_from_name(crv->name); |
| 1182 | TEST_EQUAL(crv_tls_id->grp_id, id); |
| 1183 | TEST_EQUAL(crv_name->grp_id, id); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1184 | |
Werner Lewis | 9a3463c | 2022-09-20 10:00:07 +0100 | [diff] [blame] | 1185 | // Validate write_group against test data |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1186 | TEST_EQUAL(mbedtls_ecp_tls_write_group(&grp, &olen, |
| 1187 | buf, sizeof(buf)), |
| 1188 | 0); |
| 1189 | TEST_EQUAL(mbedtls_test_hexcmp(buf, ecparameters, olen, |
| 1190 | sizeof(ecparameters)), |
| 1191 | 0); |
Werner Lewis | 9a3463c | 2022-09-20 10:00:07 +0100 | [diff] [blame] | 1192 | |
| 1193 | // Read group from buffer and compare with expected ID |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1194 | TEST_EQUAL(mbedtls_ecp_tls_read_group_id(&read_g_id, &vbuf, olen), |
| 1195 | 0); |
| 1196 | TEST_EQUAL(read_g_id, id); |
Werner Lewis | 2b984de | 2022-09-20 12:05:00 +0100 | [diff] [blame] | 1197 | vbuf = buf; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1198 | TEST_EQUAL(mbedtls_ecp_tls_read_group(&grp_read, &vbuf, olen), |
| 1199 | 0); |
| 1200 | TEST_EQUAL(grp_read.id, id); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1201 | |
| 1202 | // Check curve type, and if it can be used for ECDH/ECDSA |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1203 | TEST_EQUAL(mbedtls_ecp_get_type(&grp), crv_type); |
| 1204 | TEST_EQUAL(mbedtls_ecdh_can_do(id), 1); |
| 1205 | TEST_EQUAL(mbedtls_ecdsa_can_do(id), |
| 1206 | crv_type == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1207 | |
| 1208 | // Copy group and compare with original |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1209 | TEST_EQUAL(mbedtls_ecp_group_copy(&grp_cpy, &grp), 0); |
Gowtham Suresh Kumar | 21f2b7a | 2023-07-10 22:50:29 +0100 | [diff] [blame] | 1210 | TEST_ASSERT(grp_cpy.T == NULL); |
| 1211 | TEST_ASSERT(grp_cpy.T_size == 0); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1212 | TEST_EQUAL(mbedtls_ecp_group_cmp(&grp, &grp_cpy), 0); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1213 | |
| 1214 | // Check curve is in curve list and group ID list |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1215 | for (crv = mbedtls_ecp_curve_list(); |
| 1216 | crv->grp_id != MBEDTLS_ECP_DP_NONE && |
| 1217 | crv->grp_id != (unsigned) id; |
| 1218 | crv++) { |
| 1219 | ; |
| 1220 | } |
| 1221 | TEST_EQUAL(crv->grp_id, id); |
| 1222 | for (g_id = mbedtls_ecp_grp_id_list(); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1223 | *g_id != MBEDTLS_ECP_DP_NONE && *g_id != (unsigned) id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1224 | g_id++) { |
| 1225 | ; |
| 1226 | } |
| 1227 | TEST_EQUAL(*g_id, (unsigned) id); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1228 | |
| 1229 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1230 | mbedtls_ecp_group_free(&grp); mbedtls_ecp_group_free(&grp_cpy); |
| 1231 | mbedtls_ecp_group_free(&grp_read); |
| 1232 | mbedtls_mpi_free(&exp_P); mbedtls_mpi_free(&exp_A); |
| 1233 | mbedtls_mpi_free(&exp_B); mbedtls_mpi_free(&exp_G_x); |
| 1234 | mbedtls_mpi_free(&exp_G_y); mbedtls_mpi_free(&exp_N); |
Werner Lewis | 60b50e1 | 2022-08-15 11:43:56 +0100 | [diff] [blame] | 1235 | } |
| 1236 | /* END_CASE */ |
| 1237 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1238 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1239 | 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] | 1240 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1241 | mbedtls_ecp_group grp; |
| 1242 | mbedtls_mpi d; |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1243 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1244 | mbedtls_ecp_group_init(&grp); |
| 1245 | mbedtls_mpi_init(&d); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1246 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1247 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
| 1248 | TEST_ASSERT(mbedtls_test_read_mpi(&d, key_hex) == 0); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1249 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1250 | TEST_ASSERT(mbedtls_ecp_check_privkey(&grp, &d) == ret); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1251 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1252 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1253 | mbedtls_ecp_group_free(&grp); |
| 1254 | mbedtls_mpi_free(&d); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1255 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1256 | /* END_CASE */ |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1257 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1258 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1259 | void mbedtls_ecp_check_pub_priv(int id_pub, char *Qx_pub, char *Qy_pub, |
| 1260 | int id, char *d, char *Qx, char *Qy, |
| 1261 | int ret) |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1262 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1263 | mbedtls_ecp_keypair pub, prv; |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1264 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1265 | mbedtls_ecp_keypair_init(&pub); |
| 1266 | mbedtls_ecp_keypair_init(&prv); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1267 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1268 | if (id_pub != MBEDTLS_ECP_DP_NONE) { |
| 1269 | TEST_ASSERT(mbedtls_ecp_group_load(&pub.grp, id_pub) == 0); |
| 1270 | } |
| 1271 | 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] | 1272 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1273 | if (id != MBEDTLS_ECP_DP_NONE) { |
| 1274 | TEST_ASSERT(mbedtls_ecp_group_load(&prv.grp, id) == 0); |
| 1275 | } |
| 1276 | TEST_ASSERT(mbedtls_ecp_point_read_string(&prv.Q, 16, Qx, Qy) == 0); |
| 1277 | TEST_ASSERT(mbedtls_test_read_mpi(&prv.d, d) == 0); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1278 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1279 | TEST_ASSERT(mbedtls_ecp_check_pub_priv(&pub, &prv) == ret); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1280 | |
| 1281 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1282 | mbedtls_ecp_keypair_free(&pub); |
| 1283 | mbedtls_ecp_keypair_free(&prv); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1284 | } |
| 1285 | /* END_CASE */ |
| 1286 | |
| 1287 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1288 | void mbedtls_ecp_gen_keypair(int id) |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1289 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1290 | mbedtls_ecp_group grp; |
| 1291 | mbedtls_ecp_point Q; |
| 1292 | mbedtls_mpi d; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1293 | mbedtls_test_rnd_pseudo_info rnd_info; |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1294 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1295 | mbedtls_ecp_group_init(&grp); |
| 1296 | mbedtls_ecp_point_init(&Q); |
| 1297 | mbedtls_mpi_init(&d); |
| 1298 | memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info)); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1299 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1300 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1301 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1302 | TEST_ASSERT(mbedtls_ecp_gen_keypair(&grp, &d, &Q, |
| 1303 | &mbedtls_test_rnd_pseudo_rand, |
| 1304 | &rnd_info) == 0); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1305 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1306 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &Q) == 0); |
| 1307 | TEST_ASSERT(mbedtls_ecp_check_privkey(&grp, &d) == 0); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1308 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1309 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1310 | mbedtls_ecp_group_free(&grp); |
| 1311 | mbedtls_ecp_point_free(&Q); |
| 1312 | mbedtls_mpi_free(&d); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1313 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1314 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1315 | |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1316 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1317 | void mbedtls_ecp_gen_key(int id) |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1318 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1319 | mbedtls_ecp_keypair key; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1320 | mbedtls_test_rnd_pseudo_info rnd_info; |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1321 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1322 | mbedtls_ecp_keypair_init(&key); |
| 1323 | memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info)); |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1324 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1325 | TEST_ASSERT(mbedtls_ecp_gen_key(id, &key, |
| 1326 | &mbedtls_test_rnd_pseudo_rand, |
| 1327 | &rnd_info) == 0); |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1328 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1329 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&key.grp, &key.Q) == 0); |
| 1330 | 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] | 1331 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1332 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1333 | mbedtls_ecp_keypair_free(&key); |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1334 | } |
| 1335 | /* END_CASE */ |
| 1336 | |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1337 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1338 | 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] | 1339 | { |
| 1340 | int ret = 0; |
| 1341 | mbedtls_ecp_keypair key; |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1342 | mbedtls_ecp_keypair key2; |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1343 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1344 | mbedtls_ecp_keypair_init(&key); |
| 1345 | mbedtls_ecp_keypair_init(&key2); |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1346 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1347 | ret = mbedtls_ecp_read_key(grp_id, &key, in_key->x, in_key->len); |
| 1348 | TEST_ASSERT(ret == expected); |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1349 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1350 | if (expected == 0) { |
| 1351 | ret = mbedtls_ecp_check_privkey(&key.grp, &key.d); |
| 1352 | TEST_ASSERT(ret == 0); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1353 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1354 | if (canonical) { |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1355 | unsigned char buf[MBEDTLS_ECP_MAX_BYTES]; |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1356 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1357 | ret = mbedtls_ecp_write_key(&key, buf, in_key->len); |
| 1358 | TEST_ASSERT(ret == 0); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1359 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1360 | TEST_MEMORY_COMPARE(in_key->x, in_key->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1361 | buf, in_key->len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1362 | } else { |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1363 | unsigned char export1[MBEDTLS_ECP_MAX_BYTES]; |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1364 | unsigned char export2[MBEDTLS_ECP_MAX_BYTES]; |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1365 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1366 | ret = mbedtls_ecp_write_key(&key, export1, in_key->len); |
| 1367 | TEST_ASSERT(ret == 0); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1368 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1369 | ret = mbedtls_ecp_read_key(grp_id, &key2, export1, in_key->len); |
| 1370 | TEST_ASSERT(ret == expected); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1371 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1372 | ret = mbedtls_ecp_write_key(&key2, export2, in_key->len); |
| 1373 | TEST_ASSERT(ret == 0); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1374 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1375 | TEST_MEMORY_COMPARE(export1, in_key->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1376 | export2, in_key->len); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1377 | } |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1381 | mbedtls_ecp_keypair_free(&key); |
| 1382 | mbedtls_ecp_keypair_free(&key2); |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1383 | } |
| 1384 | /* END_CASE */ |
| 1385 | |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 1386 | /* BEGIN_CASE depends_on:HAVE_FIX_NEGATIVE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1387 | void fix_negative(data_t *N_bin, int c, int bits) |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 1388 | { |
| 1389 | mbedtls_mpi C, M, N; |
| 1390 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1391 | mbedtls_mpi_init(&C); |
| 1392 | mbedtls_mpi_init(&M); |
| 1393 | mbedtls_mpi_init(&N); |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 1394 | |
Gilles Peskine | 392d101 | 2021-04-09 15:46:51 +0200 | [diff] [blame] | 1395 | /* C = - c * 2^bits (positive since c is negative) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1396 | TEST_EQUAL(0, mbedtls_mpi_lset(&C, -c)); |
| 1397 | TEST_EQUAL(0, mbedtls_mpi_shift_l(&C, bits)); |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 1398 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1399 | TEST_EQUAL(0, mbedtls_mpi_read_binary(&N, N_bin->x, N_bin->len)); |
| 1400 | TEST_EQUAL(0, mbedtls_mpi_grow(&N, C.n)); |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 1401 | |
Gilles Peskine | 392d101 | 2021-04-09 15:46:51 +0200 | [diff] [blame] | 1402 | /* M = N - C = - ( C - N ) (expected result of fix_negative) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1403 | TEST_EQUAL(0, mbedtls_mpi_sub_mpi(&M, &N, &C)); |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 1404 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1405 | mbedtls_ecp_fix_negative(&N, c, bits); |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 1406 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1407 | TEST_EQUAL(0, mbedtls_mpi_cmp_mpi(&N, &M)); |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 1408 | |
| 1409 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1410 | mbedtls_mpi_free(&C); |
| 1411 | mbedtls_mpi_free(&M); |
| 1412 | mbedtls_mpi_free(&N); |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 1413 | } |
| 1414 | /* END_CASE */ |
| 1415 | |
Gilles Peskine | 1888285 | 2021-03-24 12:01:02 +0100 | [diff] [blame] | 1416 | /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_MONTGOMERY_ENABLED */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1417 | void genkey_mx_known_answer(int bits, data_t *seed, data_t *expected) |
Gilles Peskine | 1888285 | 2021-03-24 12:01:02 +0100 | [diff] [blame] | 1418 | { |
| 1419 | mbedtls_test_rnd_buf_info rnd_info; |
| 1420 | mbedtls_mpi d; |
| 1421 | int ret; |
| 1422 | uint8_t *actual = NULL; |
| 1423 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1424 | mbedtls_mpi_init(&d); |
Gilles Peskine | 1888285 | 2021-03-24 12:01:02 +0100 | [diff] [blame] | 1425 | rnd_info.buf = seed->x; |
| 1426 | rnd_info.length = seed->len; |
| 1427 | rnd_info.fallback_f_rng = NULL; |
| 1428 | rnd_info.fallback_p_rng = NULL; |
| 1429 | |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 1430 | TEST_CALLOC(actual, expected->len); |
Gilles Peskine | 1888285 | 2021-03-24 12:01:02 +0100 | [diff] [blame] | 1431 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1432 | ret = mbedtls_ecp_gen_privkey_mx(bits, &d, |
| 1433 | mbedtls_test_rnd_buffer_rand, &rnd_info); |
Gilles Peskine | 1888285 | 2021-03-24 12:01:02 +0100 | [diff] [blame] | 1434 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1435 | if (expected->len == 0) { |
Gilles Peskine | 1888285 | 2021-03-24 12:01:02 +0100 | [diff] [blame] | 1436 | /* Expecting an error (happens if there isn't enough randomness) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1437 | TEST_ASSERT(ret != 0); |
| 1438 | } else { |
| 1439 | TEST_EQUAL(ret, 0); |
| 1440 | TEST_EQUAL((size_t) bits + 1, mbedtls_mpi_bitlen(&d)); |
| 1441 | TEST_EQUAL(0, mbedtls_mpi_write_binary(&d, actual, expected->len)); |
Gilles Peskine | 1888285 | 2021-03-24 12:01:02 +0100 | [diff] [blame] | 1442 | /* Test the exact result. This assumes that the output of the |
| 1443 | * RNG is used in a specific way, which is overly constraining. |
| 1444 | * The advantage is that it's easier to test the expected properties |
| 1445 | * of the generated key: |
| 1446 | * - The most significant bit must be at a specific positions |
| 1447 | * (can be enforced by checking the bit-length). |
| 1448 | * - The least significant bits must have specific values |
| 1449 | * (can be enforced by checking these bits). |
| 1450 | * - Other bits must be random (by testing with different RNG outputs, |
| 1451 | * we validate that those bits are indeed influenced by the RNG). */ |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1452 | TEST_MEMORY_COMPARE(expected->x, expected->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1453 | actual, expected->len); |
Gilles Peskine | 1888285 | 2021-03-24 12:01:02 +0100 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1457 | mbedtls_free(actual); |
| 1458 | mbedtls_mpi_free(&d); |
Gilles Peskine | 1888285 | 2021-03-24 12:01:02 +0100 | [diff] [blame] | 1459 | } |
| 1460 | /* END_CASE */ |
| 1461 | |
Werner Lewis | 55a3285 | 2022-08-08 11:53:45 +0100 | [diff] [blame] | 1462 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1463 | void ecp_set_zero(int id, data_t *P_bin) |
Werner Lewis | 55a3285 | 2022-08-08 11:53:45 +0100 | [diff] [blame] | 1464 | { |
| 1465 | mbedtls_ecp_group grp; |
| 1466 | mbedtls_ecp_point pt, zero_pt, nonzero_pt; |
| 1467 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1468 | mbedtls_ecp_group_init(&grp); |
| 1469 | mbedtls_ecp_point_init(&pt); |
| 1470 | mbedtls_ecp_point_init(&zero_pt); |
| 1471 | mbedtls_ecp_point_init(&nonzero_pt); |
Werner Lewis | 55a3285 | 2022-08-08 11:53:45 +0100 | [diff] [blame] | 1472 | |
| 1473 | // Set zero and non-zero points for comparison |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1474 | TEST_EQUAL(mbedtls_ecp_set_zero(&zero_pt), 0); |
| 1475 | TEST_EQUAL(mbedtls_ecp_group_load(&grp, id), 0); |
| 1476 | TEST_EQUAL(mbedtls_ecp_point_read_binary(&grp, &nonzero_pt, |
| 1477 | P_bin->x, P_bin->len), 0); |
| 1478 | TEST_EQUAL(mbedtls_ecp_is_zero(&zero_pt), 1); |
| 1479 | TEST_EQUAL(mbedtls_ecp_is_zero(&nonzero_pt), 0); |
Werner Lewis | 55a3285 | 2022-08-08 11:53:45 +0100 | [diff] [blame] | 1480 | |
| 1481 | // Test initialized point |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1482 | TEST_EQUAL(mbedtls_ecp_set_zero(&pt), 0); |
| 1483 | TEST_EQUAL(mbedtls_ecp_is_zero(&pt), 1); |
| 1484 | TEST_EQUAL(mbedtls_ecp_point_cmp(&zero_pt, &pt), 0); |
| 1485 | TEST_EQUAL(mbedtls_ecp_point_cmp(&nonzero_pt, &zero_pt), |
| 1486 | MBEDTLS_ERR_ECP_BAD_INPUT_DATA); |
Werner Lewis | 55a3285 | 2022-08-08 11:53:45 +0100 | [diff] [blame] | 1487 | |
| 1488 | // Test zeroed point |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1489 | TEST_EQUAL(mbedtls_ecp_set_zero(&pt), 0); |
| 1490 | TEST_EQUAL(mbedtls_ecp_is_zero(&pt), 1); |
| 1491 | TEST_EQUAL(mbedtls_ecp_point_cmp(&zero_pt, &pt), 0); |
| 1492 | TEST_EQUAL(mbedtls_ecp_point_cmp(&nonzero_pt, &pt), |
| 1493 | MBEDTLS_ERR_ECP_BAD_INPUT_DATA); |
Werner Lewis | 55a3285 | 2022-08-08 11:53:45 +0100 | [diff] [blame] | 1494 | |
| 1495 | // Set point to non-zero value |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1496 | TEST_EQUAL(mbedtls_ecp_point_read_binary(&grp, &pt, |
| 1497 | P_bin->x, P_bin->len), 0); |
| 1498 | TEST_EQUAL(mbedtls_ecp_is_zero(&pt), 0); |
| 1499 | TEST_EQUAL(mbedtls_ecp_point_cmp(&zero_pt, &pt), |
| 1500 | MBEDTLS_ERR_ECP_BAD_INPUT_DATA); |
| 1501 | TEST_EQUAL(mbedtls_ecp_point_cmp(&nonzero_pt, &pt), 0); |
Werner Lewis | 55a3285 | 2022-08-08 11:53:45 +0100 | [diff] [blame] | 1502 | |
| 1503 | // Test non-zero point |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1504 | TEST_EQUAL(mbedtls_ecp_set_zero(&pt), 0); |
| 1505 | TEST_EQUAL(mbedtls_ecp_is_zero(&pt), 1); |
| 1506 | TEST_EQUAL(mbedtls_ecp_point_cmp(&zero_pt, &pt), 0); |
| 1507 | TEST_EQUAL(mbedtls_ecp_point_cmp(&nonzero_pt, &pt), |
| 1508 | MBEDTLS_ERR_ECP_BAD_INPUT_DATA); |
Werner Lewis | 55a3285 | 2022-08-08 11:53:45 +0100 | [diff] [blame] | 1509 | |
| 1510 | // Test freed non-zero point |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1511 | TEST_EQUAL(mbedtls_ecp_point_read_binary(&grp, &pt, |
| 1512 | P_bin->x, P_bin->len), 0); |
| 1513 | mbedtls_ecp_point_free(&pt); |
| 1514 | TEST_EQUAL(mbedtls_ecp_set_zero(&pt), 0); |
| 1515 | TEST_EQUAL(mbedtls_ecp_is_zero(&pt), 1); |
| 1516 | TEST_EQUAL(mbedtls_ecp_point_cmp(&zero_pt, &pt), 0); |
| 1517 | TEST_EQUAL(mbedtls_ecp_point_cmp(&nonzero_pt, &pt), |
| 1518 | MBEDTLS_ERR_ECP_BAD_INPUT_DATA); |
Werner Lewis | 55a3285 | 2022-08-08 11:53:45 +0100 | [diff] [blame] | 1519 | |
| 1520 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1521 | mbedtls_ecp_group_free(&grp); |
| 1522 | mbedtls_ecp_point_free(&pt); |
| 1523 | mbedtls_ecp_point_free(&zero_pt); |
| 1524 | mbedtls_ecp_point_free(&nonzero_pt); |
Werner Lewis | 55a3285 | 2022-08-08 11:53:45 +0100 | [diff] [blame] | 1525 | } |
| 1526 | /* END_CASE */ |
| 1527 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1528 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1529 | void ecp_selftest() |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1530 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1531 | TEST_ASSERT(mbedtls_ecp_self_test(1) == 0); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1532 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1533 | /* END_CASE */ |
Dave Rodgman | 46b5cb5 | 2022-06-17 13:41:18 +0100 | [diff] [blame] | 1534 | |
| 1535 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1536 | void ecp_check_order(int id, char *expected_order_hex) |
Dave Rodgman | 46b5cb5 | 2022-06-17 13:41:18 +0100 | [diff] [blame] | 1537 | { |
| 1538 | mbedtls_ecp_group grp; |
| 1539 | mbedtls_mpi expected_n; |
| 1540 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1541 | mbedtls_ecp_group_init(&grp); |
| 1542 | mbedtls_mpi_init(&expected_n); |
Dave Rodgman | 46b5cb5 | 2022-06-17 13:41:18 +0100 | [diff] [blame] | 1543 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1544 | TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0); |
| 1545 | TEST_ASSERT(mbedtls_test_read_mpi(&expected_n, expected_order_hex) == 0); |
Dave Rodgman | 46b5cb5 | 2022-06-17 13:41:18 +0100 | [diff] [blame] | 1546 | |
| 1547 | // check sign bits are well-formed (i.e. 1 or -1) - see #5810 |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1548 | TEST_ASSERT(grp.N.s == -1 || grp.N.s == 1); |
| 1549 | TEST_ASSERT(expected_n.s == -1 || expected_n.s == 1); |
Dave Rodgman | 46b5cb5 | 2022-06-17 13:41:18 +0100 | [diff] [blame] | 1550 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1551 | TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp.N, &expected_n) == 0); |
Dave Rodgman | 46b5cb5 | 2022-06-17 13:41:18 +0100 | [diff] [blame] | 1552 | |
| 1553 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1554 | mbedtls_ecp_group_free(&grp); |
| 1555 | mbedtls_mpi_free(&expected_n); |
Dave Rodgman | 46b5cb5 | 2022-06-17 13:41:18 +0100 | [diff] [blame] | 1556 | } |
| 1557 | /* END_CASE */ |