blob: 4ee75a628931439713ccd4ce64b7ee6547dd410f [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/ecp.h"
Paul Bakkerdbd443d2013-08-16 13:38:47 +02003
Manuel Pégourié-Gonnard6c7af4c2015-04-03 16:41:52 +02004#define ECP_PF_UNKNOWN -1
Manuel Pégourié-Gonnard7a28e992018-10-16 11:22:45 +02005
6#define ECP_PT_RESET( x ) \
7 mbedtls_ecp_point_free( x ); \
8 mbedtls_ecp_point_init( x );
Paul Bakker33b43f12013-08-20 11:48:36 +02009/* END_HEADER */
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010010
Paul Bakker33b43f12013-08-20 11:48:36 +020011/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012 * depends_on:MBEDTLS_ECP_C
Paul Bakker33b43f12013-08-20 11:48:36 +020013 * END_DEPENDENCIES
14 */
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +010015
Hanno Becker57b684f2018-12-18 12:50:02 +000016/* BEGIN_CASE */
17void ecp_valid_param( )
18{
Hanno Becker807c1072018-12-18 23:45:14 +000019 TEST_VALID_PARAM( mbedtls_ecp_group_free( NULL ) );
Hanno Becker57b684f2018-12-18 12:50:02 +000020 TEST_VALID_PARAM( mbedtls_ecp_keypair_free( NULL ) );
21 TEST_VALID_PARAM( mbedtls_ecp_point_free( NULL ) );
22
23#if defined(MBEDTLS_ECP_RESTARTABLE)
24 TEST_VALID_PARAM( mbedtls_ecp_restart_free( NULL ) );
25#endif /* MBEDTLS_ECP_RESTARTABLE */
26
27exit:
28 return;
29}
30/* END_CASE */
31
Hanno Becker12dff032018-12-14 15:08:13 +000032/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
33void ecp_invalid_param( )
34{
35 mbedtls_ecp_group grp;
36 mbedtls_ecp_keypair kp;
37 mbedtls_ecp_point P;
38 mbedtls_mpi m;
39 const char *x = "deadbeef";
40 int valid_fmt = MBEDTLS_ECP_PF_UNCOMPRESSED;
41 int invalid_fmt = 42;
42 size_t olen;
43 unsigned char buf[42] = { 0 };
44 const unsigned char *null_buf = NULL;
45 mbedtls_ecp_group_id valid_group = MBEDTLS_ECP_DP_SECP192R1;
Andrzej Kurekf3896292019-02-11 03:44:29 -050046#if defined(MBEDTLS_ECP_RESTARTABLE)
Hanno Becker0a4fa9b2018-12-18 23:45:29 +000047 mbedtls_ecp_restart_ctx restart_ctx;
Andrzej Kurekf3896292019-02-11 03:44:29 -050048#endif /* MBEDTLS_ECP_RESTARTABLE */
Hanno Becker12dff032018-12-14 15:08:13 +000049
50 TEST_INVALID_PARAM( mbedtls_ecp_point_init( NULL ) );
51 TEST_INVALID_PARAM( mbedtls_ecp_keypair_init( NULL ) );
Hanno Becker807c1072018-12-18 23:45:14 +000052 TEST_INVALID_PARAM( mbedtls_ecp_group_init( NULL ) );
Hanno Becker12dff032018-12-14 15:08:13 +000053
Hanno Becker12dff032018-12-14 15:08:13 +000054#if defined(MBEDTLS_ECP_RESTARTABLE)
55 TEST_INVALID_PARAM( mbedtls_ecp_restart_init( NULL ) );
Hanno Becker0a4fa9b2018-12-18 23:45:29 +000056 TEST_INVALID_PARAM( mbedtls_ecp_check_budget( NULL, &restart_ctx, 42 ) );
Hanno Becker12dff032018-12-14 15:08:13 +000057#endif /* MBEDTLS_ECP_RESTARTABLE */
58
59 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
60 mbedtls_ecp_copy( NULL, &P ) );
61 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
62 mbedtls_ecp_copy( &P, NULL ) );
63
64 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
65 mbedtls_ecp_group_copy( NULL, &grp ) );
66 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
67 mbedtls_ecp_group_copy( &grp, NULL ) );
68
69 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
Hanno Becker549e4552018-12-18 23:45:43 +000070 mbedtls_ecp_gen_privkey( NULL,
71 &m,
Ronald Cron351f0ee2020-06-10 12:12:18 +020072 mbedtls_test_rnd_std_rand,
Hanno Becker549e4552018-12-18 23:45:43 +000073 NULL ) );
74 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
75 mbedtls_ecp_gen_privkey( &grp,
76 NULL,
Ronald Cron351f0ee2020-06-10 12:12:18 +020077 mbedtls_test_rnd_std_rand,
Hanno Becker549e4552018-12-18 23:45:43 +000078 NULL ) );
79 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
80 mbedtls_ecp_gen_privkey( &grp,
81 &m,
82 NULL,
83 NULL ) );
84
85 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
Hanno Becker12dff032018-12-14 15:08:13 +000086 mbedtls_ecp_set_zero( NULL ) );
87 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
88 mbedtls_ecp_is_zero( NULL ) );
89
90 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
91 mbedtls_ecp_point_cmp( NULL, &P ) );
92 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
93 mbedtls_ecp_point_cmp( &P, NULL ) );
94
95 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
96 mbedtls_ecp_point_read_string( NULL, 2,
97 x, x ) );
98 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
99 mbedtls_ecp_point_read_string( &P, 2,
100 NULL, x ) );
101 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
102 mbedtls_ecp_point_read_string( &P, 2,
103 x, NULL ) );
104
105 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
106 mbedtls_ecp_point_write_binary( NULL, &P,
107 valid_fmt,
108 &olen,
109 buf, sizeof( buf ) ) );
110 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
111 mbedtls_ecp_point_write_binary( &grp, NULL,
112 valid_fmt,
113 &olen,
114 buf, sizeof( buf ) ) );
115 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
116 mbedtls_ecp_point_write_binary( &grp, &P,
117 invalid_fmt,
118 &olen,
119 buf, sizeof( buf ) ) );
120 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
121 mbedtls_ecp_point_write_binary( &grp, &P,
122 valid_fmt,
123 NULL,
124 buf, sizeof( buf ) ) );
125 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
126 mbedtls_ecp_point_write_binary( &grp, &P,
127 valid_fmt,
128 &olen,
129 NULL, sizeof( buf ) ) );
130
131 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
132 mbedtls_ecp_point_read_binary( NULL, &P, buf,
133 sizeof( buf ) ) );
134 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
135 mbedtls_ecp_point_read_binary( &grp, NULL, buf,
136 sizeof( buf ) ) );
137 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
138 mbedtls_ecp_point_read_binary( &grp, &P, NULL,
139 sizeof( buf ) ) );
140
141 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
142 mbedtls_ecp_tls_read_point( NULL, &P,
143 (const unsigned char **) &buf,
144 sizeof( buf ) ) );
145 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
146 mbedtls_ecp_tls_read_point( &grp, NULL,
147 (const unsigned char **) &buf,
148 sizeof( buf ) ) );
149 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
150 mbedtls_ecp_tls_read_point( &grp, &P, &null_buf,
151 sizeof( buf ) ) );
152 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
153 mbedtls_ecp_tls_read_point( &grp, &P, NULL,
154 sizeof( buf ) ) );
155
156 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
157 mbedtls_ecp_tls_write_point( NULL, &P,
158 valid_fmt,
159 &olen,
160 buf,
161 sizeof( buf ) ) );
162 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
163 mbedtls_ecp_tls_write_point( &grp, NULL,
164 valid_fmt,
165 &olen,
166 buf,
167 sizeof( buf ) ) );
168 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
169 mbedtls_ecp_tls_write_point( &grp, &P,
170 invalid_fmt,
171 &olen,
172 buf,
173 sizeof( buf ) ) );
174 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
175 mbedtls_ecp_tls_write_point( &grp, &P,
176 valid_fmt,
177 NULL,
178 buf,
179 sizeof( buf ) ) );
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
181 mbedtls_ecp_tls_write_point( &grp, &P,
182 valid_fmt,
183 &olen,
184 NULL,
185 sizeof( buf ) ) );
186
187 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
188 mbedtls_ecp_group_load( NULL, valid_group ) );
189
190 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
191 mbedtls_ecp_tls_read_group( NULL,
192 (const unsigned char **) &buf,
193 sizeof( buf ) ) );
194 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
195 mbedtls_ecp_tls_read_group( &grp, NULL,
196 sizeof( buf ) ) );
197 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
198 mbedtls_ecp_tls_read_group( &grp, &null_buf,
199 sizeof( buf ) ) );
200
201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
202 mbedtls_ecp_tls_read_group_id( NULL,
203 (const unsigned char **) &buf,
204 sizeof( buf ) ) );
205 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
206 mbedtls_ecp_tls_read_group_id( &valid_group, NULL,
207 sizeof( buf ) ) );
208 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
209 mbedtls_ecp_tls_read_group_id( &valid_group,
210 &null_buf,
211 sizeof( buf ) ) );
212
213 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
214 mbedtls_ecp_tls_write_group( NULL, &olen,
215 buf, sizeof( buf ) ) );
216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
217 mbedtls_ecp_tls_write_group( &grp, NULL,
218 buf, sizeof( buf ) ) );
219 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
220 mbedtls_ecp_tls_write_group( &grp, &olen,
221 NULL, sizeof( buf ) ) );
222
223 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
224 mbedtls_ecp_mul( NULL, &P, &m, &P,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200225 mbedtls_test_rnd_std_rand,
226 NULL ) );
Hanno Becker12dff032018-12-14 15:08:13 +0000227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
228 mbedtls_ecp_mul( &grp, NULL, &m, &P,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200229 mbedtls_test_rnd_std_rand,
230 NULL ) );
Hanno Becker12dff032018-12-14 15:08:13 +0000231 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
232 mbedtls_ecp_mul( &grp, &P, NULL, &P,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200233 mbedtls_test_rnd_std_rand,
234 NULL ) );
Hanno Becker12dff032018-12-14 15:08:13 +0000235 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
236 mbedtls_ecp_mul( &grp, &P, &m, NULL,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200237 mbedtls_test_rnd_std_rand,
238 NULL ) );
Hanno Becker12dff032018-12-14 15:08:13 +0000239
240 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
241 mbedtls_ecp_mul_restartable( NULL, &P, &m, &P,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200242 mbedtls_test_rnd_std_rand,
243 NULL , NULL ) );
Hanno Becker12dff032018-12-14 15:08:13 +0000244 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
245 mbedtls_ecp_mul_restartable( &grp, NULL, &m, &P,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200246 mbedtls_test_rnd_std_rand,
247 NULL , NULL ) );
Hanno Becker12dff032018-12-14 15:08:13 +0000248 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
249 mbedtls_ecp_mul_restartable( &grp, &P, NULL, &P,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200250 mbedtls_test_rnd_std_rand,
251 NULL , NULL ) );
Hanno Becker12dff032018-12-14 15:08:13 +0000252 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
253 mbedtls_ecp_mul_restartable( &grp, &P, &m, NULL,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200254 mbedtls_test_rnd_std_rand,
255 NULL , NULL ) );
Hanno Becker12dff032018-12-14 15:08:13 +0000256
257 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
258 mbedtls_ecp_muladd( NULL, &P, &m, &P,
259 &m, &P ) );
260 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
261 mbedtls_ecp_muladd( &grp, NULL, &m, &P,
262 &m, &P ) );
263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
264 mbedtls_ecp_muladd( &grp, &P, NULL, &P,
265 &m, &P ) );
266 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
267 mbedtls_ecp_muladd( &grp, &P, &m, NULL,
268 &m, &P ) );
269 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
270 mbedtls_ecp_muladd( &grp, &P, &m, &P,
271 NULL, &P ) );
272 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
273 mbedtls_ecp_muladd( &grp, &P, &m, &P,
274 &m, NULL ) );
275
276 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
277 mbedtls_ecp_muladd_restartable( NULL, &P, &m, &P,
278 &m, &P, NULL ) );
279 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
280 mbedtls_ecp_muladd_restartable( &grp, NULL, &m, &P,
281 &m, &P, NULL ) );
282 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
283 mbedtls_ecp_muladd_restartable( &grp, &P, NULL, &P,
284 &m, &P, NULL ) );
285 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
286 mbedtls_ecp_muladd_restartable( &grp, &P, &m, NULL,
287 &m, &P, NULL ) );
288 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
289 mbedtls_ecp_muladd_restartable( &grp, &P, &m, &P,
290 NULL, &P, NULL ) );
291 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
292 mbedtls_ecp_muladd_restartable( &grp, &P, &m, &P,
293 &m, NULL, NULL ) );
294
295 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
296 mbedtls_ecp_check_pubkey( NULL, &P ) );
297 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
298 mbedtls_ecp_check_pubkey( &grp, NULL ) );
299
300 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
Hanno Becker19595352018-12-18 23:54:04 +0000301 mbedtls_ecp_check_pub_priv( NULL, &kp ) );
302 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
303 mbedtls_ecp_check_pub_priv( &kp, NULL ) );
304
305 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
Hanno Becker12dff032018-12-14 15:08:13 +0000306 mbedtls_ecp_check_privkey( NULL, &m ) );
307 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
308 mbedtls_ecp_check_privkey( &grp, NULL ) );
309
310 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200311 mbedtls_ecp_gen_keypair_base( NULL, &P, &m, &P,
312 mbedtls_test_rnd_std_rand, NULL ) );
313
Hanno Becker12dff032018-12-14 15:08:13 +0000314 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200315 mbedtls_ecp_gen_keypair_base( &grp, NULL, &m, &P,
316 mbedtls_test_rnd_std_rand, NULL ) );
317
Hanno Becker12dff032018-12-14 15:08:13 +0000318 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200319 mbedtls_ecp_gen_keypair_base( &grp, &P, NULL, &P,
320 mbedtls_test_rnd_std_rand, NULL ) );
321
Hanno Becker12dff032018-12-14 15:08:13 +0000322 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200323 mbedtls_ecp_gen_keypair_base( &grp, &P, &m, NULL,
324 mbedtls_test_rnd_std_rand, NULL ) );
325
Hanno Becker12dff032018-12-14 15:08:13 +0000326 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200327 mbedtls_ecp_gen_keypair_base( &grp, &P, &m, &P, NULL, NULL ) );
Hanno Becker12dff032018-12-14 15:08:13 +0000328
329 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
330 mbedtls_ecp_gen_keypair( NULL,
331 &m, &P,
Ronald Cron351f0ee2020-06-10 12:12:18 +0200332 mbedtls_test_rnd_std_rand,
Hanno Becker12dff032018-12-14 15:08:13 +0000333 NULL ) );
334 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
335 mbedtls_ecp_gen_keypair( &grp,
336 NULL, &P,
Ronald Cron351f0ee2020-06-10 12:12:18 +0200337 mbedtls_test_rnd_std_rand,
Hanno Becker12dff032018-12-14 15:08:13 +0000338 NULL ) );
339 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
340 mbedtls_ecp_gen_keypair( &grp,
341 &m, NULL,
Ronald Cron351f0ee2020-06-10 12:12:18 +0200342 mbedtls_test_rnd_std_rand,
Hanno Becker12dff032018-12-14 15:08:13 +0000343 NULL ) );
344 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
345 mbedtls_ecp_gen_keypair( &grp,
346 &m, &P,
347 NULL,
348 NULL ) );
349
350 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
351 mbedtls_ecp_gen_key( valid_group, NULL,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200352 mbedtls_test_rnd_std_rand,
353 NULL ) );
Hanno Becker12dff032018-12-14 15:08:13 +0000354 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
355 mbedtls_ecp_gen_key( valid_group, &kp,
356 NULL, NULL ) );
357
358exit:
359 return;
360}
361/* END_CASE */
362
Paul Bakker33b43f12013-08-20 11:48:36 +0200363/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100364void mbedtls_ecp_curve_info( int id, int tls_id, int size, char * name )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 const mbedtls_ecp_curve_info *by_id, *by_tls, *by_name;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 by_id = mbedtls_ecp_curve_info_from_grp_id( id );
369 by_tls = mbedtls_ecp_curve_info_from_tls_id( tls_id );
370 by_name = mbedtls_ecp_curve_info_from_name( name );
Paul Bakker94b916c2014-04-17 16:07:20 +0200371 TEST_ASSERT( by_id != NULL );
372 TEST_ASSERT( by_tls != NULL );
373 TEST_ASSERT( by_name != NULL );
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100374
375 TEST_ASSERT( by_id == by_tls );
376 TEST_ASSERT( by_id == by_name );
377
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200378 TEST_ASSERT( by_id->bit_size == size );
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100379}
380/* END_CASE */
381
382/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100383void ecp_check_pub( int grp_id, char * x_hex, char * y_hex, char * z_hex,
384 int ret )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_ecp_group grp;
387 mbedtls_ecp_point P;
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_ecp_group_init( &grp );
390 mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100391
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200392 TEST_ASSERT( mbedtls_ecp_group_load( &grp, grp_id ) == 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100393
Janos Follath28fff142017-01-27 15:51:14 +0000394 TEST_ASSERT( mbedtls_mpi_read_string( &P.X, 16, x_hex ) == 0 );
395 TEST_ASSERT( mbedtls_mpi_read_string( &P.Y, 16, y_hex ) == 0 );
396 TEST_ASSERT( mbedtls_mpi_read_string( &P.Z, 16, z_hex ) == 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &P ) == ret );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100399
Paul Bakkerbd51b262014-07-10 15:26:12 +0200400exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_ecp_group_free( &grp );
402 mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100403}
404/* END_CASE */
405
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200406/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100407void ecp_test_vect_restart( int id,
408 char *dA_str, char *xA_str, char *yA_str,
409 char *dB_str, char *xZ_str, char *yZ_str,
410 int max_ops, int min_restarts, int max_restarts )
411{
412 /*
413 * Test for early restart. Based on test vectors like ecp_test_vect(),
414 * but for the sake of simplicity only does half of each side. It's
415 * important to test both base point and random point, though, as memory
416 * management is different in each case.
417 *
418 * Don't try using too precise bounds for restarts as the exact number
419 * will depend on settings such as MBEDTLS_ECP_FIXED_POINT_OPTIM and
420 * MBEDTLS_ECP_WINDOW_SIZE, as well as implementation details that may
421 * change in the future. A factor 2 is a minimum safety margin.
422 *
423 * For reference, with mbed TLS 2.4 and default settings, for P-256:
Manuel Pégourié-Gonnard9c5c78f2017-03-20 14:13:07 +0100424 * - Random point mult: ~3250M
425 * - Cold base point mult: ~3300M
426 * - Hot base point mult: ~1100M
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100427 * With MBEDTLS_ECP_WINDOW_SIZE set to 2 (minimum):
Manuel Pégourié-Gonnard9c5c78f2017-03-20 14:13:07 +0100428 * - Random point mult: ~3850M
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100429 */
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200430 mbedtls_ecp_restart_ctx ctx;
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100431 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnard7a28e992018-10-16 11:22:45 +0200432 mbedtls_ecp_point R, P;
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100433 mbedtls_mpi dA, xA, yA, dB, xZ, yZ;
434 int cnt_restarts;
435 int ret;
436
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200437 mbedtls_ecp_restart_init( &ctx );
Manuel Pégourié-Gonnard7a28e992018-10-16 11:22:45 +0200438 mbedtls_ecp_group_init( &grp );
439 mbedtls_ecp_point_init( &R ); mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100440 mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA ); mbedtls_mpi_init( &yA );
441 mbedtls_mpi_init( &dB ); mbedtls_mpi_init( &xZ ); mbedtls_mpi_init( &yZ );
442
443 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
444
445 TEST_ASSERT( mbedtls_mpi_read_string( &dA, 16, dA_str ) == 0 );
446 TEST_ASSERT( mbedtls_mpi_read_string( &xA, 16, xA_str ) == 0 );
447 TEST_ASSERT( mbedtls_mpi_read_string( &yA, 16, yA_str ) == 0 );
448
449 TEST_ASSERT( mbedtls_mpi_read_string( &dB, 16, dB_str ) == 0 );
450 TEST_ASSERT( mbedtls_mpi_read_string( &xZ, 16, xZ_str ) == 0 );
451 TEST_ASSERT( mbedtls_mpi_read_string( &yZ, 16, yZ_str ) == 0 );
452
453 mbedtls_ecp_set_max_ops( (unsigned) max_ops );
454
455 /* Base point case */
456 cnt_restarts = 0;
457 do {
Manuel Pégourié-Gonnard7a28e992018-10-16 11:22:45 +0200458 ECP_PT_RESET( &R );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200459 ret = mbedtls_ecp_mul_restartable( &grp, &R, &dA, &grp.G, NULL, NULL, &ctx );
Manuel Pégourié-Gonnard46ba7f32017-08-28 12:20:39 +0200460 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100461
Manuel Pégourié-Gonnard46ba7f32017-08-28 12:20:39 +0200462 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100463 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 );
464 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yA ) == 0 );
465
466 TEST_ASSERT( cnt_restarts >= min_restarts );
467 TEST_ASSERT( cnt_restarts <= max_restarts );
468
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100469 /* Non-base point case */
Manuel Pégourié-Gonnard7a28e992018-10-16 11:22:45 +0200470 mbedtls_ecp_copy( &P, &R );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100471 cnt_restarts = 0;
472 do {
Manuel Pégourié-Gonnard7a28e992018-10-16 11:22:45 +0200473 ECP_PT_RESET( &R );
474 ret = mbedtls_ecp_mul_restartable( &grp, &R, &dB, &P, NULL, NULL, &ctx );
Manuel Pégourié-Gonnard46ba7f32017-08-28 12:20:39 +0200475 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100476
Manuel Pégourié-Gonnard46ba7f32017-08-28 12:20:39 +0200477 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100478 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 );
479 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
480
481 TEST_ASSERT( cnt_restarts >= min_restarts );
482 TEST_ASSERT( cnt_restarts <= max_restarts );
483
Manuel Pégourié-Gonnard46ba7f32017-08-28 12:20:39 +0200484 /* Do we leak memory when aborting an operation?
485 * This test only makes sense when we actually restart */
486 if( min_restarts > 0 )
487 {
Manuel Pégourié-Gonnard7a28e992018-10-16 11:22:45 +0200488 ret = mbedtls_ecp_mul_restartable( &grp, &R, &dB, &P, NULL, NULL, &ctx );
Manuel Pégourié-Gonnard46ba7f32017-08-28 12:20:39 +0200489 TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
490 }
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100491
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100492exit:
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200493 mbedtls_ecp_restart_free( &ctx );
Manuel Pégourié-Gonnard7a28e992018-10-16 11:22:45 +0200494 mbedtls_ecp_group_free( &grp );
495 mbedtls_ecp_point_free( &R ); mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100496 mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA ); mbedtls_mpi_free( &yA );
497 mbedtls_mpi_free( &dB ); mbedtls_mpi_free( &xZ ); mbedtls_mpi_free( &yZ );
498}
499/* END_CASE */
500
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200501/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200502void ecp_muladd_restart( int id, char *xR_str, char *yR_str,
503 char *u1_str, char *u2_str,
504 char *xQ_str, char *yQ_str,
505 int max_ops, int min_restarts, int max_restarts )
506{
507 /*
508 * Compute R = u1 * G + u2 * Q
509 * (test vectors mostly taken from ECDSA intermediate results)
510 *
511 * See comments at the top of ecp_test_vect_restart()
512 */
513 mbedtls_ecp_restart_ctx ctx;
514 mbedtls_ecp_group grp;
515 mbedtls_ecp_point R, Q;
516 mbedtls_mpi u1, u2, xR, yR;
517 int cnt_restarts;
518 int ret;
519
520 mbedtls_ecp_restart_init( &ctx );
521 mbedtls_ecp_group_init( &grp );
522 mbedtls_ecp_point_init( &R );
523 mbedtls_ecp_point_init( &Q );
524 mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
525 mbedtls_mpi_init( &xR ); mbedtls_mpi_init( &yR );
526
527 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
528
529 TEST_ASSERT( mbedtls_mpi_read_string( &u1, 16, u1_str ) == 0 );
530 TEST_ASSERT( mbedtls_mpi_read_string( &u2, 16, u2_str ) == 0 );
531 TEST_ASSERT( mbedtls_mpi_read_string( &xR, 16, xR_str ) == 0 );
532 TEST_ASSERT( mbedtls_mpi_read_string( &yR, 16, yR_str ) == 0 );
533
534 TEST_ASSERT( mbedtls_mpi_read_string( &Q.X, 16, xQ_str ) == 0 );
535 TEST_ASSERT( mbedtls_mpi_read_string( &Q.Y, 16, yQ_str ) == 0 );
536 TEST_ASSERT( mbedtls_mpi_lset( &Q.Z, 1 ) == 0 );
537
538 mbedtls_ecp_set_max_ops( (unsigned) max_ops );
539
540 cnt_restarts = 0;
541 do {
Manuel Pégourié-Gonnard7a28e992018-10-16 11:22:45 +0200542 ECP_PT_RESET( &R );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200543 ret = mbedtls_ecp_muladd_restartable( &grp, &R,
544 &u1, &grp.G, &u2, &Q, &ctx );
Manuel Pégourié-Gonnard46ba7f32017-08-28 12:20:39 +0200545 } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200546
Manuel Pégourié-Gonnard46ba7f32017-08-28 12:20:39 +0200547 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200548 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xR ) == 0 );
549 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yR ) == 0 );
550
551 TEST_ASSERT( cnt_restarts >= min_restarts );
552 TEST_ASSERT( cnt_restarts <= max_restarts );
553
Manuel Pégourié-Gonnard46ba7f32017-08-28 12:20:39 +0200554 /* Do we leak memory when aborting an operation?
555 * This test only makes sense when we actually restart */
556 if( min_restarts > 0 )
557 {
558 ret = mbedtls_ecp_muladd_restartable( &grp, &R,
559 &u1, &grp.G, &u2, &Q, &ctx );
560 TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
561 }
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200562
563exit:
564 mbedtls_ecp_restart_free( &ctx );
565 mbedtls_ecp_group_free( &grp );
566 mbedtls_ecp_point_free( &R );
567 mbedtls_ecp_point_free( &Q );
568 mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
569 mbedtls_mpi_free( &xR ); mbedtls_mpi_free( &yR );
570}
571/* END_CASE */
572
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100573/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100574void ecp_test_vect( int id, char * dA_str, char * xA_str, char * yA_str,
575 char * dB_str, char * xB_str, char * yB_str,
576 char * xZ_str, char * yZ_str )
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100577{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_ecp_group grp;
579 mbedtls_ecp_point R;
580 mbedtls_mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200581 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R );
584 mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA ); mbedtls_mpi_init( &yA ); mbedtls_mpi_init( &dB );
585 mbedtls_mpi_init( &xB ); mbedtls_mpi_init( &yB ); mbedtls_mpi_init( &xZ ); mbedtls_mpi_init( &yZ );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200586 memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100587
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200588 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 TEST_ASSERT( mbedtls_mpi_read_string( &dA, 16, dA_str ) == 0 );
593 TEST_ASSERT( mbedtls_mpi_read_string( &xA, 16, xA_str ) == 0 );
594 TEST_ASSERT( mbedtls_mpi_read_string( &yA, 16, yA_str ) == 0 );
595 TEST_ASSERT( mbedtls_mpi_read_string( &dB, 16, dB_str ) == 0 );
596 TEST_ASSERT( mbedtls_mpi_read_string( &xB, 16, xB_str ) == 0 );
597 TEST_ASSERT( mbedtls_mpi_read_string( &yB, 16, yB_str ) == 0 );
598 TEST_ASSERT( mbedtls_mpi_read_string( &xZ, 16, xZ_str ) == 0 );
599 TEST_ASSERT( mbedtls_mpi_read_string( &yZ, 16, yZ_str ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &grp.G,
Ronald Cron351f0ee2020-06-10 12:12:18 +0200602 &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 );
604 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yA ) == 0 );
605 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
606 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &R, NULL, NULL ) == 0 );
607 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 );
608 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
609 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
612 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xB ) == 0 );
613 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yB ) == 0 );
614 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
615 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &R,
Ronald Cron351f0ee2020-06-10 12:12:18 +0200616 &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 );
618 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
619 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
Manuel Pégourié-Gonnarde739f012012-11-07 12:24:22 +0100620
Paul Bakkerbd51b262014-07-10 15:26:12 +0200621exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R );
623 mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA ); mbedtls_mpi_free( &yA ); mbedtls_mpi_free( &dB );
624 mbedtls_mpi_free( &xB ); mbedtls_mpi_free( &yB ); mbedtls_mpi_free( &xZ ); mbedtls_mpi_free( &yZ );
Manuel Pégourié-Gonnard4b8c3f22012-11-07 21:39:45 +0100625}
Paul Bakker33b43f12013-08-20 11:48:36 +0200626/* END_CASE */
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100627
Paul Bakker33b43f12013-08-20 11:48:36 +0200628/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100629void ecp_test_vec_x( int id, char * dA_hex, char * xA_hex, char * dB_hex,
630 char * xB_hex, char * xS_hex )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100631{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_ecp_group grp;
633 mbedtls_ecp_point R;
634 mbedtls_mpi dA, xA, dB, xB, xS;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200635 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R );
638 mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA );
639 mbedtls_mpi_init( &dB ); mbedtls_mpi_init( &xB );
640 mbedtls_mpi_init( &xS );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200641 memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100642
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200643 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 TEST_ASSERT( mbedtls_mpi_read_string( &dA, 16, dA_hex ) == 0 );
648 TEST_ASSERT( mbedtls_mpi_read_string( &dB, 16, dB_hex ) == 0 );
649 TEST_ASSERT( mbedtls_mpi_read_string( &xA, 16, xA_hex ) == 0 );
650 TEST_ASSERT( mbedtls_mpi_read_string( &xB, 16, xB_hex ) == 0 );
651 TEST_ASSERT( mbedtls_mpi_read_string( &xS, 16, xS_hex ) == 0 );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &grp.G,
Ronald Cron351f0ee2020-06-10 12:12:18 +0200654 &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
656 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &R,
Ronald Cron351f0ee2020-06-10 12:12:18 +0200659 &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
661 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xS ) == 0 );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
664 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
665 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xB ) == 0 );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &R, NULL, NULL ) == 0 );
668 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
669 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xS ) == 0 );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100670
Paul Bakkerbd51b262014-07-10 15:26:12 +0200671exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R );
673 mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA );
674 mbedtls_mpi_free( &dB ); mbedtls_mpi_free( &xB );
675 mbedtls_mpi_free( &xS );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +0100676}
677/* END_CASE */
678
679/* BEGIN_CASE */
Janos Follath182b0b92019-04-26 14:28:19 +0100680void ecp_test_mul( int id, data_t * n_hex,
681 data_t * Px_hex, data_t * Py_hex, data_t * Pz_hex,
682 data_t * nPx_hex, data_t * nPy_hex, data_t * nPz_hex,
683 int expected_ret )
684{
685 mbedtls_ecp_group grp;
686 mbedtls_ecp_point P, nP, R;
687 mbedtls_mpi n;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200688 mbedtls_test_rnd_pseudo_info rnd_info;
Janos Follath182b0b92019-04-26 14:28:19 +0100689
690 mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R );
691 mbedtls_ecp_point_init( &P ); mbedtls_ecp_point_init( &nP );
692 mbedtls_mpi_init( &n );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200693 memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
Janos Follath182b0b92019-04-26 14:28:19 +0100694
695 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
696
697 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
698
699 TEST_ASSERT( mbedtls_mpi_read_binary( &n, n_hex->x, n_hex->len ) == 0 );
700
701 TEST_ASSERT( mbedtls_mpi_read_binary( &P.X, Px_hex->x, Px_hex->len ) == 0 );
702 TEST_ASSERT( mbedtls_mpi_read_binary( &P.Y, Py_hex->x, Py_hex->len ) == 0 );
703 TEST_ASSERT( mbedtls_mpi_read_binary( &P.Z, Pz_hex->x, Pz_hex->len ) == 0 );
704 TEST_ASSERT( mbedtls_mpi_read_binary( &nP.X, nPx_hex->x, nPx_hex->len )
705 == 0 );
706 TEST_ASSERT( mbedtls_mpi_read_binary( &nP.Y, nPy_hex->x, nPy_hex->len )
707 == 0 );
708 TEST_ASSERT( mbedtls_mpi_read_binary( &nP.Z, nPz_hex->x, nPz_hex->len )
709 == 0 );
710
711 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &n, &P,
Ronald Cron351f0ee2020-06-10 12:12:18 +0200712 &mbedtls_test_rnd_pseudo_rand, &rnd_info )
Janos Follath182b0b92019-04-26 14:28:19 +0100713 == expected_ret );
714
715 if( expected_ret == 0 )
716 {
717 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &nP.X, &R.X ) == 0 );
718 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &nP.Y, &R.Y ) == 0 );
719 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &nP.Z, &R.Z ) == 0 );
720 }
721
722exit:
723 mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R );
724 mbedtls_ecp_point_free( &P ); mbedtls_ecp_point_free( &nP );
725 mbedtls_mpi_free( &n );
726}
727/* END_CASE */
728
729/* BEGIN_CASE */
Jonas923d5792020-05-13 14:22:45 +0900730void ecp_test_mul_rng( int id, data_t * d_hex)
731{
732 mbedtls_ecp_group grp;
733 mbedtls_mpi d;
734 mbedtls_ecp_point Q;
735
736 mbedtls_ecp_group_init( &grp ); mbedtls_mpi_init( &d );
737 mbedtls_ecp_point_init( &Q );
738
739 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
740
741 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
742
743 TEST_ASSERT( mbedtls_mpi_read_binary( &d, d_hex->x, d_hex->len ) == 0 );
744
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200745 TEST_ASSERT( mbedtls_ecp_mul( &grp, &Q, &d, &grp.G,
746 &mbedtls_test_rnd_zero_rand, NULL )
Jonas923d5792020-05-13 14:22:45 +0900747 == MBEDTLS_ERR_ECP_RANDOM_FAILED );
748
749exit:
750 mbedtls_ecp_group_free( &grp ); mbedtls_mpi_free( &d );
751 mbedtls_ecp_point_free( &Q );
752}
753/* END_CASE */
754
755/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100756void ecp_fast_mod( int id, char * N_str )
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100757{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_ecp_group grp;
759 mbedtls_mpi N, R;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &R );
762 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 TEST_ASSERT( mbedtls_mpi_read_string( &N, 16, N_str ) == 0 );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200765 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200766 TEST_ASSERT( grp.modp != NULL );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100767
768 /*
769 * Store correct result before we touch N
770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &N, &grp.P ) == 0 );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100772
773 TEST_ASSERT( grp.modp( &N ) == 0 );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200774 TEST_ASSERT( mbedtls_mpi_bitlen( &N ) <= grp.pbits + 3 );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100775
776 /*
Paul Bakkerd8b0c5e2014-04-11 15:31:33 +0200777 * Use mod rather than addition/subtraction in case previous test fails
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 TEST_ASSERT( mbedtls_mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
780 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &R ) == 0 );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100781
Paul Bakkerbd51b262014-07-10 15:26:12 +0200782exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &R );
784 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100785}
Paul Bakker33b43f12013-08-20 11:48:36 +0200786/* END_CASE */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100787
Paul Bakker33b43f12013-08-20 11:48:36 +0200788/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100789void ecp_write_binary( int id, char * x, char * y, char * z, int format,
Azim Khan5fcca462018-06-29 11:05:32 +0100790 data_t * out, int blen, int ret )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100791{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_ecp_group grp;
793 mbedtls_ecp_point P;
Azim Khanf1aaec92017-05-30 14:23:15 +0100794 unsigned char buf[256];
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100795 size_t olen;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100796
797 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100800
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200801 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 TEST_ASSERT( mbedtls_mpi_read_string( &P.X, 16, x ) == 0 );
804 TEST_ASSERT( mbedtls_mpi_read_string( &P.Y, 16, y ) == 0 );
805 TEST_ASSERT( mbedtls_mpi_read_string( &P.Z, 16, z ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 TEST_ASSERT( mbedtls_ecp_point_write_binary( &grp, &P, format,
Paul Bakker33b43f12013-08-20 11:48:36 +0200808 &olen, buf, blen ) == ret );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100809
Paul Bakker33b43f12013-08-20 11:48:36 +0200810 if( ret == 0 )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100811 {
Ronald Cronde70b162020-06-10 11:03:08 +0200812 TEST_ASSERT( mbedtls_test_hexcmp( buf, out->x, olen, out->len ) == 0 );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100813 }
814
Paul Bakkerbd51b262014-07-10 15:26:12 +0200815exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100817}
Paul Bakker33b43f12013-08-20 11:48:36 +0200818/* END_CASE */
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100819
Paul Bakker33b43f12013-08-20 11:48:36 +0200820/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100821void ecp_read_binary( int id, data_t * buf, char * x, char * y, char * z,
Paul Bakker33b43f12013-08-20 11:48:36 +0200822 int ret )
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100823{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_ecp_group grp;
825 mbedtls_ecp_point P;
826 mbedtls_mpi X, Y, Z;
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100827
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P );
830 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100831
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200832 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x ) == 0 );
835 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y ) == 0 );
836 TEST_ASSERT( mbedtls_mpi_read_string( &Z, 16, z ) == 0 );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100837
Azim Khand30ca132017-06-09 04:32:58 +0100838 TEST_ASSERT( mbedtls_ecp_point_read_binary( &grp, &P, buf->x, buf->len ) == ret );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100839
Paul Bakker33b43f12013-08-20 11:48:36 +0200840 if( ret == 0 )
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.X, &X ) == 0 );
Janos Follathdf9295b2019-02-26 12:36:52 +0000843 if( mbedtls_ecp_get_type( &grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
Janos Follath59b813c2019-02-13 10:44:06 +0000844 {
845 TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, 0 ) == 0 );
846 TEST_ASSERT( P.Y.p == NULL );
847 TEST_ASSERT( mbedtls_mpi_cmp_int( &Z, 1 ) == 0 );
848 TEST_ASSERT( mbedtls_mpi_cmp_int( &P.Z, 1 ) == 0 );
849 }
850 else
851 {
852 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Y, &Y ) == 0 );
853 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Z, &Z ) == 0 );
854 }
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100855 }
856
Paul Bakkerbd51b262014-07-10 15:26:12 +0200857exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P );
859 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100860}
Paul Bakker33b43f12013-08-20 11:48:36 +0200861/* END_CASE */
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100862
Paul Bakker33b43f12013-08-20 11:48:36 +0200863/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100864void mbedtls_ecp_tls_read_point( int id, data_t * buf, char * x, char * y,
Azim Khand30ca132017-06-09 04:32:58 +0100865 char * z, int ret )
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100866{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_ecp_group grp;
868 mbedtls_ecp_point P;
869 mbedtls_mpi X, Y, Z;
Azim Khand30ca132017-06-09 04:32:58 +0100870 const unsigned char *vbuf = buf->x;
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100871
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P );
874 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100875
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200876 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x ) == 0 );
879 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y ) == 0 );
880 TEST_ASSERT( mbedtls_mpi_read_string( &Z, 16, z ) == 0 );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100881
Azim Khand30ca132017-06-09 04:32:58 +0100882 TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &P, &vbuf, buf->len ) == ret );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100883
Paul Bakker33b43f12013-08-20 11:48:36 +0200884 if( ret == 0 )
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.X, &X ) == 0 );
887 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Y, &Y ) == 0 );
888 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Z, &Z ) == 0 );
Azim Khand30ca132017-06-09 04:32:58 +0100889 TEST_ASSERT( (uint32_t)( vbuf - buf->x ) == buf->len );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100890 }
891
Paul Bakkerbd51b262014-07-10 15:26:12 +0200892exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P );
894 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100895}
Paul Bakker33b43f12013-08-20 11:48:36 +0200896/* END_CASE */
Manuel Pégourié-Gonnard8c16f962013-02-10 13:00:20 +0100897
Paul Bakker33b43f12013-08-20 11:48:36 +0200898/* BEGIN_CASE */
899void ecp_tls_write_read_point( int id )
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100900{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_ecp_group grp;
902 mbedtls_ecp_point pt;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100903 unsigned char buf[256];
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100904 const unsigned char *vbuf;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100905 size_t olen;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_ecp_group_init( &grp );
908 mbedtls_ecp_point_init( &pt );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100909
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200910 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100911
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100912 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &grp.G,
914 MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
915 TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen )
916 == MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100917 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100918
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100919 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &grp.G,
921 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
922 TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
923 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 );
924 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 );
925 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100926 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100927
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100928 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 TEST_ASSERT( mbedtls_ecp_set_zero( &pt ) == 0 );
930 TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &pt,
931 MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
932 TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
933 TEST_ASSERT( mbedtls_ecp_is_zero( &pt ) );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100934 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100935
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100936 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 TEST_ASSERT( mbedtls_ecp_set_zero( &pt ) == 0 );
938 TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &pt,
939 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
940 TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
941 TEST_ASSERT( mbedtls_ecp_is_zero( &pt ) );
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100942 TEST_ASSERT( vbuf == buf + olen );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100943
Paul Bakkerbd51b262014-07-10 15:26:12 +0200944exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 mbedtls_ecp_group_free( &grp );
946 mbedtls_ecp_point_free( &pt );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100947}
Paul Bakker33b43f12013-08-20 11:48:36 +0200948/* END_CASE */
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100949
Paul Bakker33b43f12013-08-20 11:48:36 +0200950/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100951void mbedtls_ecp_tls_read_group( data_t * buf, int result, int bits,
Azim Khand30ca132017-06-09 04:32:58 +0100952 int record_len )
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100953{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 mbedtls_ecp_group grp;
Azim Khand30ca132017-06-09 04:32:58 +0100955 const unsigned char *vbuf = buf->x;
Azim Khanf1aaec92017-05-30 14:23:15 +0100956 int ret;
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100959
Azim Khand30ca132017-06-09 04:32:58 +0100960 ret = mbedtls_ecp_tls_read_group( &grp, &vbuf, buf->len );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100961
Paul Bakker33b43f12013-08-20 11:48:36 +0200962 TEST_ASSERT( ret == result );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100963 if( ret == 0)
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100964 {
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200965 TEST_ASSERT( mbedtls_mpi_bitlen( &grp.P ) == (size_t) bits );
Azim Khand30ca132017-06-09 04:32:58 +0100966 TEST_ASSERT( vbuf - buf->x == record_len);
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100967 }
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100968
Paul Bakkerbd51b262014-07-10 15:26:12 +0200969exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100971}
Paul Bakker33b43f12013-08-20 11:48:36 +0200972/* END_CASE */
Manuel Pégourié-Gonnard6282aca2013-02-10 11:15:11 +0100973
Paul Bakker33b43f12013-08-20 11:48:36 +0200974/* BEGIN_CASE */
975void ecp_tls_write_read_group( int id )
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100976{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_ecp_group grp1, grp2;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100978 unsigned char buf[10];
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100979 const unsigned char *vbuf = buf;
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100980 size_t len;
981 int ret;
982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_ecp_group_init( &grp1 );
984 mbedtls_ecp_group_init( &grp2 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100985 memset( buf, 0x00, sizeof( buf ) );
986
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200987 TEST_ASSERT( mbedtls_ecp_group_load( &grp1, id ) == 0 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 TEST_ASSERT( mbedtls_ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 );
990 ret = mbedtls_ecp_tls_read_group( &grp2, &vbuf, len );
Paul Bakker94b916c2014-04-17 16:07:20 +0200991 TEST_ASSERT( ret == 0 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100992
993 if( ret == 0 )
994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +0100996 TEST_ASSERT( grp1.id == grp2.id );
997 }
998
Paul Bakkerbd51b262014-07-10 15:26:12 +0200999exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_ecp_group_free( &grp1 );
1001 mbedtls_ecp_group_free( &grp2 );
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +01001002}
Paul Bakker33b43f12013-08-20 11:48:36 +02001003/* END_CASE */
Manuel Pégourié-Gonnard46106a92013-02-10 12:51:17 +01001004
Paul Bakker33b43f12013-08-20 11:48:36 +02001005/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001006void mbedtls_ecp_check_privkey( int id, char * key_hex, int ret )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001007{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_ecp_group grp;
1009 mbedtls_mpi d;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_ecp_group_init( &grp );
1012 mbedtls_mpi_init( &d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001013
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02001014 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 TEST_ASSERT( mbedtls_mpi_read_string( &d, 16, key_hex ) == 0 );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 TEST_ASSERT( mbedtls_ecp_check_privkey( &grp, &d ) == ret );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001018
Paul Bakkerbd51b262014-07-10 15:26:12 +02001019exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 mbedtls_ecp_group_free( &grp );
1021 mbedtls_mpi_free( &d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001022}
Paul Bakker33b43f12013-08-20 11:48:36 +02001023/* END_CASE */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001024
Paul Bakker33b43f12013-08-20 11:48:36 +02001025/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001026void mbedtls_ecp_check_pub_priv( int id_pub, char * Qx_pub, char * Qy_pub,
1027 int id, char * d, char * Qx, char * Qy,
1028 int ret )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_ecp_keypair pub, prv;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_ecp_keypair_init( &pub );
1033 mbedtls_ecp_keypair_init( &prv );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 if( id_pub != MBEDTLS_ECP_DP_NONE )
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02001036 TEST_ASSERT( mbedtls_ecp_group_load( &pub.grp, id_pub ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 TEST_ASSERT( mbedtls_ecp_point_read_string( &pub.Q, 16, Qx_pub, Qy_pub ) == 0 );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 if( id != MBEDTLS_ECP_DP_NONE )
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02001040 TEST_ASSERT( mbedtls_ecp_group_load( &prv.grp, id ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 TEST_ASSERT( mbedtls_ecp_point_read_string( &prv.Q, 16, Qx, Qy ) == 0 );
1042 TEST_ASSERT( mbedtls_mpi_read_string( &prv.d, 16, d ) == 0 );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 TEST_ASSERT( mbedtls_ecp_check_pub_priv( &pub, &prv ) == ret );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001045
1046exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_ecp_keypair_free( &pub );
1048 mbedtls_ecp_keypair_free( &prv );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001049}
1050/* END_CASE */
1051
1052/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053void mbedtls_ecp_gen_keypair( int id )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001054{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 mbedtls_ecp_group grp;
1056 mbedtls_ecp_point Q;
1057 mbedtls_mpi d;
Ronald Cron351f0ee2020-06-10 12:12:18 +02001058 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 mbedtls_ecp_group_init( &grp );
1061 mbedtls_ecp_point_init( &Q );
1062 mbedtls_mpi_init( &d );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001063 memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001064
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02001065 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001066
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001067 TEST_ASSERT( mbedtls_ecp_gen_keypair( &grp, &d, &Q,
1068 &mbedtls_test_rnd_pseudo_rand,
1069 &rnd_info ) == 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &Q ) == 0 );
1072 TEST_ASSERT( mbedtls_ecp_check_privkey( &grp, &d ) == 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001073
Paul Bakkerbd51b262014-07-10 15:26:12 +02001074exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 mbedtls_ecp_group_free( &grp );
1076 mbedtls_ecp_point_free( &Q );
1077 mbedtls_mpi_free( &d );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001078}
Paul Bakker33b43f12013-08-20 11:48:36 +02001079/* END_CASE */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001080
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001081/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082void mbedtls_ecp_gen_key( int id )
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 mbedtls_ecp_keypair key;
Ronald Cron351f0ee2020-06-10 12:12:18 +02001085 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_ecp_keypair_init( &key );
Ronald Cron351f0ee2020-06-10 12:12:18 +02001088 memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001089
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02001090 TEST_ASSERT( mbedtls_ecp_gen_key( id, &key,
1091 &mbedtls_test_rnd_pseudo_rand,
1092 &rnd_info ) == 0 );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 TEST_ASSERT( mbedtls_ecp_check_pubkey( &key.grp, &key.Q ) == 0 );
1095 TEST_ASSERT( mbedtls_ecp_check_privkey( &key.grp, &key.d ) == 0 );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001096
Paul Bakkerbd51b262014-07-10 15:26:12 +02001097exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_ecp_keypair_free( &key );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001099}
1100/* END_CASE */
1101
Janos Follath171a7ef2019-02-15 16:17:45 +00001102/* BEGIN_CASE */
Steven Cooremande8593f2020-06-09 19:55:26 +02001103void mbedtls_ecp_read_key( int grp_id, data_t* in_key, int expected, int canonical )
Janos Follath171a7ef2019-02-15 16:17:45 +00001104{
1105 int ret = 0;
1106 mbedtls_ecp_keypair key;
Steven Cooremande8593f2020-06-09 19:55:26 +02001107 mbedtls_ecp_keypair key2;
Janos Follath171a7ef2019-02-15 16:17:45 +00001108
1109 mbedtls_ecp_keypair_init( &key );
Steven Cooremande8593f2020-06-09 19:55:26 +02001110 mbedtls_ecp_keypair_init( &key2 );
Janos Follath171a7ef2019-02-15 16:17:45 +00001111
1112 ret = mbedtls_ecp_read_key( grp_id, &key, in_key->x, in_key->len );
1113 TEST_ASSERT( ret == expected );
1114
1115 if( expected == 0 )
1116 {
1117 ret = mbedtls_ecp_check_privkey( &key.grp, &key.d );
1118 TEST_ASSERT( ret == 0 );
Steven Cooremande8593f2020-06-09 19:55:26 +02001119
1120 if( canonical )
1121 {
1122 unsigned char buf[MBEDTLS_ECP_MAX_BYTES];
Steven Cooremande8593f2020-06-09 19:55:26 +02001123
Steven Cooreman0024df62020-07-13 10:59:40 +02001124 ret = mbedtls_ecp_write_key( &key, buf, in_key->len );
Steven Cooremande8593f2020-06-09 19:55:26 +02001125 TEST_ASSERT( ret == 0 );
1126
Steven Cooremande8593f2020-06-09 19:55:26 +02001127 ASSERT_COMPARE( in_key->x, in_key->len,
Steven Cooremanc9b7f782020-06-11 17:00:36 +02001128 buf, in_key->len );
Steven Cooremande8593f2020-06-09 19:55:26 +02001129 }
1130 else
1131 {
1132 unsigned char export1[MBEDTLS_ECP_MAX_BYTES];
Steven Cooremande8593f2020-06-09 19:55:26 +02001133 unsigned char export2[MBEDTLS_ECP_MAX_BYTES];
Steven Cooremande8593f2020-06-09 19:55:26 +02001134
Steven Cooreman0024df62020-07-13 10:59:40 +02001135 ret = mbedtls_ecp_write_key( &key, export1, in_key->len );
Steven Cooremande8593f2020-06-09 19:55:26 +02001136 TEST_ASSERT( ret == 0 );
1137
1138 ret = mbedtls_ecp_read_key( grp_id, &key2, export1, in_key->len );
1139 TEST_ASSERT( ret == expected );
1140
Steven Cooreman0024df62020-07-13 10:59:40 +02001141 ret = mbedtls_ecp_write_key( &key2, export2, in_key->len );
Steven Cooremande8593f2020-06-09 19:55:26 +02001142 TEST_ASSERT( ret == 0 );
1143
Steven Cooremanc9b7f782020-06-11 17:00:36 +02001144 ASSERT_COMPARE( export1, in_key->len,
1145 export2, in_key->len );
Steven Cooremande8593f2020-06-09 19:55:26 +02001146 }
Janos Follath171a7ef2019-02-15 16:17:45 +00001147 }
1148
1149exit:
1150 mbedtls_ecp_keypair_free( &key );
Steven Cooremande8593f2020-06-09 19:55:26 +02001151 mbedtls_ecp_keypair_free( &key2 );
Janos Follath171a7ef2019-02-15 16:17:45 +00001152}
1153/* END_CASE */
1154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001156void ecp_selftest( )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001157{
Andres AG93012e82016-09-09 09:10:28 +01001158 TEST_ASSERT( mbedtls_ecp_self_test( 1 ) == 0 );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001159}
Paul Bakker33b43f12013-08-20 11:48:36 +02001160/* END_CASE */