blob: 3c0e35f3a655e5dd200400d5c002020a0e988b5e [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"
Hanno Beckera565f542017-10-11 11:00:19 +01003#include "mbedtls/rsa_internal.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,
Werner Lewis955a0bb2022-07-07 15:09:15 +0100516 int digest, int mod, char * input_P,
Werner Lewis3d52e442022-07-06 13:03:36 +0100517 char * input_Q, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200518 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000519{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200520 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
521 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100523 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200524 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000525
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100526 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
527 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000529
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200530 memset( hash_result, 0x00, sizeof( hash_result ) );
531 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200532 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000533
Werner Lewis24b60782022-07-07 15:08:17 +0100534 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
535 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
536 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
537 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000538
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100539 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
540 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100541 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000543
Paul Bakker42a29bf2009-07-07 20:18:41 +0000544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100546 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 +0000547
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200548 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
549 &rnd_info, MBEDTLS_RSA_PRIVATE, digest,
550 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200551 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000552 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000553
Ronald Cronac6ae352020-06-26 14:33:03 +0200554 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
555 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000556 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000557
Paul Bakkerbd51b262014-07-10 15:26:12 +0200558exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100559 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
560 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000562}
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000564
Paul Bakker33b43f12013-08-20 11:48:36 +0200565/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100566void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Werner Lewis955a0bb2022-07-07 15:09:15 +0100567 int digest, int mod,
568 char * input_N, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100569 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000570{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200571 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000573
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100574 mbedtls_mpi N, E;
575
576 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200578 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000579
Werner Lewis24b60782022-07-07 15:08:17 +0100580 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
581 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100582 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
583 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000585
Paul Bakker42a29bf2009-07-07 20:18:41 +0000586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100588 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 +0000589
Azim Khand30ca132017-06-09 04:32:58 +0100590 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 +0100591
Paul Bakkerbd51b262014-07-10 15:26:12 +0200592exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100593 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000595}
Paul Bakker33b43f12013-08-20 11:48:36 +0200596/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000597
Paul Bakker821fb082009-07-12 13:26:42 +0000598
Paul Bakker33b43f12013-08-20 11:48:36 +0200599/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100600void rsa_pkcs1_sign_raw( data_t * hash_result,
Werner Lewis955a0bb2022-07-07 15:09:15 +0100601 int padding_mode, int mod,
602 char * input_P, char * input_Q,
Werner Lewis3d52e442022-07-06 13:03:36 +0100603 char * input_N, char * input_E,
604 data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000605{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200606 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100608 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200609 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100612 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
613 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000614
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200615 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200616 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000617
Werner Lewis24b60782022-07-07 15:08:17 +0100618 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
619 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
620 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
621 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000622
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100623 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
624 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100625 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000627
Paul Bakker821fb082009-07-12 13:26:42 +0000628
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200629 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
630 &rnd_info, MBEDTLS_RSA_PRIVATE,
631 MBEDTLS_MD_NONE, hash_result->len,
632 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000633
Paul Bakker821fb082009-07-12 13:26:42 +0000634
Ronald Cronac6ae352020-06-26 14:33:03 +0200635 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
636 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000637
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200638#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100639 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100641 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100642 int res;
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200643 memset( output, 0x00, sizeof( output) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100644
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100645 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200646 &mbedtls_test_rnd_pseudo_rand, &rnd_info,
647 MBEDTLS_RSA_PRIVATE, hash_result->len,
648 hash_result->x, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100649
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100650#if !defined(MBEDTLS_RSA_ALT)
651 TEST_ASSERT( res == 0 );
652#else
653 TEST_ASSERT( ( res == 0 ) ||
654 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
655#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100656
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100657 if( res == 0 )
658 {
Ronald Cronac6ae352020-06-26 14:33:03 +0200659 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200660 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200661 result_str->len ) == 0 );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100662 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100663 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200664#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100665
Paul Bakkerbd51b262014-07-10 15:26:12 +0200666exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100667 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
668 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000671}
Paul Bakker33b43f12013-08-20 11:48:36 +0200672/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000673
Paul Bakker33b43f12013-08-20 11:48:36 +0200674/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100675void rsa_pkcs1_verify_raw( data_t * hash_result,
Werner Lewis955a0bb2022-07-07 15:09:15 +0100676 int padding_mode, int mod,
677 char * input_N, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100678 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000679{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200680 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000682
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100683 mbedtls_mpi N, E;
684 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100687 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000688
Werner Lewis24b60782022-07-07 15:08:17 +0100689 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
690 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000691
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100692 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
693 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000695
Paul Bakker821fb082009-07-12 13:26:42 +0000696
Azim Khand30ca132017-06-09 04:32:58 +0100697 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 +0100698
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200699#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100700 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100702 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100703 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100704 int ok;
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200705 size_t olen;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100706
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100707 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Azim Khand30ca132017-06-09 04:32:58 +0100709 &olen, result_str->x, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100710
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100711#if !defined(MBEDTLS_RSA_ALT)
712 TEST_ASSERT( res == 0 );
713#else
714 TEST_ASSERT( ( res == 0 ) ||
715 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
716#endif
717
718 if( res == 0 )
719 {
Azim Khand30ca132017-06-09 04:32:58 +0100720 ok = olen == hash_result->len && memcmp( output, hash_result->x, olen ) == 0;
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100721 if( correct == 0 )
722 TEST_ASSERT( ok == 1 );
723 else
724 TEST_ASSERT( ok == 0 );
725 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100726 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200727#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100728
Paul Bakkerbd51b262014-07-10 15:26:12 +0200729exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100730 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000732}
Paul Bakker33b43f12013-08-20 11:48:36 +0200733/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000734
Paul Bakker33b43f12013-08-20 11:48:36 +0200735/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100736void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Werner Lewis3d52e442022-07-06 13:03:36 +0100737 int mod, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200738 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000739{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200740 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200742 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000743
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100744 mbedtls_mpi N, E;
745 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
746
Ronald Cron351f0ee2020-06-10 12:12:18 +0200747 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200750 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000751
Werner Lewis24b60782022-07-07 15:08:17 +0100752 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
753 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000754
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100755 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
756 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000758
Paul Bakker42a29bf2009-07-07 20:18:41 +0000759
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200760 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
761 &mbedtls_test_rnd_pseudo_rand,
762 &rnd_info, MBEDTLS_RSA_PUBLIC,
763 message_str->len, message_str->x,
764 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200765 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000766 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000767
Ronald Cronac6ae352020-06-26 14:33:03 +0200768 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
769 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000770 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100771
Paul Bakkerbd51b262014-07-10 15:26:12 +0200772exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100773 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000775}
Paul Bakker33b43f12013-08-20 11:48:36 +0200776/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000777
Paul Bakker33b43f12013-08-20 11:48:36 +0200778/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100779void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Werner Lewis3d52e442022-07-06 13:03:36 +0100780 int mod, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200781 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000782{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200783 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000785
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100786 mbedtls_mpi N, E;
787
788 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200790 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000791
Werner Lewis24b60782022-07-07 15:08:17 +0100792 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
793 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000794
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100795 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
796 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000798
Paul Bakkera6656852010-07-18 19:47:14 +0000799
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200800 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
801 NULL, MBEDTLS_RSA_PUBLIC,
802 message_str->len, message_str->x,
803 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200804 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000805 {
Paul Bakkera6656852010-07-18 19:47:14 +0000806
Ronald Cronac6ae352020-06-26 14:33:03 +0200807 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
808 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000809 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100810
Paul Bakkerbd51b262014-07-10 15:26:12 +0200811exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100812 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000814}
Paul Bakker33b43f12013-08-20 11:48:36 +0200815/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000816
Paul Bakker33b43f12013-08-20 11:48:36 +0200817/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100818void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Werner Lewis955a0bb2022-07-07 15:09:15 +0100819 int mod, char * input_P,
Werner Lewis3d52e442022-07-06 13:03:36 +0100820 char * input_Q, char * input_N,
821 char * input_E, int max_output,
822 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000823{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200824 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000826 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200827 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100828 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000829
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100830 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
831 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000834
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200835 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200836 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000837
Paul Bakker42a29bf2009-07-07 20:18:41 +0000838
Werner Lewis24b60782022-07-07 15:08:17 +0100839 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
840 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
841 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
842 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000843
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100844 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
845 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100846 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000848
Paul Bakker69998dd2009-07-11 19:15:20 +0000849 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000850
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200851 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
852 &rnd_info, MBEDTLS_RSA_PRIVATE,
853 &output_len, message_str->x, output,
854 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200855 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000856 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000857
Ronald Cronac6ae352020-06-26 14:33:03 +0200858 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200859 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200860 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000861 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000862
Paul Bakkerbd51b262014-07-10 15:26:12 +0200863exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100864 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
865 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000867}
Paul Bakker33b43f12013-08-20 11:48:36 +0200868/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000869
Paul Bakker33b43f12013-08-20 11:48:36 +0200870/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100871void mbedtls_rsa_public( data_t * message_str, int mod,
872 char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200873 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000874{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200875 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000877
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100878 mbedtls_mpi N, E;
879
880 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
882 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200883 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000884
Werner Lewis24b60782022-07-07 15:08:17 +0100885 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
886 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000887
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100888 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Gilles Peskine88ea3e82021-06-09 16:24:35 +0200889
890 /* Check test data consistency */
891 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100892 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000894
Azim Khand30ca132017-06-09 04:32:58 +0100895 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200896 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000897 {
Paul Bakker821fb082009-07-12 13:26:42 +0000898
Ronald Cronac6ae352020-06-26 14:33:03 +0200899 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
900 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000901 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100902
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100903 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200905 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100909
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200910 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100911 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100912 if( result == 0 )
913 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100914
Ronald Cronac6ae352020-06-26 14:33:03 +0200915 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
916 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100917 }
918
Paul Bakkerbd51b262014-07-10 15:26:12 +0200919exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100920 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_rsa_free( &ctx );
922 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000923}
Paul Bakker33b43f12013-08-20 11:48:36 +0200924/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000925
Paul Bakker33b43f12013-08-20 11:48:36 +0200926/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +0100927void mbedtls_rsa_private( data_t * message_str, int mod,
928 char * input_P, char * input_Q,
Werner Lewis3d52e442022-07-06 13:03:36 +0100929 char * input_N, char * input_E,
930 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000931{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200932 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100934 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200935 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200936 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000937
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100938 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
939 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
941 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000942
Ronald Cron351f0ee2020-06-10 12:12:18 +0200943 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000944
Werner Lewis24b60782022-07-07 15:08:17 +0100945 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
946 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
947 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
948 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000949
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100950 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
Gilles Peskine88ea3e82021-06-09 16:24:35 +0200951
952 /* Check test data consistency */
953 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100954 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100955 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000957
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200958 /* repeat three times to test updating of blinding values */
959 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000960 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200961 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200962 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
963 &rnd_info, message_str->x,
964 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200965 if( result == 0 )
966 {
Paul Bakker821fb082009-07-12 13:26:42 +0000967
Ronald Cronac6ae352020-06-26 14:33:03 +0200968 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200969 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200970 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200971 }
Paul Bakker821fb082009-07-12 13:26:42 +0000972 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000973
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100974 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200976 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100980
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200981 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200982 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
983 &rnd_info, message_str->x,
984 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100985 if( result == 0 )
986 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100987
Ronald Cronac6ae352020-06-26 14:33:03 +0200988 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200989 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200990 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100991 }
992
Paul Bakkerbd51b262014-07-10 15:26:12 +0200993exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100994 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
995 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000998}
Paul Bakker33b43f12013-08-20 11:48:36 +0200999/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +00001000
Paul Bakker33b43f12013-08-20 11:48:36 +02001001/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001002void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +00001003{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_rsa_context ctx;
1005 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +00001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +00001008}
Paul Bakker33b43f12013-08-20 11:48:36 +02001009/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +00001010
Paul Bakker33b43f12013-08-20 11:48:36 +02001011/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001012void mbedtls_rsa_check_pubkey( char * input_N, char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +00001013{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001015 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +00001016
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001017 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001019
Paul Bakker33b43f12013-08-20 11:48:36 +02001020 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001021 {
Werner Lewis24b60782022-07-07 15:08:17 +01001022 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001023 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001024 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001025 {
Werner Lewis24b60782022-07-07 15:08:17 +01001026 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001027 }
1028
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001029 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001031
Paul Bakkerbd51b262014-07-10 15:26:12 +02001032exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001033 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001035}
Paul Bakker33b43f12013-08-20 11:48:36 +02001036/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001037
Paul Bakker33b43f12013-08-20 11:48:36 +02001038/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001039void mbedtls_rsa_check_privkey( int mod, char * input_P, char * input_Q,
1040 char * input_N, char * input_E, char * input_D,
1041 char * input_DP, char * input_DQ, char * input_QP,
1042 int result )
Paul Bakker821fb082009-07-12 13:26:42 +00001043{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +00001045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001047
Paul Bakker33b43f12013-08-20 11:48:36 +02001048 ctx.len = mod / 8;
1049 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001050 {
Werner Lewis24b60782022-07-07 15:08:17 +01001051 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001052 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001053 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001054 {
Werner Lewis24b60782022-07-07 15:08:17 +01001055 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001056 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001057 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001058 {
Werner Lewis24b60782022-07-07 15:08:17 +01001059 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001060 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001061 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001062 {
Werner Lewis24b60782022-07-07 15:08:17 +01001063 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001064 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001065 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001066 {
Werner Lewis24b60782022-07-07 15:08:17 +01001067 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001068 }
Hanno Becker131134f2017-08-23 08:31:07 +01001069#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +02001070 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001071 {
Werner Lewis24b60782022-07-07 15:08:17 +01001072 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001073 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001074 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001075 {
Werner Lewis24b60782022-07-07 15:08:17 +01001076 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001077 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001078 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001079 {
Werner Lewis24b60782022-07-07 15:08:17 +01001080 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001081 }
Hanno Becker131134f2017-08-23 08:31:07 +01001082#else
Werner Lewis3e005f32022-07-07 11:38:44 +01001083 ((void) input_DP);
1084 ((void) input_DQ);
1085 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +01001086#endif
Paul Bakker821fb082009-07-12 13:26:42 +00001087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001089
Paul Bakkerbd51b262014-07-10 15:26:12 +02001090exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001092}
Paul Bakker33b43f12013-08-20 11:48:36 +02001093/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001094
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001095/* BEGIN_CASE */
Werner Lewis3d52e442022-07-06 13:03:36 +01001096void rsa_check_pubpriv( int mod, char * input_Npub, char * input_Epub,
1097 char * input_P, char * input_Q, char * input_N,
1098 char * input_E, char * input_D, char * input_DP,
1099 char * input_DQ, char * input_QP, int result )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1104 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001105
1106 pub.len = mod / 8;
1107 prv.len = mod / 8;
1108
1109 if( strlen( input_Npub ) )
1110 {
Werner Lewis24b60782022-07-07 15:08:17 +01001111 TEST_ASSERT( mbedtls_test_read_mpi( &pub.N, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001112 }
1113 if( strlen( input_Epub ) )
1114 {
Werner Lewis24b60782022-07-07 15:08:17 +01001115 TEST_ASSERT( mbedtls_test_read_mpi( &pub.E, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001116 }
1117
1118 if( strlen( input_P ) )
1119 {
Werner Lewis24b60782022-07-07 15:08:17 +01001120 TEST_ASSERT( mbedtls_test_read_mpi( &prv.P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001121 }
1122 if( strlen( input_Q ) )
1123 {
Werner Lewis24b60782022-07-07 15:08:17 +01001124 TEST_ASSERT( mbedtls_test_read_mpi( &prv.Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001125 }
1126 if( strlen( input_N ) )
1127 {
Werner Lewis24b60782022-07-07 15:08:17 +01001128 TEST_ASSERT( mbedtls_test_read_mpi( &prv.N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001129 }
1130 if( strlen( input_E ) )
1131 {
Werner Lewis24b60782022-07-07 15:08:17 +01001132 TEST_ASSERT( mbedtls_test_read_mpi( &prv.E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001133 }
1134 if( strlen( input_D ) )
1135 {
Werner Lewis24b60782022-07-07 15:08:17 +01001136 TEST_ASSERT( mbedtls_test_read_mpi( &prv.D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001137 }
Hanno Becker131134f2017-08-23 08:31:07 +01001138#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001139 if( strlen( input_DP ) )
1140 {
Werner Lewis24b60782022-07-07 15:08:17 +01001141 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001142 }
1143 if( strlen( input_DQ ) )
1144 {
Werner Lewis24b60782022-07-07 15:08:17 +01001145 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001146 }
1147 if( strlen( input_QP ) )
1148 {
Werner Lewis24b60782022-07-07 15:08:17 +01001149 TEST_ASSERT( mbedtls_test_read_mpi( &prv.QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001150 }
Hanno Becker131134f2017-08-23 08:31:07 +01001151#else
Werner Lewis3e005f32022-07-07 11:38:44 +01001152 ((void) input_DP);
1153 ((void) input_DQ);
1154 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +01001155#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001158
1159exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 mbedtls_rsa_free( &pub );
1161 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001162}
1163/* END_CASE */
1164
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001165/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 mbedtls_rsa_context ctx;
1169 mbedtls_entropy_context entropy;
1170 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001171 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001172
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001173 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001175 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001176
Hanno Beckera47023e2017-12-22 17:08:03 +00001177 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1178 &entropy, (const unsigned char *) pers,
1179 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001182 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001185 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001186 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001187
Paul Bakkerbd51b262014-07-10 15:26:12 +02001188exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_rsa_free( &ctx );
1190 mbedtls_ctr_drbg_free( &ctr_drbg );
1191 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001192}
Paul Bakker33b43f12013-08-20 11:48:36 +02001193/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001194
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001195/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001196void mbedtls_rsa_deduce_primes( char *input_N,
1197 char *input_D,
1198 char *input_E,
1199 char *output_P,
1200 char *output_Q,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001201 int corrupt, int result )
1202{
1203 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1204
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001205 mbedtls_mpi_init( &N );
1206 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1207 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1208 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1209
Werner Lewis24b60782022-07-07 15:08:17 +01001210 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
1211 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
1212 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
1213 TEST_ASSERT( mbedtls_test_read_mpi( &Qp, output_P ) == 0 );
1214 TEST_ASSERT( mbedtls_test_read_mpi( &Pp, output_Q ) == 0 );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001215
1216 if( corrupt )
1217 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1218
1219 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001220 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001221
1222 if( !corrupt )
1223 {
1224 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1225 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1226 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1227 }
1228
1229exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001230 mbedtls_mpi_free( &N );
1231 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1232 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1233 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001234}
1235/* END_CASE */
1236
Hanno Becker6b4ce492017-08-23 11:00:21 +01001237/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001238void mbedtls_rsa_deduce_private_exponent( char *input_P,
1239 char *input_Q,
1240 char *input_E,
1241 char *output_D,
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001242 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001243{
1244 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1245
1246 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1247 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1248 mbedtls_mpi_init( &E );
1249 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1250
Werner Lewis24b60782022-07-07 15:08:17 +01001251 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
1252 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
1253 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
1254 TEST_ASSERT( mbedtls_test_read_mpi( &Dp, output_D ) == 0 );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001255
1256 if( corrupt )
1257 {
1258 /* Make E even */
1259 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1260 }
1261
1262 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001263 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1264 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001265
1266 if( !corrupt )
1267 {
1268 /*
1269 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1270 */
1271
1272 /* Replace P,Q by P-1, Q-1 */
1273 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1274 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1275
1276 /* Check D == Dp modulo P-1 */
1277 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1278 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1279 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1280
1281 /* Check D == Dp modulo Q-1 */
1282 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1283 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1284 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1285 }
1286
1287exit:
1288
1289 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1290 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1291 mbedtls_mpi_free( &E );
1292 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1293}
1294/* END_CASE */
1295
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001296/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001297void mbedtls_rsa_import( char *input_N,
1298 char *input_P,
1299 char *input_Q,
1300 char *input_D,
1301 char *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001302 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001303 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001304 int res_check,
1305 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001306{
1307 mbedtls_mpi N, P, Q, D, E;
1308 mbedtls_rsa_context ctx;
1309
Hanno Beckere1582a82017-09-29 11:51:05 +01001310 /* Buffers used for encryption-decryption test */
1311 unsigned char *buf_orig = NULL;
1312 unsigned char *buf_enc = NULL;
1313 unsigned char *buf_dec = NULL;
1314
Hanno Beckerc77ab892017-08-23 11:01:06 +01001315 mbedtls_entropy_context entropy;
1316 mbedtls_ctr_drbg_context ctr_drbg;
1317 const char *pers = "test_suite_rsa";
1318
Hanno Becker4d6e8342017-09-29 11:50:18 +01001319 const int have_N = ( strlen( input_N ) > 0 );
1320 const int have_P = ( strlen( input_P ) > 0 );
1321 const int have_Q = ( strlen( input_Q ) > 0 );
1322 const int have_D = ( strlen( input_D ) > 0 );
1323 const int have_E = ( strlen( input_E ) > 0 );
1324
Hanno Beckerc77ab892017-08-23 11:01:06 +01001325 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001326 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001327 mbedtls_rsa_init( &ctx, 0, 0 );
1328
1329 mbedtls_mpi_init( &N );
1330 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1331 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1332
Hanno Beckerd4d60572018-01-10 07:12:01 +00001333 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1334 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1335
Hanno Becker4d6e8342017-09-29 11:50:18 +01001336 if( have_N )
Werner Lewis24b60782022-07-07 15:08:17 +01001337 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001338
Hanno Becker4d6e8342017-09-29 11:50:18 +01001339 if( have_P )
Werner Lewis24b60782022-07-07 15:08:17 +01001340 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001341
Hanno Becker4d6e8342017-09-29 11:50:18 +01001342 if( have_Q )
Werner Lewis24b60782022-07-07 15:08:17 +01001343 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001344
Hanno Becker4d6e8342017-09-29 11:50:18 +01001345 if( have_D )
Werner Lewis24b60782022-07-07 15:08:17 +01001346 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001347
Hanno Becker4d6e8342017-09-29 11:50:18 +01001348 if( have_E )
Werner Lewis24b60782022-07-07 15:08:17 +01001349 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001350
1351 if( !successive )
1352 {
1353 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001354 have_N ? &N : NULL,
1355 have_P ? &P : NULL,
1356 have_Q ? &Q : NULL,
1357 have_D ? &D : NULL,
1358 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001359 }
1360 else
1361 {
1362 /* Import N, P, Q, D, E separately.
1363 * This should make no functional difference. */
1364
1365 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001366 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001367 NULL, NULL, NULL, NULL ) == 0 );
1368
1369 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1370 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001371 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001372 NULL, NULL, NULL ) == 0 );
1373
1374 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1375 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001376 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001377 NULL, NULL ) == 0 );
1378
1379 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1380 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001381 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001382 NULL ) == 0 );
1383
1384 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1385 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001386 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001387 }
1388
Hanno Becker04877a42017-10-11 10:01:33 +01001389 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001390
Hanno Beckere1582a82017-09-29 11:51:05 +01001391 /* On expected success, perform some public and private
1392 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001393 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001394 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001395 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001396 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1397 else
1398 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1399
1400 if( res_check != 0 )
1401 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001402
1403 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1404 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1405 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1406 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1407 goto exit;
1408
1409 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1410 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1411
1412 /* Make sure the number we're generating is smaller than the modulus */
1413 buf_orig[0] = 0x00;
1414
1415 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1416
1417 if( is_priv )
1418 {
1419 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1420 &ctr_drbg, buf_enc,
1421 buf_dec ) == 0 );
1422
1423 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1424 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1425 }
1426 }
1427
Hanno Beckerc77ab892017-08-23 11:01:06 +01001428exit:
1429
Hanno Beckere1582a82017-09-29 11:51:05 +01001430 mbedtls_free( buf_orig );
1431 mbedtls_free( buf_enc );
1432 mbedtls_free( buf_dec );
1433
Hanno Beckerc77ab892017-08-23 11:01:06 +01001434 mbedtls_rsa_free( &ctx );
1435
1436 mbedtls_ctr_drbg_free( &ctr_drbg );
1437 mbedtls_entropy_free( &entropy );
1438
1439 mbedtls_mpi_free( &N );
1440 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1441 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1442}
1443/* END_CASE */
1444
Hanno Becker417f2d62017-08-23 11:44:51 +01001445/* BEGIN_CASE */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001446void mbedtls_rsa_export( char *input_N,
1447 char *input_P,
1448 char *input_Q,
1449 char *input_D,
1450 char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001451 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001452 int successive )
1453{
1454 /* Original MPI's with which we set up the RSA context */
1455 mbedtls_mpi N, P, Q, D, E;
1456
1457 /* Exported MPI's */
1458 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1459
1460 const int have_N = ( strlen( input_N ) > 0 );
1461 const int have_P = ( strlen( input_P ) > 0 );
1462 const int have_Q = ( strlen( input_Q ) > 0 );
1463 const int have_D = ( strlen( input_D ) > 0 );
1464 const int have_E = ( strlen( input_E ) > 0 );
1465
Hanno Becker417f2d62017-08-23 11:44:51 +01001466 mbedtls_rsa_context ctx;
1467
1468 mbedtls_rsa_init( &ctx, 0, 0 );
1469
1470 mbedtls_mpi_init( &N );
1471 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1472 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1473
1474 mbedtls_mpi_init( &Ne );
1475 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1476 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1477
1478 /* Setup RSA context */
1479
1480 if( have_N )
Werner Lewis24b60782022-07-07 15:08:17 +01001481 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001482
1483 if( have_P )
Werner Lewis24b60782022-07-07 15:08:17 +01001484 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001485
1486 if( have_Q )
Werner Lewis24b60782022-07-07 15:08:17 +01001487 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001488
1489 if( have_D )
Werner Lewis24b60782022-07-07 15:08:17 +01001490 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001491
1492 if( have_E )
Werner Lewis24b60782022-07-07 15:08:17 +01001493 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001494
1495 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1496 strlen( input_N ) ? &N : NULL,
1497 strlen( input_P ) ? &P : NULL,
1498 strlen( input_Q ) ? &Q : NULL,
1499 strlen( input_D ) ? &D : NULL,
1500 strlen( input_E ) ? &E : NULL ) == 0 );
1501
Hanno Becker7f25f852017-10-10 16:56:22 +01001502 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001503
1504 /*
1505 * Export parameters and compare to original ones.
1506 */
1507
1508 /* N and E must always be present. */
1509 if( !successive )
1510 {
1511 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1512 }
1513 else
1514 {
1515 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1516 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1517 }
1518 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1519 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1520
1521 /* If we were providing enough information to setup a complete private context,
1522 * we expect to be able to export all core parameters. */
1523
1524 if( is_priv )
1525 {
1526 if( !successive )
1527 {
1528 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1529 &De, NULL ) == 0 );
1530 }
1531 else
1532 {
1533 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1534 NULL, NULL ) == 0 );
1535 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1536 NULL, NULL ) == 0 );
1537 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1538 &De, NULL ) == 0 );
1539 }
1540
1541 if( have_P )
1542 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1543
1544 if( have_Q )
1545 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1546
1547 if( have_D )
1548 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1549
1550 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001551 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1552 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001553 }
1554
1555exit:
1556
1557 mbedtls_rsa_free( &ctx );
1558
1559 mbedtls_mpi_free( &N );
1560 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1561 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1562
1563 mbedtls_mpi_free( &Ne );
1564 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1565 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1566}
1567/* END_CASE */
1568
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001569/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Werner Lewis955a0bb2022-07-07 15:09:15 +01001570void mbedtls_rsa_validate_params( char *input_N,
1571 char *input_P,
1572 char *input_Q,
1573 char *input_D,
1574 char *input_E,
Hanno Becker750e8b42017-08-25 07:54:27 +01001575 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001576{
1577 /* Original MPI's with which we set up the RSA context */
1578 mbedtls_mpi N, P, Q, D, E;
1579
1580 const int have_N = ( strlen( input_N ) > 0 );
1581 const int have_P = ( strlen( input_P ) > 0 );
1582 const int have_Q = ( strlen( input_Q ) > 0 );
1583 const int have_D = ( strlen( input_D ) > 0 );
1584 const int have_E = ( strlen( input_E ) > 0 );
1585
1586 mbedtls_entropy_context entropy;
1587 mbedtls_ctr_drbg_context ctr_drbg;
1588 const char *pers = "test_suite_rsa";
1589
1590 mbedtls_mpi_init( &N );
1591 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1592 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1593
1594 mbedtls_ctr_drbg_init( &ctr_drbg );
1595 mbedtls_entropy_init( &entropy );
1596 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1597 &entropy, (const unsigned char *) pers,
1598 strlen( pers ) ) == 0 );
1599
1600 if( have_N )
Werner Lewis24b60782022-07-07 15:08:17 +01001601 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001602
1603 if( have_P )
Werner Lewis24b60782022-07-07 15:08:17 +01001604 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001605
1606 if( have_Q )
Werner Lewis24b60782022-07-07 15:08:17 +01001607 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001608
1609 if( have_D )
Werner Lewis24b60782022-07-07 15:08:17 +01001610 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001611
1612 if( have_E )
Werner Lewis24b60782022-07-07 15:08:17 +01001613 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001614
Hanno Becker750e8b42017-08-25 07:54:27 +01001615 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1616 have_P ? &P : NULL,
1617 have_Q ? &Q : NULL,
1618 have_D ? &D : NULL,
1619 have_E ? &E : NULL,
1620 prng ? mbedtls_ctr_drbg_random : NULL,
1621 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001622exit:
1623
1624 mbedtls_ctr_drbg_free( &ctr_drbg );
1625 mbedtls_entropy_free( &entropy );
1626
1627 mbedtls_mpi_free( &N );
1628 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1629 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1630}
1631/* END_CASE */
1632
Hanno Beckerc77ab892017-08-23 11:01:06 +01001633/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001634void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1635 data_t *input_Q, data_t *input_D,
1636 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001637 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001638{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001639 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001640 unsigned char bufNe[256];
1641 unsigned char bufPe[128];
1642 unsigned char bufQe[128];
1643 unsigned char bufDe[256];
1644 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001645
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001646 mbedtls_rsa_context ctx;
1647
1648 mbedtls_rsa_init( &ctx, 0, 0 );
1649
1650 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001651 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001652 input_N->len ? input_N->x : NULL, input_N->len,
1653 input_P->len ? input_P->x : NULL, input_P->len,
1654 input_Q->len ? input_Q->x : NULL, input_Q->len,
1655 input_D->len ? input_D->x : NULL, input_D->len,
1656 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001657
Hanno Becker7f25f852017-10-10 16:56:22 +01001658 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001659
1660 /*
1661 * Export parameters and compare to original ones.
1662 */
1663
1664 /* N and E must always be present. */
1665 if( !successive )
1666 {
Azim Khand30ca132017-06-09 04:32:58 +01001667 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001668 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001669 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001670 }
1671 else
1672 {
Azim Khand30ca132017-06-09 04:32:58 +01001673 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001674 NULL, 0, NULL, 0, NULL, 0,
1675 NULL, 0 ) == 0 );
1676 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1677 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001678 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001679 }
Azim Khand30ca132017-06-09 04:32:58 +01001680 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1681 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001682
1683 /* If we were providing enough information to setup a complete private context,
1684 * we expect to be able to export all core parameters. */
1685
1686 if( is_priv )
1687 {
1688 if( !successive )
1689 {
1690 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001691 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1692 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1693 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001694 NULL, 0 ) == 0 );
1695 }
1696 else
1697 {
1698 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001699 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001700 NULL, 0, NULL, 0,
1701 NULL, 0 ) == 0 );
1702
1703 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001704 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001705 NULL, 0, NULL, 0 ) == 0 );
1706
Azim Khand30ca132017-06-09 04:32:58 +01001707 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1708 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001709 NULL, 0 ) == 0 );
1710 }
1711
Azim Khand30ca132017-06-09 04:32:58 +01001712 if( input_P->len )
1713 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001714
Azim Khand30ca132017-06-09 04:32:58 +01001715 if( input_Q->len )
1716 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001717
Azim Khand30ca132017-06-09 04:32:58 +01001718 if( input_D->len )
1719 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001720
1721 }
1722
1723exit:
1724 mbedtls_rsa_free( &ctx );
1725}
1726/* END_CASE */
1727
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001728/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001729void mbedtls_rsa_import_raw( data_t *input_N,
1730 data_t *input_P, data_t *input_Q,
1731 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001732 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001733 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001734 int res_check,
1735 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001736{
Hanno Beckere1582a82017-09-29 11:51:05 +01001737 /* Buffers used for encryption-decryption test */
1738 unsigned char *buf_orig = NULL;
1739 unsigned char *buf_enc = NULL;
1740 unsigned char *buf_dec = NULL;
1741
Hanno Beckerc77ab892017-08-23 11:01:06 +01001742 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001743 mbedtls_entropy_context entropy;
1744 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001745
Hanno Beckerc77ab892017-08-23 11:01:06 +01001746 const char *pers = "test_suite_rsa";
1747
1748 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001749 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001750 mbedtls_rsa_init( &ctx, 0, 0 );
1751
Hanno Beckerc77ab892017-08-23 11:01:06 +01001752 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1753 &entropy, (const unsigned char *) pers,
1754 strlen( pers ) ) == 0 );
1755
Hanno Beckerc77ab892017-08-23 11:01:06 +01001756 if( !successive )
1757 {
1758 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001759 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1760 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1761 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1762 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1763 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001764 }
1765 else
1766 {
1767 /* Import N, P, Q, D, E separately.
1768 * This should make no functional difference. */
1769
1770 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001771 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001772 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1773
1774 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1775 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001776 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001777 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1778
1779 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1780 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001781 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001782 NULL, 0, NULL, 0 ) == 0 );
1783
1784 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1785 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001786 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001787 NULL, 0 ) == 0 );
1788
1789 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1790 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001791 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001792 }
1793
Hanno Becker04877a42017-10-11 10:01:33 +01001794 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001795
Hanno Beckere1582a82017-09-29 11:51:05 +01001796 /* On expected success, perform some public and private
1797 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001798 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001799 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001800 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001801 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1802 else
1803 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1804
1805 if( res_check != 0 )
1806 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001807
1808 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1809 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1810 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1811 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1812 goto exit;
1813
1814 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1815 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1816
1817 /* Make sure the number we're generating is smaller than the modulus */
1818 buf_orig[0] = 0x00;
1819
1820 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1821
1822 if( is_priv )
1823 {
1824 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1825 &ctr_drbg, buf_enc,
1826 buf_dec ) == 0 );
1827
1828 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1829 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1830 }
1831 }
1832
Hanno Beckerc77ab892017-08-23 11:01:06 +01001833exit:
1834
Hanno Becker3f3ae852017-10-02 10:08:39 +01001835 mbedtls_free( buf_orig );
1836 mbedtls_free( buf_enc );
1837 mbedtls_free( buf_dec );
1838
Hanno Beckerc77ab892017-08-23 11:01:06 +01001839 mbedtls_rsa_free( &ctx );
1840
1841 mbedtls_ctr_drbg_free( &ctr_drbg );
1842 mbedtls_entropy_free( &entropy );
1843
1844}
1845/* END_CASE */
1846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001848void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001849{
Andres AG93012e82016-09-09 09:10:28 +01001850 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001851}
Paul Bakker33b43f12013-08-20 11:48:36 +02001852/* END_CASE */