blob: d3d016da05f9d72ddc00b10cde1760fde1ab1b01 [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,
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 ) ||
655 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
656#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
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200700#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100701 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100703 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100704 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100705 int ok;
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200706 size_t olen;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100707
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100708 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Azim Khand30ca132017-06-09 04:32:58 +0100710 &olen, result_str->x, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100711
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100712#if !defined(MBEDTLS_RSA_ALT)
713 TEST_ASSERT( res == 0 );
714#else
715 TEST_ASSERT( ( res == 0 ) ||
716 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
717#endif
718
719 if( res == 0 )
720 {
Azim Khand30ca132017-06-09 04:32:58 +0100721 ok = olen == hash_result->len && memcmp( output, hash_result->x, olen ) == 0;
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100722 if( correct == 0 )
723 TEST_ASSERT( ok == 1 );
724 else
725 TEST_ASSERT( ok == 0 );
726 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100727 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200728#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100729
Paul Bakkerbd51b262014-07-10 15:26:12 +0200730exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100731 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000733}
Paul Bakker33b43f12013-08-20 11:48:36 +0200734/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000735
Paul Bakker33b43f12013-08-20 11:48:36 +0200736/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100737void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100738 int mod, int radix_N, char * input_N,
739 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200740 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000741{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200742 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200744 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000745
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100746 mbedtls_mpi N, E;
747 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
748
Ronald Cron351f0ee2020-06-10 12:12:18 +0200749 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200752 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000753
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100754 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
755 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000756
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100757 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
758 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000760
Paul Bakker42a29bf2009-07-07 20:18:41 +0000761
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200762 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
763 &mbedtls_test_rnd_pseudo_rand,
764 &rnd_info, MBEDTLS_RSA_PUBLIC,
765 message_str->len, message_str->x,
766 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200767 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000768 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000769
Ronald Cronac6ae352020-06-26 14:33:03 +0200770 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
771 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000772 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100773
Paul Bakkerbd51b262014-07-10 15:26:12 +0200774exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100775 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000777}
Paul Bakker33b43f12013-08-20 11:48:36 +0200778/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000779
Paul Bakker33b43f12013-08-20 11:48:36 +0200780/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100781void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100782 int mod, int radix_N, char * input_N,
783 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200784 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000785{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200786 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000788
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100789 mbedtls_mpi N, E;
790
791 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200793 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000794
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100795 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
796 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000797
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100798 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
799 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000801
Paul Bakkera6656852010-07-18 19:47:14 +0000802
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200803 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
804 NULL, MBEDTLS_RSA_PUBLIC,
805 message_str->len, message_str->x,
806 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200807 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000808 {
Paul Bakkera6656852010-07-18 19:47:14 +0000809
Ronald Cronac6ae352020-06-26 14:33:03 +0200810 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
811 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000812 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100813
Paul Bakkerbd51b262014-07-10 15:26:12 +0200814exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100815 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000817}
Paul Bakker33b43f12013-08-20 11:48:36 +0200818/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000819
Paul Bakker33b43f12013-08-20 11:48:36 +0200820/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100821void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100822 int mod, int radix_P, char * input_P,
823 int radix_Q, char * input_Q, int radix_N,
824 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200825 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100826 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000827{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200828 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000830 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200831 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100832 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000833
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100834 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
835 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000838
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200839 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200840 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000841
Paul Bakker42a29bf2009-07-07 20:18:41 +0000842
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100843 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
844 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
845 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
846 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000847
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100848 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
849 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100850 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000852
Paul Bakker69998dd2009-07-11 19:15:20 +0000853 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000854
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200855 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
856 &rnd_info, MBEDTLS_RSA_PRIVATE,
857 &output_len, message_str->x, output,
858 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200859 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000860 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000861
Ronald Cronac6ae352020-06-26 14:33:03 +0200862 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200863 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200864 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000865 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000866
Paul Bakkerbd51b262014-07-10 15:26:12 +0200867exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100868 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
869 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000871}
Paul Bakker33b43f12013-08-20 11:48:36 +0200872/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000873
Paul Bakker33b43f12013-08-20 11:48:36 +0200874/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100875void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100876 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200877 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000878{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200879 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000881
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100882 mbedtls_mpi N, E;
883
884 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
886 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200887 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000888
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100889 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
890 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000891
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100892 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
893 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000895
Paul Bakker821fb082009-07-12 13:26:42 +0000896
Azim Khand30ca132017-06-09 04:32:58 +0100897 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200898 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000899 {
Paul Bakker821fb082009-07-12 13:26:42 +0000900
Ronald Cronac6ae352020-06-26 14:33:03 +0200901 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
902 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000903 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100904
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100905 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200907 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100911
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200912 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100913 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100914 if( result == 0 )
915 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100916
Ronald Cronac6ae352020-06-26 14:33:03 +0200917 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
918 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100919 }
920
Paul Bakkerbd51b262014-07-10 15:26:12 +0200921exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100922 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 mbedtls_rsa_free( &ctx );
924 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000925}
Paul Bakker33b43f12013-08-20 11:48:36 +0200926/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000927
Paul Bakker33b43f12013-08-20 11:48:36 +0200928/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100929void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100930 char * input_P, int radix_Q, char * input_Q,
931 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200932 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100933 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000934{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200935 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100937 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200938 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200939 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000940
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100941 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
942 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
944 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000945
Ronald Cron351f0ee2020-06-10 12:12:18 +0200946 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000947
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100948 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
949 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
950 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
951 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000952
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100953 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
954 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
Paul Bakker821fb082009-07-12 13:26:42 +0000958
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200959 /* repeat three times to test updating of blinding values */
960 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000961 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200962 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200963 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
964 &rnd_info, message_str->x,
965 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200966 if( result == 0 )
967 {
Paul Bakker821fb082009-07-12 13:26:42 +0000968
Ronald Cronac6ae352020-06-26 14:33:03 +0200969 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200970 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200971 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200972 }
Paul Bakker821fb082009-07-12 13:26:42 +0000973 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000974
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100975 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200977 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100981
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200982 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200983 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
984 &rnd_info, message_str->x,
985 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100986 if( result == 0 )
987 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100988
Ronald Cronac6ae352020-06-26 14:33:03 +0200989 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200990 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200991 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100992 }
993
Paul Bakkerbd51b262014-07-10 15:26:12 +0200994exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100995 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
996 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000999}
Paul Bakker33b43f12013-08-20 11:48:36 +02001000/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +00001001
Paul Bakker33b43f12013-08-20 11:48:36 +02001002/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001003void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +00001004{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_rsa_context ctx;
1006 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +00001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +00001009}
Paul Bakker33b43f12013-08-20 11:48:36 +02001010/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +00001011
Paul Bakker33b43f12013-08-20 11:48:36 +02001012/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001013void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
1014 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +00001015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001017 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +00001018
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001019 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001021
Paul Bakker33b43f12013-08-20 11:48:36 +02001022 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001023 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001024 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001025 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001026 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001027 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001028 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001029 }
1030
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001031 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001033
Paul Bakkerbd51b262014-07-10 15:26:12 +02001034exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001035 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001037}
Paul Bakker33b43f12013-08-20 11:48:36 +02001038/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001039
Paul Bakker33b43f12013-08-20 11:48:36 +02001040/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001041void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
1042 int radix_Q, char * input_Q, int radix_N,
1043 char * input_N, int radix_E, char * input_E,
1044 int radix_D, char * input_D, int radix_DP,
1045 char * input_DP, int radix_DQ,
1046 char * input_DQ, int radix_QP,
1047 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +00001048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +00001050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001052
Paul Bakker33b43f12013-08-20 11:48:36 +02001053 ctx.len = mod / 8;
1054 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001057 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001058 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001061 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001062 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001065 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001066 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001069 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001070 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001073 }
Hanno Becker131134f2017-08-23 08:31:07 +01001074#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +02001075 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001078 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001079 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001082 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001083 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001086 }
Hanno Becker131134f2017-08-23 08:31:07 +01001087#else
1088 ((void) radix_DP); ((void) input_DP);
1089 ((void) radix_DQ); ((void) input_DQ);
1090 ((void) radix_QP); ((void) input_QP);
1091#endif
Paul Bakker821fb082009-07-12 13:26:42 +00001092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001094
Paul Bakkerbd51b262014-07-10 15:26:12 +02001095exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001097}
Paul Bakker33b43f12013-08-20 11:48:36 +02001098/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001099
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001100/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001101void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
1102 int radix_Epub, char * input_Epub, int radix_P,
1103 char * input_P, int radix_Q, char * input_Q,
1104 int radix_N, char * input_N, int radix_E,
1105 char * input_E, int radix_D, char * input_D,
1106 int radix_DP, char * input_DP, int radix_DQ,
1107 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001108 int result )
1109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1113 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001114
1115 pub.len = mod / 8;
1116 prv.len = mod / 8;
1117
1118 if( strlen( input_Npub ) )
1119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001121 }
1122 if( strlen( input_Epub ) )
1123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001125 }
1126
1127 if( strlen( input_P ) )
1128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001130 }
1131 if( strlen( input_Q ) )
1132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001134 }
1135 if( strlen( input_N ) )
1136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001138 }
1139 if( strlen( input_E ) )
1140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001142 }
1143 if( strlen( input_D ) )
1144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001146 }
Hanno Becker131134f2017-08-23 08:31:07 +01001147#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001148 if( strlen( input_DP ) )
1149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001151 }
1152 if( strlen( input_DQ ) )
1153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001155 }
1156 if( strlen( input_QP ) )
1157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001159 }
Hanno Becker131134f2017-08-23 08:31:07 +01001160#else
1161 ((void) radix_DP); ((void) input_DP);
1162 ((void) radix_DQ); ((void) input_DQ);
1163 ((void) radix_QP); ((void) input_QP);
1164#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001167
1168exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 mbedtls_rsa_free( &pub );
1170 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001171}
1172/* END_CASE */
1173
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001174/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001176{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_rsa_context ctx;
1178 mbedtls_entropy_context entropy;
1179 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001180 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001181
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001182 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001184 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001185
Hanno Beckera47023e2017-12-22 17:08:03 +00001186 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1187 &entropy, (const unsigned char *) pers,
1188 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001191 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001194 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001195 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001196
Paul Bakkerbd51b262014-07-10 15:26:12 +02001197exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 mbedtls_rsa_free( &ctx );
1199 mbedtls_ctr_drbg_free( &ctr_drbg );
1200 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001201}
Paul Bakker33b43f12013-08-20 11:48:36 +02001202/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001203
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001204/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001205void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001206 int radix_D, char *input_D,
1207 int radix_E, char *input_E,
1208 int radix_P, char *output_P,
1209 int radix_Q, char *output_Q,
1210 int corrupt, int result )
1211{
1212 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1213
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001214 mbedtls_mpi_init( &N );
1215 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1216 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1217 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1218
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001219 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1220 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1221 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1222 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1223 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1224
1225 if( corrupt )
1226 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1227
1228 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001229 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001230
1231 if( !corrupt )
1232 {
1233 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1234 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1235 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1236 }
1237
1238exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001239 mbedtls_mpi_free( &N );
1240 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1241 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1242 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001243}
1244/* END_CASE */
1245
Hanno Becker6b4ce492017-08-23 11:00:21 +01001246/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001247void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1248 int radix_Q, char *input_Q,
1249 int radix_E, char *input_E,
1250 int radix_D, char *output_D,
1251 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001252{
1253 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1254
1255 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1256 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1257 mbedtls_mpi_init( &E );
1258 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1259
1260 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1261 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1262 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1263 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1264
1265 if( corrupt )
1266 {
1267 /* Make E even */
1268 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1269 }
1270
1271 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001272 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1273 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001274
1275 if( !corrupt )
1276 {
1277 /*
1278 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1279 */
1280
1281 /* Replace P,Q by P-1, Q-1 */
1282 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1283 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1284
1285 /* Check D == Dp modulo P-1 */
1286 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1287 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1288 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1289
1290 /* Check D == Dp modulo Q-1 */
1291 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1292 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1293 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1294 }
1295
1296exit:
1297
1298 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1299 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1300 mbedtls_mpi_free( &E );
1301 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1302}
1303/* END_CASE */
1304
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001305/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001306void mbedtls_rsa_import( int radix_N, char *input_N,
1307 int radix_P, char *input_P,
1308 int radix_Q, char *input_Q,
1309 int radix_D, char *input_D,
1310 int radix_E, char *input_E,
1311 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001312 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001313 int res_check,
1314 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001315{
1316 mbedtls_mpi N, P, Q, D, E;
1317 mbedtls_rsa_context ctx;
1318
Hanno Beckere1582a82017-09-29 11:51:05 +01001319 /* Buffers used for encryption-decryption test */
1320 unsigned char *buf_orig = NULL;
1321 unsigned char *buf_enc = NULL;
1322 unsigned char *buf_dec = NULL;
1323
Hanno Beckerc77ab892017-08-23 11:01:06 +01001324 mbedtls_entropy_context entropy;
1325 mbedtls_ctr_drbg_context ctr_drbg;
1326 const char *pers = "test_suite_rsa";
1327
Hanno Becker4d6e8342017-09-29 11:50:18 +01001328 const int have_N = ( strlen( input_N ) > 0 );
1329 const int have_P = ( strlen( input_P ) > 0 );
1330 const int have_Q = ( strlen( input_Q ) > 0 );
1331 const int have_D = ( strlen( input_D ) > 0 );
1332 const int have_E = ( strlen( input_E ) > 0 );
1333
Hanno Beckerc77ab892017-08-23 11:01:06 +01001334 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001335 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001336 mbedtls_rsa_init( &ctx, 0, 0 );
1337
1338 mbedtls_mpi_init( &N );
1339 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1340 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1341
Hanno Beckerd4d60572018-01-10 07:12:01 +00001342 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1343 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1344
Hanno Becker4d6e8342017-09-29 11:50:18 +01001345 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001346 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1347
Hanno Becker4d6e8342017-09-29 11:50:18 +01001348 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001349 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1350
Hanno Becker4d6e8342017-09-29 11:50:18 +01001351 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001352 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1353
Hanno Becker4d6e8342017-09-29 11:50:18 +01001354 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001355 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1356
Hanno Becker4d6e8342017-09-29 11:50:18 +01001357 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001358 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1359
1360 if( !successive )
1361 {
1362 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001363 have_N ? &N : NULL,
1364 have_P ? &P : NULL,
1365 have_Q ? &Q : NULL,
1366 have_D ? &D : NULL,
1367 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001368 }
1369 else
1370 {
1371 /* Import N, P, Q, D, E separately.
1372 * This should make no functional difference. */
1373
1374 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001375 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001376 NULL, NULL, NULL, NULL ) == 0 );
1377
1378 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1379 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001380 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001381 NULL, NULL, NULL ) == 0 );
1382
1383 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1384 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001385 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001386 NULL, NULL ) == 0 );
1387
1388 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1389 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001390 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001391 NULL ) == 0 );
1392
1393 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1394 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001395 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001396 }
1397
Hanno Becker04877a42017-10-11 10:01:33 +01001398 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001399
Hanno Beckere1582a82017-09-29 11:51:05 +01001400 /* On expected success, perform some public and private
1401 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001402 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001403 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001404 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001405 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1406 else
1407 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1408
1409 if( res_check != 0 )
1410 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001411
1412 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1413 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1414 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1415 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1416 goto exit;
1417
1418 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1419 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1420
1421 /* Make sure the number we're generating is smaller than the modulus */
1422 buf_orig[0] = 0x00;
1423
1424 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1425
1426 if( is_priv )
1427 {
1428 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1429 &ctr_drbg, buf_enc,
1430 buf_dec ) == 0 );
1431
1432 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1433 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1434 }
1435 }
1436
Hanno Beckerc77ab892017-08-23 11:01:06 +01001437exit:
1438
Hanno Beckere1582a82017-09-29 11:51:05 +01001439 mbedtls_free( buf_orig );
1440 mbedtls_free( buf_enc );
1441 mbedtls_free( buf_dec );
1442
Hanno Beckerc77ab892017-08-23 11:01:06 +01001443 mbedtls_rsa_free( &ctx );
1444
1445 mbedtls_ctr_drbg_free( &ctr_drbg );
1446 mbedtls_entropy_free( &entropy );
1447
1448 mbedtls_mpi_free( &N );
1449 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1450 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1451}
1452/* END_CASE */
1453
Hanno Becker417f2d62017-08-23 11:44:51 +01001454/* BEGIN_CASE */
1455void mbedtls_rsa_export( int radix_N, char *input_N,
1456 int radix_P, char *input_P,
1457 int radix_Q, char *input_Q,
1458 int radix_D, char *input_D,
1459 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001460 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001461 int successive )
1462{
1463 /* Original MPI's with which we set up the RSA context */
1464 mbedtls_mpi N, P, Q, D, E;
1465
1466 /* Exported MPI's */
1467 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1468
1469 const int have_N = ( strlen( input_N ) > 0 );
1470 const int have_P = ( strlen( input_P ) > 0 );
1471 const int have_Q = ( strlen( input_Q ) > 0 );
1472 const int have_D = ( strlen( input_D ) > 0 );
1473 const int have_E = ( strlen( input_E ) > 0 );
1474
Hanno Becker417f2d62017-08-23 11:44:51 +01001475 mbedtls_rsa_context ctx;
1476
1477 mbedtls_rsa_init( &ctx, 0, 0 );
1478
1479 mbedtls_mpi_init( &N );
1480 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1481 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1482
1483 mbedtls_mpi_init( &Ne );
1484 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1485 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1486
1487 /* Setup RSA context */
1488
1489 if( have_N )
1490 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1491
1492 if( have_P )
1493 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1494
1495 if( have_Q )
1496 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1497
1498 if( have_D )
1499 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1500
1501 if( have_E )
1502 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1503
1504 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1505 strlen( input_N ) ? &N : NULL,
1506 strlen( input_P ) ? &P : NULL,
1507 strlen( input_Q ) ? &Q : NULL,
1508 strlen( input_D ) ? &D : NULL,
1509 strlen( input_E ) ? &E : NULL ) == 0 );
1510
Hanno Becker7f25f852017-10-10 16:56:22 +01001511 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001512
1513 /*
1514 * Export parameters and compare to original ones.
1515 */
1516
1517 /* N and E must always be present. */
1518 if( !successive )
1519 {
1520 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1521 }
1522 else
1523 {
1524 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1525 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1526 }
1527 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1528 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1529
1530 /* If we were providing enough information to setup a complete private context,
1531 * we expect to be able to export all core parameters. */
1532
1533 if( is_priv )
1534 {
1535 if( !successive )
1536 {
1537 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1538 &De, NULL ) == 0 );
1539 }
1540 else
1541 {
1542 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1543 NULL, NULL ) == 0 );
1544 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1545 NULL, NULL ) == 0 );
1546 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1547 &De, NULL ) == 0 );
1548 }
1549
1550 if( have_P )
1551 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1552
1553 if( have_Q )
1554 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1555
1556 if( have_D )
1557 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1558
1559 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001560 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1561 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001562 }
1563
1564exit:
1565
1566 mbedtls_rsa_free( &ctx );
1567
1568 mbedtls_mpi_free( &N );
1569 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1570 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1571
1572 mbedtls_mpi_free( &Ne );
1573 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1574 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1575}
1576/* END_CASE */
1577
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001578/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001579void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1580 int radix_P, char *input_P,
1581 int radix_Q, char *input_Q,
1582 int radix_D, char *input_D,
1583 int radix_E, char *input_E,
1584 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001585{
1586 /* Original MPI's with which we set up the RSA context */
1587 mbedtls_mpi N, P, Q, D, E;
1588
1589 const int have_N = ( strlen( input_N ) > 0 );
1590 const int have_P = ( strlen( input_P ) > 0 );
1591 const int have_Q = ( strlen( input_Q ) > 0 );
1592 const int have_D = ( strlen( input_D ) > 0 );
1593 const int have_E = ( strlen( input_E ) > 0 );
1594
1595 mbedtls_entropy_context entropy;
1596 mbedtls_ctr_drbg_context ctr_drbg;
1597 const char *pers = "test_suite_rsa";
1598
1599 mbedtls_mpi_init( &N );
1600 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1601 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1602
1603 mbedtls_ctr_drbg_init( &ctr_drbg );
1604 mbedtls_entropy_init( &entropy );
1605 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1606 &entropy, (const unsigned char *) pers,
1607 strlen( pers ) ) == 0 );
1608
1609 if( have_N )
1610 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1611
1612 if( have_P )
1613 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1614
1615 if( have_Q )
1616 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1617
1618 if( have_D )
1619 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1620
1621 if( have_E )
1622 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1623
Hanno Becker750e8b42017-08-25 07:54:27 +01001624 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1625 have_P ? &P : NULL,
1626 have_Q ? &Q : NULL,
1627 have_D ? &D : NULL,
1628 have_E ? &E : NULL,
1629 prng ? mbedtls_ctr_drbg_random : NULL,
1630 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001631exit:
1632
1633 mbedtls_ctr_drbg_free( &ctr_drbg );
1634 mbedtls_entropy_free( &entropy );
1635
1636 mbedtls_mpi_free( &N );
1637 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1638 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1639}
1640/* END_CASE */
1641
Hanno Beckerc77ab892017-08-23 11:01:06 +01001642/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001643void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1644 data_t *input_Q, data_t *input_D,
1645 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001646 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001647{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001648 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001649 unsigned char bufNe[256];
1650 unsigned char bufPe[128];
1651 unsigned char bufQe[128];
1652 unsigned char bufDe[256];
1653 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001654
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001655 mbedtls_rsa_context ctx;
1656
1657 mbedtls_rsa_init( &ctx, 0, 0 );
1658
1659 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001660 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001661 input_N->len ? input_N->x : NULL, input_N->len,
1662 input_P->len ? input_P->x : NULL, input_P->len,
1663 input_Q->len ? input_Q->x : NULL, input_Q->len,
1664 input_D->len ? input_D->x : NULL, input_D->len,
1665 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001666
Hanno Becker7f25f852017-10-10 16:56:22 +01001667 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001668
1669 /*
1670 * Export parameters and compare to original ones.
1671 */
1672
1673 /* N and E must always be present. */
1674 if( !successive )
1675 {
Azim Khand30ca132017-06-09 04:32:58 +01001676 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001677 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 }
1680 else
1681 {
Azim Khand30ca132017-06-09 04:32:58 +01001682 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001683 NULL, 0, NULL, 0, NULL, 0,
1684 NULL, 0 ) == 0 );
1685 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1686 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001687 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001688 }
Azim Khand30ca132017-06-09 04:32:58 +01001689 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1690 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001691
1692 /* If we were providing enough information to setup a complete private context,
1693 * we expect to be able to export all core parameters. */
1694
1695 if( is_priv )
1696 {
1697 if( !successive )
1698 {
1699 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001700 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1701 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1702 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001703 NULL, 0 ) == 0 );
1704 }
1705 else
1706 {
1707 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001708 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001709 NULL, 0, NULL, 0,
1710 NULL, 0 ) == 0 );
1711
1712 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001713 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001714 NULL, 0, NULL, 0 ) == 0 );
1715
Azim Khand30ca132017-06-09 04:32:58 +01001716 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1717 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001718 NULL, 0 ) == 0 );
1719 }
1720
Azim Khand30ca132017-06-09 04:32:58 +01001721 if( input_P->len )
1722 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001723
Azim Khand30ca132017-06-09 04:32:58 +01001724 if( input_Q->len )
1725 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001726
Azim Khand30ca132017-06-09 04:32:58 +01001727 if( input_D->len )
1728 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001729
1730 }
1731
1732exit:
1733 mbedtls_rsa_free( &ctx );
1734}
1735/* END_CASE */
1736
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001737/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001738void mbedtls_rsa_import_raw( data_t *input_N,
1739 data_t *input_P, data_t *input_Q,
1740 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001741 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001742 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001743 int res_check,
1744 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001745{
Hanno Beckere1582a82017-09-29 11:51:05 +01001746 /* Buffers used for encryption-decryption test */
1747 unsigned char *buf_orig = NULL;
1748 unsigned char *buf_enc = NULL;
1749 unsigned char *buf_dec = NULL;
1750
Hanno Beckerc77ab892017-08-23 11:01:06 +01001751 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001752 mbedtls_entropy_context entropy;
1753 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001754
Hanno Beckerc77ab892017-08-23 11:01:06 +01001755 const char *pers = "test_suite_rsa";
1756
1757 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001758 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001759 mbedtls_rsa_init( &ctx, 0, 0 );
1760
Hanno Beckerc77ab892017-08-23 11:01:06 +01001761 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1762 &entropy, (const unsigned char *) pers,
1763 strlen( pers ) ) == 0 );
1764
Hanno Beckerc77ab892017-08-23 11:01:06 +01001765 if( !successive )
1766 {
1767 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001768 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1769 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1770 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1771 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1772 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001773 }
1774 else
1775 {
1776 /* Import N, P, Q, D, E separately.
1777 * This should make no functional difference. */
1778
1779 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001780 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001781 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1782
1783 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1784 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001785 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001786 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1787
1788 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1789 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001790 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001791 NULL, 0, NULL, 0 ) == 0 );
1792
1793 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1794 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001795 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001796 NULL, 0 ) == 0 );
1797
1798 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1799 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001800 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001801 }
1802
Hanno Becker04877a42017-10-11 10:01:33 +01001803 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001804
Hanno Beckere1582a82017-09-29 11:51:05 +01001805 /* On expected success, perform some public and private
1806 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001807 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001808 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001809 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001810 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1811 else
1812 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1813
1814 if( res_check != 0 )
1815 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001816
1817 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1818 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1819 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1820 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1821 goto exit;
1822
1823 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1824 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1825
1826 /* Make sure the number we're generating is smaller than the modulus */
1827 buf_orig[0] = 0x00;
1828
1829 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1830
1831 if( is_priv )
1832 {
1833 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1834 &ctr_drbg, buf_enc,
1835 buf_dec ) == 0 );
1836
1837 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1838 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1839 }
1840 }
1841
Hanno Beckerc77ab892017-08-23 11:01:06 +01001842exit:
1843
Hanno Becker3f3ae852017-10-02 10:08:39 +01001844 mbedtls_free( buf_orig );
1845 mbedtls_free( buf_enc );
1846 mbedtls_free( buf_dec );
1847
Hanno Beckerc77ab892017-08-23 11:01:06 +01001848 mbedtls_rsa_free( &ctx );
1849
1850 mbedtls_ctr_drbg_free( &ctr_drbg );
1851 mbedtls_entropy_free( &entropy );
1852
1853}
1854/* END_CASE */
1855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001857void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001858{
Andres AG93012e82016-09-09 09:10:28 +01001859 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001860}
Paul Bakker33b43f12013-08-20 11:48:36 +02001861/* END_CASE */