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