blob: 516b3a08f54d540d19fdf4f19ab5b4431df5f60a [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/rsa.h"
Chris Jones66a4cd42021-03-09 16:04:12 +00003#include "rsa_alt_helpers.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/md2.h"
5#include "mbedtls/md4.h"
6#include "mbedtls/md5.h"
7#include "mbedtls/sha1.h"
8#include "mbedtls/sha256.h"
9#include "mbedtls/sha512.h"
10#include "mbedtls/entropy.h"
11#include "mbedtls/ctr_drbg.h"
Hanno Becker47deec42017-07-24 12:27:09 +010012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +000014
Paul Bakker33b43f12013-08-20 11:48:36 +020015/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +020017 * END_DEPENDENCIES
18 */
Paul Bakker5690efc2011-05-26 13:16:06 +000019
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050020/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
21void rsa_invalid_param( )
22{
23 mbedtls_rsa_context ctx;
24 const int valid_padding = MBEDTLS_RSA_PKCS_V21;
25 const int invalid_padding = 42;
26 const int valid_mode = MBEDTLS_RSA_PRIVATE;
27 const int invalid_mode = 42;
28 unsigned char buf[42] = { 0 };
29 size_t olen;
30
31 TEST_INVALID_PARAM( mbedtls_rsa_init( NULL, valid_padding, 0 ) );
32 TEST_INVALID_PARAM( mbedtls_rsa_init( &ctx, invalid_padding, 0 ) );
33 TEST_VALID_PARAM( mbedtls_rsa_free( NULL ) );
34
35 /* No more variants because only the first argument must be non-NULL. */
36 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
37 mbedtls_rsa_import( NULL, NULL, NULL,
38 NULL, NULL, NULL ) );
39 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
40 mbedtls_rsa_import_raw( NULL,
41 NULL, 0,
42 NULL, 0,
43 NULL, 0,
44 NULL, 0,
45 NULL, 0 ) );
46
47 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
48 mbedtls_rsa_complete( NULL ) );
49
50 /* No more variants because only the first argument must be non-NULL. */
51 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
52 mbedtls_rsa_export( NULL, NULL, NULL,
53 NULL, NULL, NULL ) );
54 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
55 mbedtls_rsa_export_raw( NULL,
56 NULL, 0,
57 NULL, 0,
58 NULL, 0,
59 NULL, 0,
60 NULL, 0 ) );
61 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
62 mbedtls_rsa_export_crt( NULL, NULL, NULL, NULL ) );
63
64 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( NULL,
65 valid_padding, 0 ) );
66 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( &ctx,
67 invalid_padding, 0 ) );
68
69 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +020070 mbedtls_rsa_gen_key( NULL,
71 mbedtls_test_rnd_std_rand,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072 NULL, 0, 0 ) );
73 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
74 mbedtls_rsa_gen_key( &ctx, NULL,
75 NULL, 0, 0 ) );
76
77 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
78 mbedtls_rsa_check_pubkey( NULL ) );
79 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
80 mbedtls_rsa_check_privkey( NULL ) );
81
82 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
83 mbedtls_rsa_check_pub_priv( NULL, &ctx ) );
84 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
85 mbedtls_rsa_check_pub_priv( &ctx, NULL ) );
86
87 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
88 mbedtls_rsa_public( NULL, buf, buf ) );
89 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
90 mbedtls_rsa_public( &ctx, NULL, buf ) );
91 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
92 mbedtls_rsa_public( &ctx, buf, NULL ) );
93
94 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
95 mbedtls_rsa_private( NULL, NULL, NULL,
96 buf, buf ) );
97 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
98 mbedtls_rsa_private( &ctx, NULL, NULL,
99 NULL, buf ) );
100 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
101 mbedtls_rsa_private( &ctx, NULL, NULL,
102 buf, NULL ) );
103
104 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
105 mbedtls_rsa_pkcs1_encrypt( NULL, NULL, NULL,
106 valid_mode,
107 sizeof( buf ), buf,
108 buf ) );
109 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
110 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
111 invalid_mode,
112 sizeof( buf ), buf,
113 buf ) );
114 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
115 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
116 valid_mode,
117 sizeof( buf ), NULL,
118 buf ) );
119 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
120 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
121 valid_mode,
122 sizeof( buf ), buf,
123 NULL ) );
124
125 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
126 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
127 NULL,
128 valid_mode,
129 sizeof( buf ), buf,
130 buf ) );
131 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
132 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
133 NULL,
134 invalid_mode,
135 sizeof( buf ), buf,
136 buf ) );
137 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
138 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
139 NULL,
140 valid_mode,
141 sizeof( buf ), NULL,
142 buf ) );
143 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
144 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
145 NULL,
146 valid_mode,
147 sizeof( buf ), buf,
148 NULL ) );
149
150 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
151 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
152 valid_mode,
153 buf, sizeof( buf ),
154 sizeof( buf ), buf,
155 buf ) );
156 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
157 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
158 invalid_mode,
159 buf, sizeof( buf ),
160 sizeof( buf ), buf,
161 buf ) );
162 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
163 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
164 valid_mode,
165 NULL, sizeof( buf ),
166 sizeof( buf ), buf,
167 buf ) );
168 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
169 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
170 valid_mode,
171 buf, sizeof( buf ),
172 sizeof( buf ), NULL,
173 buf ) );
174 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
175 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
176 valid_mode,
177 buf, sizeof( buf ),
178 sizeof( buf ), buf,
179 NULL ) );
180
181 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
182 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
183 valid_mode, &olen,
184 buf, buf, 42 ) );
185 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
186 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
187 invalid_mode, &olen,
188 buf, buf, 42 ) );
189 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
190 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
191 valid_mode, NULL,
192 buf, buf, 42 ) );
193 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
194 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
195 valid_mode, &olen,
196 NULL, buf, 42 ) );
197 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
198 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
199 valid_mode, &olen,
200 buf, NULL, 42 ) );
201
202 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
203 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
204 NULL,
205 valid_mode, &olen,
206 buf, buf, 42 ) );
207 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
208 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
209 NULL,
210 invalid_mode, &olen,
211 buf, buf, 42 ) );
212 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
213 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
214 NULL,
215 valid_mode, NULL,
216 buf, buf, 42 ) );
217 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
218 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
219 NULL,
220 valid_mode, &olen,
221 NULL, buf, 42 ) );
222 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
223 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
224 NULL,
225 valid_mode, &olen,
226 buf, NULL, 42 ) );
227
228 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
229 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
230 valid_mode,
231 buf, sizeof( buf ),
232 &olen,
233 buf, buf, 42 ) );
234 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
235 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
236 invalid_mode,
237 buf, sizeof( buf ),
238 &olen,
239 buf, buf, 42 ) );
240 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
241 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
242 valid_mode,
243 NULL, sizeof( buf ),
244 NULL,
245 buf, buf, 42 ) );
246 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
247 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
248 valid_mode,
249 buf, sizeof( buf ),
250 &olen,
251 NULL, buf, 42 ) );
252 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
253 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
254 valid_mode,
255 buf, sizeof( buf ),
256 &olen,
257 buf, NULL, 42 ) );
258
259 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
260 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
261 valid_mode,
262 0, sizeof( buf ), buf,
263 buf ) );
264 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
265 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
266 invalid_mode,
267 0, sizeof( buf ), buf,
268 buf ) );
269 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
270 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
271 valid_mode,
272 0, sizeof( buf ), NULL,
273 buf ) );
274 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
275 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
276 valid_mode,
277 0, sizeof( buf ), buf,
278 NULL ) );
279 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
280 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
281 valid_mode,
282 MBEDTLS_MD_SHA1,
283 0, NULL,
284 buf ) );
285
286 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
287 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
288 valid_mode,
289 0, sizeof( buf ), buf,
290 buf ) );
291 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
292 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
293 invalid_mode,
294 0, sizeof( buf ), buf,
295 buf ) );
296 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
297 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
298 valid_mode,
299 0, sizeof( buf ), NULL,
300 buf ) );
301 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
302 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
303 valid_mode,
304 0, sizeof( buf ), buf,
305 NULL ) );
306 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
307 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
308 valid_mode,
309 MBEDTLS_MD_SHA1,
310 0, NULL,
311 buf ) );
312
313 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
314 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
315 valid_mode,
316 0, sizeof( buf ), buf,
317 buf ) );
318 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
319 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
320 invalid_mode,
321 0, sizeof( buf ), buf,
322 buf ) );
323 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
324 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
325 valid_mode,
326 0, sizeof( buf ), NULL,
327 buf ) );
328 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
329 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
330 valid_mode,
331 0, sizeof( buf ), buf,
332 NULL ) );
333 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
334 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
335 valid_mode,
336 MBEDTLS_MD_SHA1,
337 0, NULL,
338 buf ) );
339
340 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Cédric Meutera05cbec2020-04-25 15:02:34 +0200341 mbedtls_rsa_rsassa_pss_sign_ext( NULL, NULL, NULL,
342 0, sizeof( buf ), buf,
343 MBEDTLS_RSA_SALT_LEN_ANY,
344 buf ) );
345 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
346 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
347 0, sizeof( buf ), NULL,
348 MBEDTLS_RSA_SALT_LEN_ANY,
349 buf ) );
350 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
351 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
352 0, sizeof( buf ), buf,
353 MBEDTLS_RSA_SALT_LEN_ANY,
354 NULL ) );
355 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
356 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
357 MBEDTLS_MD_SHA1,
358 0, NULL,
359 MBEDTLS_RSA_SALT_LEN_ANY,
360 buf ) );
361
362 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500363 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
364 valid_mode,
365 0, sizeof( buf ), buf,
366 buf ) );
367 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
368 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
369 invalid_mode,
370 0, sizeof( buf ), buf,
371 buf ) );
372 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
373 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
374 valid_mode,
375 0, sizeof( buf ), NULL,
376 buf ) );
377 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
378 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
379 valid_mode,
380 0, sizeof( buf ), buf,
381 NULL ) );
382 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
383 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
384 valid_mode,
385 MBEDTLS_MD_SHA1, 0, NULL,
386 buf ) );
387
388 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
389 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
390 NULL,
391 valid_mode,
392 0, sizeof( buf ), buf,
393 buf ) );
394 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
395 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
396 NULL,
397 invalid_mode,
398 0, sizeof( buf ), buf,
399 buf ) );
400 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
401 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
402 NULL,
403 valid_mode,
404 0, sizeof( buf ),
405 NULL, buf ) );
406 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
407 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
408 NULL,
409 valid_mode,
410 0, sizeof( buf ), buf,
411 NULL ) );
412 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
413 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
414 NULL,
415 valid_mode,
416 MBEDTLS_MD_SHA1,
417 0, NULL,
418 buf ) );
419
420 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
421 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
422 valid_mode,
423 0, sizeof( buf ),
424 buf, buf ) );
425 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
426 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
427 invalid_mode,
428 0, sizeof( buf ),
429 buf, buf ) );
430 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
431 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
432 valid_mode,
433 0, sizeof( buf ),
434 NULL, buf ) );
435 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
436 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
437 valid_mode,
438 0, sizeof( buf ),
439 buf, NULL ) );
440 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
441 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
442 valid_mode,
443 MBEDTLS_MD_SHA1,
444 0, NULL,
445 buf ) );
446
447 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
448 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
449 valid_mode,
450 0, sizeof( buf ),
451 buf,
452 0, 0,
453 buf ) );
454 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
455 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
456 invalid_mode,
457 0, sizeof( buf ),
458 buf,
459 0, 0,
460 buf ) );
461 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
462 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
463 valid_mode,
464 0, sizeof( buf ),
465 NULL, 0, 0,
466 buf ) );
467 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
468 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
469 valid_mode,
470 0, sizeof( buf ),
471 buf, 0, 0,
472 NULL ) );
473 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
474 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
475 valid_mode,
476 MBEDTLS_MD_SHA1,
477 0, NULL,
478 0, 0,
479 buf ) );
480
481 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
482 mbedtls_rsa_copy( NULL, &ctx ) );
483 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
484 mbedtls_rsa_copy( &ctx, NULL ) );
485
486exit:
487 return;
488}
489/* END_CASE */
490
Paul Bakker33b43f12013-08-20 11:48:36 +0200491/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100492void rsa_init_free( int reinit )
493{
494 mbedtls_rsa_context ctx;
495
496 /* Double free is not explicitly documented to work, but we rely on it
497 * even inside the library so that you can call mbedtls_rsa_free()
498 * unconditionally on an error path without checking whether it has
499 * already been called in the success path. */
500
501 mbedtls_rsa_init( &ctx, 0, 0 );
502 mbedtls_rsa_free( &ctx );
503
504 if( reinit )
505 mbedtls_rsa_init( &ctx, 0, 0 );
506 mbedtls_rsa_free( &ctx );
507
508 /* This test case always succeeds, functionally speaking. A plausible
509 * bug might trigger an invalid pointer dereference or a memory leak. */
510 goto exit;
511}
512/* END_CASE */
513
514/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100515void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100516 int digest, int mod, int radix_P, char * input_P,
517 int radix_Q, char * input_Q, int radix_N,
518 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200519 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000520{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200521 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
522 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100524 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200525 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000526
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100527 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
528 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000530
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200531 memset( hash_result, 0x00, sizeof( hash_result ) );
532 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200533 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000534
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100535 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
536 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
537 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
538 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000539
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100540 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
541 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100542 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000544
Paul Bakker42a29bf2009-07-07 20:18:41 +0000545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100547 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000548
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200549 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
550 &rnd_info, MBEDTLS_RSA_PRIVATE, digest,
551 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200552 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000553 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000554
Ronald Cronac6ae352020-06-26 14:33:03 +0200555 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
556 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000557 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000558
Paul Bakkerbd51b262014-07-10 15:26:12 +0200559exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100560 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
561 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000563}
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000565
Paul Bakker33b43f12013-08-20 11:48:36 +0200566/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100567void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100568 int digest, int mod, int radix_N,
569 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100570 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000571{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200572 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000574
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100575 mbedtls_mpi N, E;
576
577 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200579 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000580
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100581 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
582 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
583 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
584 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000586
Paul Bakker42a29bf2009-07-07 20:18:41 +0000587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100589 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000590
Azim Khand30ca132017-06-09 04:32:58 +0100591 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str->x ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100592
Paul Bakkerbd51b262014-07-10 15:26:12 +0200593exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100594 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000596}
Paul Bakker33b43f12013-08-20 11:48:36 +0200597/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000598
Paul Bakker821fb082009-07-12 13:26:42 +0000599
Paul Bakker33b43f12013-08-20 11:48:36 +0200600/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100601void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100602 int padding_mode, int mod, int radix_P,
603 char * input_P, int radix_Q, char * input_Q,
604 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200605 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000606{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200607 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100609 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200610 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100613 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
614 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000615
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200616 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200617 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000618
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100619 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
620 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
621 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
622 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000623
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100624 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
625 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100626 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000628
Paul Bakker821fb082009-07-12 13:26:42 +0000629
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200630 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
631 &rnd_info, MBEDTLS_RSA_PRIVATE,
632 MBEDTLS_MD_NONE, hash_result->len,
633 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000634
Paul Bakker821fb082009-07-12 13:26:42 +0000635
Ronald Cronac6ae352020-06-26 14:33:03 +0200636 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
637 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000638
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200639#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100640 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100642 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100643 int res;
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200644 memset( output, 0x00, sizeof( output) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100645
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100646 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200647 &mbedtls_test_rnd_pseudo_rand, &rnd_info,
648 MBEDTLS_RSA_PRIVATE, hash_result->len,
649 hash_result->x, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100650
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100651#if !defined(MBEDTLS_RSA_ALT)
652 TEST_ASSERT( res == 0 );
653#else
654 TEST_ASSERT( ( res == 0 ) ||
TRodziewiczb579ccd2021-04-13 14:28:28 +0200655 ( res == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100656#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100657
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100658 if( res == 0 )
659 {
Ronald Cronac6ae352020-06-26 14:33:03 +0200660 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200661 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200662 result_str->len ) == 0 );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100663 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100664 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200665#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100666
Paul Bakkerbd51b262014-07-10 15:26:12 +0200667exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100668 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
669 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000672}
Paul Bakker33b43f12013-08-20 11:48:36 +0200673/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000674
Paul Bakker33b43f12013-08-20 11:48:36 +0200675/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100676void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200677 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100678 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100679 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000680{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200681 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000683
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100684 mbedtls_mpi N, E;
685 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100688 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000689
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100690 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
691 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000692
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100693 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
694 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000696
Paul Bakker821fb082009-07-12 13:26:42 +0000697
Azim Khand30ca132017-06-09 04:32:58 +0100698 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE, hash_result->len, hash_result->x, result_str->x ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100699
Paul Bakkerbd51b262014-07-10 15:26:12 +0200700exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100701 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000703}
Paul Bakker33b43f12013-08-20 11:48:36 +0200704/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000705
Paul Bakker33b43f12013-08-20 11:48:36 +0200706/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100707void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100708 int mod, int radix_N, char * input_N,
709 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200710 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000711{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200712 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200714 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000715
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100716 mbedtls_mpi N, E;
717 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
718
Ronald Cron351f0ee2020-06-10 12:12:18 +0200719 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200722 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000723
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100724 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
725 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000726
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100727 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
728 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000730
Paul Bakker42a29bf2009-07-07 20:18:41 +0000731
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200732 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
733 &mbedtls_test_rnd_pseudo_rand,
734 &rnd_info, MBEDTLS_RSA_PUBLIC,
735 message_str->len, message_str->x,
736 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200737 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000738 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000739
Ronald Cronac6ae352020-06-26 14:33:03 +0200740 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
741 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000742 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100743
Paul Bakkerbd51b262014-07-10 15:26:12 +0200744exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100745 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000747}
Paul Bakker33b43f12013-08-20 11:48:36 +0200748/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000749
Paul Bakker33b43f12013-08-20 11:48:36 +0200750/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100751void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100752 int mod, int radix_N, char * input_N,
753 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200754 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000755{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200756 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000758
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100759 mbedtls_mpi N, E;
760
761 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200763 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000764
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100765 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
766 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000767
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100768 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
769 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000771
Paul Bakkera6656852010-07-18 19:47:14 +0000772
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200773 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
774 NULL, MBEDTLS_RSA_PUBLIC,
775 message_str->len, message_str->x,
776 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200777 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000778 {
Paul Bakkera6656852010-07-18 19:47:14 +0000779
Ronald Cronac6ae352020-06-26 14:33:03 +0200780 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
781 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000782 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100783
Paul Bakkerbd51b262014-07-10 15:26:12 +0200784exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100785 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000787}
Paul Bakker33b43f12013-08-20 11:48:36 +0200788/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000789
Paul Bakker33b43f12013-08-20 11:48:36 +0200790/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100791void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100792 int mod, int radix_P, char * input_P,
793 int radix_Q, char * input_Q, int radix_N,
794 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200795 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100796 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000797{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200798 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000800 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200801 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100802 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000803
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100804 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
805 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000808
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200809 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200810 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000811
Paul Bakker42a29bf2009-07-07 20:18:41 +0000812
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100813 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
814 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
815 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
816 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000817
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100818 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
819 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100820 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000822
Paul Bakker69998dd2009-07-11 19:15:20 +0000823 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000824
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200825 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
826 &rnd_info, MBEDTLS_RSA_PRIVATE,
827 &output_len, message_str->x, output,
828 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200829 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000830 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000831
Ronald Cronac6ae352020-06-26 14:33:03 +0200832 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200833 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200834 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000835 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000836
Paul Bakkerbd51b262014-07-10 15:26:12 +0200837exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100838 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
839 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000841}
Paul Bakker33b43f12013-08-20 11:48:36 +0200842/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000843
Paul Bakker33b43f12013-08-20 11:48:36 +0200844/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100845void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100846 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200847 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000848{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200849 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000851
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100852 mbedtls_mpi N, E;
853
854 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
856 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200857 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000858
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100859 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
860 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000861
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100862 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
863 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000865
Paul Bakker821fb082009-07-12 13:26:42 +0000866
Azim Khand30ca132017-06-09 04:32:58 +0100867 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200868 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000869 {
Paul Bakker821fb082009-07-12 13:26:42 +0000870
Ronald Cronac6ae352020-06-26 14:33:03 +0200871 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
872 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000873 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100874
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100875 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200877 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100881
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200882 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100883 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100884 if( result == 0 )
885 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100886
Ronald Cronac6ae352020-06-26 14:33:03 +0200887 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
888 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100889 }
890
Paul Bakkerbd51b262014-07-10 15:26:12 +0200891exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100892 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_rsa_free( &ctx );
894 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000895}
Paul Bakker33b43f12013-08-20 11:48:36 +0200896/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000897
Paul Bakker33b43f12013-08-20 11:48:36 +0200898/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100899void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100900 char * input_P, int radix_Q, char * input_Q,
901 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200902 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100903 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000904{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200905 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100907 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200908 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200909 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000910
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100911 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
912 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
914 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000915
Ronald Cron351f0ee2020-06-10 12:12:18 +0200916 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000917
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100918 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
919 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
920 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
921 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000922
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100923 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
924 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100925 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000927
Paul Bakker821fb082009-07-12 13:26:42 +0000928
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200929 /* repeat three times to test updating of blinding values */
930 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000931 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200932 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200933 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
934 &rnd_info, message_str->x,
935 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200936 if( result == 0 )
937 {
Paul Bakker821fb082009-07-12 13:26:42 +0000938
Ronald Cronac6ae352020-06-26 14:33:03 +0200939 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200940 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200941 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200942 }
Paul Bakker821fb082009-07-12 13:26:42 +0000943 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000944
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100945 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200947 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100951
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200952 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200953 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
954 &rnd_info, message_str->x,
955 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100956 if( result == 0 )
957 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100958
Ronald Cronac6ae352020-06-26 14:33:03 +0200959 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200960 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200961 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100962 }
963
Paul Bakkerbd51b262014-07-10 15:26:12 +0200964exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100965 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
966 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000969}
Paul Bakker33b43f12013-08-20 11:48:36 +0200970/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000971
Paul Bakker33b43f12013-08-20 11:48:36 +0200972/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100973void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000974{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_rsa_context ctx;
976 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000979}
Paul Bakker33b43f12013-08-20 11:48:36 +0200980/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000981
Paul Bakker33b43f12013-08-20 11:48:36 +0200982/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100983void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
984 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000985{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100987 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000988
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100989 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000991
Paul Bakker33b43f12013-08-20 11:48:36 +0200992 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000993 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100994 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000995 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200996 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000997 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100998 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000999 }
1000
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001001 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001003
Paul Bakkerbd51b262014-07-10 15:26:12 +02001004exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001005 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001007}
Paul Bakker33b43f12013-08-20 11:48:36 +02001008/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001009
Paul Bakker33b43f12013-08-20 11:48:36 +02001010/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001011void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
1012 int radix_Q, char * input_Q, int radix_N,
1013 char * input_N, int radix_E, char * input_E,
1014 int radix_D, char * input_D, int radix_DP,
1015 char * input_DP, int radix_DQ,
1016 char * input_DQ, int radix_QP,
1017 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +00001018{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +00001020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001022
Paul Bakker33b43f12013-08-20 11:48:36 +02001023 ctx.len = mod / 8;
1024 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001027 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001028 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001031 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001032 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001035 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001036 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001039 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001040 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001043 }
Hanno Becker131134f2017-08-23 08:31:07 +01001044#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +02001045 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001048 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001049 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001052 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001053 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001056 }
Hanno Becker131134f2017-08-23 08:31:07 +01001057#else
1058 ((void) radix_DP); ((void) input_DP);
1059 ((void) radix_DQ); ((void) input_DQ);
1060 ((void) radix_QP); ((void) input_QP);
1061#endif
Paul Bakker821fb082009-07-12 13:26:42 +00001062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001064
Paul Bakkerbd51b262014-07-10 15:26:12 +02001065exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001067}
Paul Bakker33b43f12013-08-20 11:48:36 +02001068/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001069
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001070/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001071void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
1072 int radix_Epub, char * input_Epub, int radix_P,
1073 char * input_P, int radix_Q, char * input_Q,
1074 int radix_N, char * input_N, int radix_E,
1075 char * input_E, int radix_D, char * input_D,
1076 int radix_DP, char * input_DP, int radix_DQ,
1077 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001078 int result )
1079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1083 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001084
1085 pub.len = mod / 8;
1086 prv.len = mod / 8;
1087
1088 if( strlen( input_Npub ) )
1089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001091 }
1092 if( strlen( input_Epub ) )
1093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001095 }
1096
1097 if( strlen( input_P ) )
1098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001100 }
1101 if( strlen( input_Q ) )
1102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001104 }
1105 if( strlen( input_N ) )
1106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001108 }
1109 if( strlen( input_E ) )
1110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001112 }
1113 if( strlen( input_D ) )
1114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001116 }
Hanno Becker131134f2017-08-23 08:31:07 +01001117#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001118 if( strlen( input_DP ) )
1119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001121 }
1122 if( strlen( input_DQ ) )
1123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001125 }
1126 if( strlen( input_QP ) )
1127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001129 }
Hanno Becker131134f2017-08-23 08:31:07 +01001130#else
1131 ((void) radix_DP); ((void) input_DP);
1132 ((void) radix_DQ); ((void) input_DQ);
1133 ((void) radix_QP); ((void) input_QP);
1134#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001137
1138exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 mbedtls_rsa_free( &pub );
1140 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001141}
1142/* END_CASE */
1143
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001144/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 mbedtls_rsa_context ctx;
1148 mbedtls_entropy_context entropy;
1149 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001150 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001151
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001152 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001154 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001155
Hanno Beckera47023e2017-12-22 17:08:03 +00001156 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1157 &entropy, (const unsigned char *) pers,
1158 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001161 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001164 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001165 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001166
Paul Bakkerbd51b262014-07-10 15:26:12 +02001167exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 mbedtls_rsa_free( &ctx );
1169 mbedtls_ctr_drbg_free( &ctr_drbg );
1170 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001171}
Paul Bakker33b43f12013-08-20 11:48:36 +02001172/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001173
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001174/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001175void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001176 int radix_D, char *input_D,
1177 int radix_E, char *input_E,
1178 int radix_P, char *output_P,
1179 int radix_Q, char *output_Q,
1180 int corrupt, int result )
1181{
1182 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1183
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001184 mbedtls_mpi_init( &N );
1185 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1186 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1187 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1188
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001189 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1190 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1191 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1192 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1193 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1194
1195 if( corrupt )
1196 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1197
1198 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001199 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001200
1201 if( !corrupt )
1202 {
1203 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1204 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1205 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1206 }
1207
1208exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001209 mbedtls_mpi_free( &N );
1210 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1211 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1212 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001213}
1214/* END_CASE */
1215
Hanno Becker6b4ce492017-08-23 11:00:21 +01001216/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001217void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1218 int radix_Q, char *input_Q,
1219 int radix_E, char *input_E,
1220 int radix_D, char *output_D,
1221 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001222{
1223 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1224
1225 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1226 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1227 mbedtls_mpi_init( &E );
1228 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1229
1230 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1231 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1232 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1233 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1234
1235 if( corrupt )
1236 {
1237 /* Make E even */
1238 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1239 }
1240
1241 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001242 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1243 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001244
1245 if( !corrupt )
1246 {
1247 /*
1248 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1249 */
1250
1251 /* Replace P,Q by P-1, Q-1 */
1252 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1253 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1254
1255 /* Check D == Dp modulo P-1 */
1256 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1257 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1258 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1259
1260 /* Check D == Dp modulo Q-1 */
1261 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1262 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1263 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1264 }
1265
1266exit:
1267
1268 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1269 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1270 mbedtls_mpi_free( &E );
1271 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1272}
1273/* END_CASE */
1274
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001275/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001276void mbedtls_rsa_import( int radix_N, char *input_N,
1277 int radix_P, char *input_P,
1278 int radix_Q, char *input_Q,
1279 int radix_D, char *input_D,
1280 int radix_E, char *input_E,
1281 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001282 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001283 int res_check,
1284 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001285{
1286 mbedtls_mpi N, P, Q, D, E;
1287 mbedtls_rsa_context ctx;
1288
Hanno Beckere1582a82017-09-29 11:51:05 +01001289 /* Buffers used for encryption-decryption test */
1290 unsigned char *buf_orig = NULL;
1291 unsigned char *buf_enc = NULL;
1292 unsigned char *buf_dec = NULL;
1293
Hanno Beckerc77ab892017-08-23 11:01:06 +01001294 mbedtls_entropy_context entropy;
1295 mbedtls_ctr_drbg_context ctr_drbg;
1296 const char *pers = "test_suite_rsa";
1297
Hanno Becker4d6e8342017-09-29 11:50:18 +01001298 const int have_N = ( strlen( input_N ) > 0 );
1299 const int have_P = ( strlen( input_P ) > 0 );
1300 const int have_Q = ( strlen( input_Q ) > 0 );
1301 const int have_D = ( strlen( input_D ) > 0 );
1302 const int have_E = ( strlen( input_E ) > 0 );
1303
Hanno Beckerc77ab892017-08-23 11:01:06 +01001304 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001305 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001306 mbedtls_rsa_init( &ctx, 0, 0 );
1307
1308 mbedtls_mpi_init( &N );
1309 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1310 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1311
Hanno Beckerd4d60572018-01-10 07:12:01 +00001312 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1313 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1314
Hanno Becker4d6e8342017-09-29 11:50:18 +01001315 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001316 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1317
Hanno Becker4d6e8342017-09-29 11:50:18 +01001318 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001319 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1320
Hanno Becker4d6e8342017-09-29 11:50:18 +01001321 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001322 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1323
Hanno Becker4d6e8342017-09-29 11:50:18 +01001324 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001325 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1326
Hanno Becker4d6e8342017-09-29 11:50:18 +01001327 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001328 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1329
1330 if( !successive )
1331 {
1332 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001333 have_N ? &N : NULL,
1334 have_P ? &P : NULL,
1335 have_Q ? &Q : NULL,
1336 have_D ? &D : NULL,
1337 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001338 }
1339 else
1340 {
1341 /* Import N, P, Q, D, E separately.
1342 * This should make no functional difference. */
1343
1344 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001345 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001346 NULL, NULL, NULL, NULL ) == 0 );
1347
1348 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1349 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001350 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001351 NULL, NULL, NULL ) == 0 );
1352
1353 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1354 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001355 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001356 NULL, NULL ) == 0 );
1357
1358 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1359 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001360 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001361 NULL ) == 0 );
1362
1363 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1364 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001365 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001366 }
1367
Hanno Becker04877a42017-10-11 10:01:33 +01001368 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001369
Hanno Beckere1582a82017-09-29 11:51:05 +01001370 /* On expected success, perform some public and private
1371 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001372 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001373 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001374 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001375 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1376 else
1377 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1378
1379 if( res_check != 0 )
1380 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001381
1382 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1383 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1384 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1385 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1386 goto exit;
1387
1388 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1389 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1390
1391 /* Make sure the number we're generating is smaller than the modulus */
1392 buf_orig[0] = 0x00;
1393
1394 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1395
1396 if( is_priv )
1397 {
1398 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1399 &ctr_drbg, buf_enc,
1400 buf_dec ) == 0 );
1401
1402 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1403 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1404 }
1405 }
1406
Hanno Beckerc77ab892017-08-23 11:01:06 +01001407exit:
1408
Hanno Beckere1582a82017-09-29 11:51:05 +01001409 mbedtls_free( buf_orig );
1410 mbedtls_free( buf_enc );
1411 mbedtls_free( buf_dec );
1412
Hanno Beckerc77ab892017-08-23 11:01:06 +01001413 mbedtls_rsa_free( &ctx );
1414
1415 mbedtls_ctr_drbg_free( &ctr_drbg );
1416 mbedtls_entropy_free( &entropy );
1417
1418 mbedtls_mpi_free( &N );
1419 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1420 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1421}
1422/* END_CASE */
1423
Hanno Becker417f2d62017-08-23 11:44:51 +01001424/* BEGIN_CASE */
1425void mbedtls_rsa_export( int radix_N, char *input_N,
1426 int radix_P, char *input_P,
1427 int radix_Q, char *input_Q,
1428 int radix_D, char *input_D,
1429 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001430 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001431 int successive )
1432{
1433 /* Original MPI's with which we set up the RSA context */
1434 mbedtls_mpi N, P, Q, D, E;
1435
1436 /* Exported MPI's */
1437 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1438
1439 const int have_N = ( strlen( input_N ) > 0 );
1440 const int have_P = ( strlen( input_P ) > 0 );
1441 const int have_Q = ( strlen( input_Q ) > 0 );
1442 const int have_D = ( strlen( input_D ) > 0 );
1443 const int have_E = ( strlen( input_E ) > 0 );
1444
Hanno Becker417f2d62017-08-23 11:44:51 +01001445 mbedtls_rsa_context ctx;
1446
1447 mbedtls_rsa_init( &ctx, 0, 0 );
1448
1449 mbedtls_mpi_init( &N );
1450 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1451 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1452
1453 mbedtls_mpi_init( &Ne );
1454 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1455 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1456
1457 /* Setup RSA context */
1458
1459 if( have_N )
1460 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1461
1462 if( have_P )
1463 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1464
1465 if( have_Q )
1466 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1467
1468 if( have_D )
1469 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1470
1471 if( have_E )
1472 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1473
1474 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1475 strlen( input_N ) ? &N : NULL,
1476 strlen( input_P ) ? &P : NULL,
1477 strlen( input_Q ) ? &Q : NULL,
1478 strlen( input_D ) ? &D : NULL,
1479 strlen( input_E ) ? &E : NULL ) == 0 );
1480
Hanno Becker7f25f852017-10-10 16:56:22 +01001481 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001482
1483 /*
1484 * Export parameters and compare to original ones.
1485 */
1486
1487 /* N and E must always be present. */
1488 if( !successive )
1489 {
1490 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1491 }
1492 else
1493 {
1494 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1495 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1496 }
1497 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1498 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1499
1500 /* If we were providing enough information to setup a complete private context,
1501 * we expect to be able to export all core parameters. */
1502
1503 if( is_priv )
1504 {
1505 if( !successive )
1506 {
1507 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1508 &De, NULL ) == 0 );
1509 }
1510 else
1511 {
1512 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1513 NULL, NULL ) == 0 );
1514 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1515 NULL, NULL ) == 0 );
1516 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1517 &De, NULL ) == 0 );
1518 }
1519
1520 if( have_P )
1521 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1522
1523 if( have_Q )
1524 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1525
1526 if( have_D )
1527 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1528
1529 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001530 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1531 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001532 }
1533
1534exit:
1535
1536 mbedtls_rsa_free( &ctx );
1537
1538 mbedtls_mpi_free( &N );
1539 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1540 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1541
1542 mbedtls_mpi_free( &Ne );
1543 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1544 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1545}
1546/* END_CASE */
1547
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001548/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001549void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1550 int radix_P, char *input_P,
1551 int radix_Q, char *input_Q,
1552 int radix_D, char *input_D,
1553 int radix_E, char *input_E,
1554 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001555{
1556 /* Original MPI's with which we set up the RSA context */
1557 mbedtls_mpi N, P, Q, D, E;
1558
1559 const int have_N = ( strlen( input_N ) > 0 );
1560 const int have_P = ( strlen( input_P ) > 0 );
1561 const int have_Q = ( strlen( input_Q ) > 0 );
1562 const int have_D = ( strlen( input_D ) > 0 );
1563 const int have_E = ( strlen( input_E ) > 0 );
1564
1565 mbedtls_entropy_context entropy;
1566 mbedtls_ctr_drbg_context ctr_drbg;
1567 const char *pers = "test_suite_rsa";
1568
1569 mbedtls_mpi_init( &N );
1570 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1571 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1572
1573 mbedtls_ctr_drbg_init( &ctr_drbg );
1574 mbedtls_entropy_init( &entropy );
1575 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1576 &entropy, (const unsigned char *) pers,
1577 strlen( pers ) ) == 0 );
1578
1579 if( have_N )
1580 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1581
1582 if( have_P )
1583 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1584
1585 if( have_Q )
1586 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1587
1588 if( have_D )
1589 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1590
1591 if( have_E )
1592 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1593
Hanno Becker750e8b42017-08-25 07:54:27 +01001594 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1595 have_P ? &P : NULL,
1596 have_Q ? &Q : NULL,
1597 have_D ? &D : NULL,
1598 have_E ? &E : NULL,
1599 prng ? mbedtls_ctr_drbg_random : NULL,
1600 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001601exit:
1602
1603 mbedtls_ctr_drbg_free( &ctr_drbg );
1604 mbedtls_entropy_free( &entropy );
1605
1606 mbedtls_mpi_free( &N );
1607 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1608 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1609}
1610/* END_CASE */
1611
Hanno Beckerc77ab892017-08-23 11:01:06 +01001612/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001613void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1614 data_t *input_Q, data_t *input_D,
1615 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001616 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001617{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001618 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001619 unsigned char bufNe[256];
1620 unsigned char bufPe[128];
1621 unsigned char bufQe[128];
1622 unsigned char bufDe[256];
1623 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001624
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001625 mbedtls_rsa_context ctx;
1626
1627 mbedtls_rsa_init( &ctx, 0, 0 );
1628
1629 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001630 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001631 input_N->len ? input_N->x : NULL, input_N->len,
1632 input_P->len ? input_P->x : NULL, input_P->len,
1633 input_Q->len ? input_Q->x : NULL, input_Q->len,
1634 input_D->len ? input_D->x : NULL, input_D->len,
1635 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001636
Hanno Becker7f25f852017-10-10 16:56:22 +01001637 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001638
1639 /*
1640 * Export parameters and compare to original ones.
1641 */
1642
1643 /* N and E must always be present. */
1644 if( !successive )
1645 {
Azim Khand30ca132017-06-09 04:32:58 +01001646 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001647 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001648 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001649 }
1650 else
1651 {
Azim Khand30ca132017-06-09 04:32:58 +01001652 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001653 NULL, 0, NULL, 0, NULL, 0,
1654 NULL, 0 ) == 0 );
1655 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1656 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001657 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001658 }
Azim Khand30ca132017-06-09 04:32:58 +01001659 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1660 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001661
1662 /* If we were providing enough information to setup a complete private context,
1663 * we expect to be able to export all core parameters. */
1664
1665 if( is_priv )
1666 {
1667 if( !successive )
1668 {
1669 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001670 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1671 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1672 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001673 NULL, 0 ) == 0 );
1674 }
1675 else
1676 {
1677 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001678 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001679 NULL, 0, NULL, 0,
1680 NULL, 0 ) == 0 );
1681
1682 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001683 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001684 NULL, 0, NULL, 0 ) == 0 );
1685
Azim Khand30ca132017-06-09 04:32:58 +01001686 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1687 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001688 NULL, 0 ) == 0 );
1689 }
1690
Azim Khand30ca132017-06-09 04:32:58 +01001691 if( input_P->len )
1692 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001693
Azim Khand30ca132017-06-09 04:32:58 +01001694 if( input_Q->len )
1695 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001696
Azim Khand30ca132017-06-09 04:32:58 +01001697 if( input_D->len )
1698 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001699
1700 }
1701
1702exit:
1703 mbedtls_rsa_free( &ctx );
1704}
1705/* END_CASE */
1706
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001707/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001708void mbedtls_rsa_import_raw( data_t *input_N,
1709 data_t *input_P, data_t *input_Q,
1710 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001711 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001712 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001713 int res_check,
1714 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001715{
Hanno Beckere1582a82017-09-29 11:51:05 +01001716 /* Buffers used for encryption-decryption test */
1717 unsigned char *buf_orig = NULL;
1718 unsigned char *buf_enc = NULL;
1719 unsigned char *buf_dec = NULL;
1720
Hanno Beckerc77ab892017-08-23 11:01:06 +01001721 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001722 mbedtls_entropy_context entropy;
1723 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001724
Hanno Beckerc77ab892017-08-23 11:01:06 +01001725 const char *pers = "test_suite_rsa";
1726
1727 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001728 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001729 mbedtls_rsa_init( &ctx, 0, 0 );
1730
Hanno Beckerc77ab892017-08-23 11:01:06 +01001731 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1732 &entropy, (const unsigned char *) pers,
1733 strlen( pers ) ) == 0 );
1734
Hanno Beckerc77ab892017-08-23 11:01:06 +01001735 if( !successive )
1736 {
1737 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001738 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1739 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1740 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1741 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1742 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001743 }
1744 else
1745 {
1746 /* Import N, P, Q, D, E separately.
1747 * This should make no functional difference. */
1748
1749 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001750 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001751 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1752
1753 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1754 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001755 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001756 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1757
1758 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1759 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001760 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001761 NULL, 0, NULL, 0 ) == 0 );
1762
1763 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1764 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001765 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001766 NULL, 0 ) == 0 );
1767
1768 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1769 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001770 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001771 }
1772
Hanno Becker04877a42017-10-11 10:01:33 +01001773 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001774
Hanno Beckere1582a82017-09-29 11:51:05 +01001775 /* On expected success, perform some public and private
1776 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001777 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001778 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001779 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001780 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1781 else
1782 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1783
1784 if( res_check != 0 )
1785 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001786
1787 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1788 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1789 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1790 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1791 goto exit;
1792
1793 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1794 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1795
1796 /* Make sure the number we're generating is smaller than the modulus */
1797 buf_orig[0] = 0x00;
1798
1799 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1800
1801 if( is_priv )
1802 {
1803 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1804 &ctr_drbg, buf_enc,
1805 buf_dec ) == 0 );
1806
1807 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1808 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1809 }
1810 }
1811
Hanno Beckerc77ab892017-08-23 11:01:06 +01001812exit:
1813
Hanno Becker3f3ae852017-10-02 10:08:39 +01001814 mbedtls_free( buf_orig );
1815 mbedtls_free( buf_enc );
1816 mbedtls_free( buf_dec );
1817
Hanno Beckerc77ab892017-08-23 11:01:06 +01001818 mbedtls_rsa_free( &ctx );
1819
1820 mbedtls_ctr_drbg_free( &ctr_drbg );
1821 mbedtls_entropy_free( &entropy );
1822
1823}
1824/* END_CASE */
1825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001827void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001828{
Andres AG93012e82016-09-09 09:10:28 +01001829 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001830}
Paul Bakker33b43f12013-08-20 11:48:36 +02001831/* END_CASE */