blob: 89b419fbda28540ff3cac99e6fb716b058546de7 [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
Hanno Becker046d2022018-12-13 18:07:09 +000020/* 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
Hanno Beckera7ee0022018-12-18 13:30:20 +000031 TEST_INVALID_PARAM( mbedtls_rsa_init( NULL, valid_padding, 0 ) );
32 TEST_INVALID_PARAM( mbedtls_rsa_init( &ctx, invalid_padding, 0 ) );
Hanno Becker046d2022018-12-13 18:07:09 +000033 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,
Hanno Becker046d2022018-12-13 18:07:09 +000055 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,
Hanno Beckerf04d9232018-12-18 13:30:42 +000070 mbedtls_rsa_gen_key( NULL, rnd_std_rand,
71 NULL, 0, 0 ) );
72 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
73 mbedtls_rsa_gen_key( &ctx, NULL,
74 NULL, 0, 0 ) );
Hanno Becker046d2022018-12-13 18:07:09 +000075
76 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
77 mbedtls_rsa_check_pubkey( NULL ) );
78 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
79 mbedtls_rsa_check_privkey( NULL ) );
80
81 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
82 mbedtls_rsa_check_pub_priv( NULL, &ctx ) );
83 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
84 mbedtls_rsa_check_pub_priv( &ctx, NULL ) );
85
86 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
87 mbedtls_rsa_public( NULL, buf, buf ) );
88 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
89 mbedtls_rsa_public( &ctx, NULL, buf ) );
90 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
91 mbedtls_rsa_public( &ctx, buf, NULL ) );
92
93 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
94 mbedtls_rsa_private( NULL, NULL, NULL,
95 buf, buf ) );
96 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
97 mbedtls_rsa_private( &ctx, NULL, NULL,
98 NULL, buf ) );
99 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
100 mbedtls_rsa_private( &ctx, NULL, NULL,
101 buf, NULL ) );
102
103 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
104 mbedtls_rsa_pkcs1_encrypt( NULL, NULL, NULL,
105 valid_mode,
106 sizeof( buf ), buf,
107 buf ) );
108 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
109 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
110 invalid_mode,
111 sizeof( buf ), buf,
112 buf ) );
113 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
114 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
115 valid_mode,
116 sizeof( buf ), NULL,
117 buf ) );
118 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
119 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
120 valid_mode,
121 sizeof( buf ), buf,
122 NULL ) );
123
124 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
125 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
126 NULL,
127 valid_mode,
128 sizeof( buf ), buf,
129 buf ) );
130 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
131 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
132 NULL,
133 invalid_mode,
134 sizeof( buf ), buf,
135 buf ) );
136 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
137 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
138 NULL,
139 valid_mode,
140 sizeof( buf ), NULL,
141 buf ) );
142 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
143 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
144 NULL,
145 valid_mode,
146 sizeof( buf ), buf,
147 NULL ) );
148
149 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
150 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
151 valid_mode,
152 buf, sizeof( buf ),
153 sizeof( buf ), buf,
154 buf ) );
155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
156 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
157 invalid_mode,
158 buf, sizeof( buf ),
159 sizeof( buf ), buf,
160 buf ) );
161 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
162 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
163 valid_mode,
164 NULL, sizeof( buf ),
165 sizeof( buf ), buf,
166 buf ) );
167 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
168 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
169 valid_mode,
170 buf, sizeof( buf ),
171 sizeof( buf ), NULL,
172 buf ) );
173 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
174 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
175 valid_mode,
176 buf, sizeof( buf ),
177 sizeof( buf ), buf,
178 NULL ) );
179
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
181 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
182 valid_mode, &olen,
183 buf, buf, 42 ) );
184 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
185 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
186 invalid_mode, &olen,
187 buf, buf, 42 ) );
188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
189 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
190 valid_mode, NULL,
191 buf, buf, 42 ) );
192 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
193 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
194 valid_mode, &olen,
195 NULL, buf, 42 ) );
196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
197 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
198 valid_mode, &olen,
199 buf, NULL, 42 ) );
200
201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
202 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
203 NULL,
204 valid_mode, &olen,
205 buf, buf, 42 ) );
206 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
207 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
208 NULL,
209 invalid_mode, &olen,
210 buf, buf, 42 ) );
211 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
212 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
213 NULL,
214 valid_mode, NULL,
215 buf, buf, 42 ) );
216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
217 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
218 NULL,
219 valid_mode, &olen,
220 NULL, buf, 42 ) );
221 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
222 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
223 NULL,
224 valid_mode, &olen,
225 buf, NULL, 42 ) );
226
227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
228 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
229 valid_mode,
230 buf, sizeof( buf ),
231 &olen,
232 buf, buf, 42 ) );
233 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
234 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
235 invalid_mode,
236 buf, sizeof( buf ),
237 &olen,
238 buf, buf, 42 ) );
239 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
240 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
241 valid_mode,
242 NULL, sizeof( buf ),
243 NULL,
244 buf, buf, 42 ) );
245 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
246 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
247 valid_mode,
248 buf, sizeof( buf ),
249 &olen,
250 NULL, buf, 42 ) );
251 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
252 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
253 valid_mode,
254 buf, sizeof( buf ),
255 &olen,
256 buf, NULL, 42 ) );
257
258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
259 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
260 valid_mode,
261 0, sizeof( buf ), buf,
262 buf ) );
263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
264 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
265 invalid_mode,
266 0, sizeof( buf ), buf,
267 buf ) );
268 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
269 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
270 valid_mode,
271 0, sizeof( buf ), NULL,
272 buf ) );
273 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
274 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
275 valid_mode,
276 0, sizeof( buf ), buf,
277 NULL ) );
Hanno Becker05cf6da2018-12-18 13:33:37 +0000278 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
279 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
280 valid_mode,
281 MBEDTLS_MD_SHA1,
282 0, NULL,
283 buf ) );
Hanno Becker046d2022018-12-13 18:07:09 +0000284
285 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
286 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
287 valid_mode,
288 0, sizeof( buf ), buf,
289 buf ) );
290 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
291 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
292 invalid_mode,
293 0, sizeof( buf ), buf,
294 buf ) );
295 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
296 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
297 valid_mode,
298 0, sizeof( buf ), NULL,
299 buf ) );
300 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
301 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
302 valid_mode,
303 0, sizeof( buf ), buf,
304 NULL ) );
Hanno Beckerb06f1932018-12-18 14:04:28 +0000305 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
306 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
307 valid_mode,
308 MBEDTLS_MD_SHA1,
309 0, NULL,
310 buf ) );
Hanno Becker046d2022018-12-13 18:07:09 +0000311
312 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
313 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
314 valid_mode,
315 0, sizeof( buf ), buf,
316 buf ) );
317 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
318 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
319 invalid_mode,
320 0, sizeof( buf ), buf,
321 buf ) );
322 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
323 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
324 valid_mode,
325 0, sizeof( buf ), NULL,
326 buf ) );
327 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
328 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
329 valid_mode,
330 0, sizeof( buf ), buf,
331 NULL ) );
Hanno Beckerb06f1932018-12-18 14:04:28 +0000332 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
333 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
334 valid_mode,
335 MBEDTLS_MD_SHA1,
336 0, NULL,
337 buf ) );
Hanno Becker046d2022018-12-13 18:07:09 +0000338
339 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
340 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
341 valid_mode,
342 0, sizeof( buf ), buf,
343 buf ) );
344 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
345 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
346 invalid_mode,
347 0, sizeof( buf ), buf,
348 buf ) );
349 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
350 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
351 valid_mode,
352 0, sizeof( buf ), NULL,
353 buf ) );
354 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
355 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
356 valid_mode,
357 0, sizeof( buf ), buf,
358 NULL ) );
Hanno Beckerb06f1932018-12-18 14:04:28 +0000359 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
360 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
361 valid_mode,
362 MBEDTLS_MD_SHA1, 0, NULL,
363 buf ) );
Hanno Becker046d2022018-12-13 18:07:09 +0000364
365 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
366 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
367 NULL,
368 valid_mode,
369 0, sizeof( buf ), buf,
370 buf ) );
371 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
372 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
373 NULL,
374 invalid_mode,
375 0, sizeof( buf ), buf,
376 buf ) );
377 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
378 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
379 NULL,
380 valid_mode,
381 0, sizeof( buf ),
382 NULL, buf ) );
383 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
384 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
385 NULL,
386 valid_mode,
387 0, sizeof( buf ), buf,
388 NULL ) );
Hanno Beckerb06f1932018-12-18 14:04:28 +0000389 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
390 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
391 NULL,
392 valid_mode,
393 MBEDTLS_MD_SHA1,
394 0, NULL,
395 buf ) );
Hanno Becker046d2022018-12-13 18:07:09 +0000396
397 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
398 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
399 valid_mode,
400 0, sizeof( buf ),
401 buf, buf ) );
402 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
403 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
404 invalid_mode,
405 0, sizeof( buf ),
406 buf, buf ) );
407 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
408 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
409 valid_mode,
410 0, sizeof( buf ),
411 NULL, buf ) );
412 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
413 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
414 valid_mode,
415 0, sizeof( buf ),
416 buf, NULL ) );
Hanno Beckerb06f1932018-12-18 14:04:28 +0000417 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
418 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
419 valid_mode,
420 MBEDTLS_MD_SHA1,
421 0, NULL,
422 buf ) );
Hanno Becker046d2022018-12-13 18:07:09 +0000423
424 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
425 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
426 valid_mode,
427 0, sizeof( buf ),
428 buf,
429 0, 0,
430 buf ) );
431 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
432 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
433 invalid_mode,
434 0, sizeof( buf ),
435 buf,
436 0, 0,
437 buf ) );
438 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
439 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
440 valid_mode,
441 0, sizeof( buf ),
442 NULL, 0, 0,
443 buf ) );
444 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
445 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
446 valid_mode,
447 0, sizeof( buf ),
448 buf, 0, 0,
449 NULL ) );
Hanno Beckerb06f1932018-12-18 14:04:28 +0000450 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
451 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
452 valid_mode,
453 MBEDTLS_MD_SHA1,
454 0, NULL,
455 0, 0,
456 buf ) );
Hanno Becker046d2022018-12-13 18:07:09 +0000457
458 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
459 mbedtls_rsa_copy( NULL, &ctx ) );
460 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
461 mbedtls_rsa_copy( &ctx, NULL ) );
462
463exit:
464 return;
465}
466/* END_CASE */
467
Paul Bakker33b43f12013-08-20 11:48:36 +0200468/* BEGIN_CASE */
Gilles Peskineab584952021-02-01 17:55:24 +0100469void rsa_init_free( int reinit )
470{
471 mbedtls_rsa_context ctx;
472
473 /* Double free is not explicitly documented to work, but we rely on it
474 * even inside the library so that you can call mbedtls_rsa_free()
475 * unconditionally on an error path without checking whether it has
476 * already been called in the success path. */
477
478 mbedtls_rsa_init( &ctx, 0, 0 );
479 mbedtls_rsa_free( &ctx );
480
481 if( reinit )
482 mbedtls_rsa_init( &ctx, 0, 0 );
483 mbedtls_rsa_free( &ctx );
484
485 /* This test case always succeeds, functionally speaking. A plausible
486 * bug might trigger an invalid pointer dereference or a memory leak. */
487 goto exit;
488}
489/* END_CASE */
490
491/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100492void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100493 int digest, int mod, int radix_P, char * input_P,
494 int radix_Q, char * input_Q, int radix_N,
495 char * input_N, int radix_E, char * input_E,
Ronald Cronaea41df2020-06-26 14:33:03 +0200496 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000497{
Ron Eldore4c5fa72018-11-22 15:47:51 +0200498 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
499 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100501 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200502 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000503
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100504 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
505 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000507
Ron Eldore4c5fa72018-11-22 15:47:51 +0200508 memset( hash_result, 0x00, sizeof( hash_result ) );
509 memset( output, 0x00, sizeof( output ) );
Paul Bakker548957d2013-08-30 10:30:02 +0200510 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000511
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100512 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
513 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
514 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
515 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000516
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100517 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
518 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100519 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000521
Paul Bakker42a29bf2009-07-07 20:18:41 +0000522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100524 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 +0000525
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100526 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
527 MBEDTLS_RSA_PRIVATE, digest, 0,
528 hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200529 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000530 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000531
Ronald Cronaea41df2020-06-26 14:33:03 +0200532 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
533 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000534 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000535
Paul Bakkerbd51b262014-07-10 15:26:12 +0200536exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100537 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
538 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000540}
Paul Bakker33b43f12013-08-20 11:48:36 +0200541/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000542
Paul Bakker33b43f12013-08-20 11:48:36 +0200543/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100544void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100545 int digest, int mod, int radix_N,
546 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100547 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000548{
Ron Eldore4c5fa72018-11-22 15:47:51 +0200549 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000551
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100552 mbedtls_mpi N, E;
553
554 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldore4c5fa72018-11-22 15:47:51 +0200556 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000557
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100558 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
559 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
560 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
561 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000563
Paul Bakker42a29bf2009-07-07 20:18:41 +0000564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100566 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 +0000567
Azim Khand30ca132017-06-09 04:32:58 +0100568 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 +0100569
Paul Bakkerbd51b262014-07-10 15:26:12 +0200570exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100571 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000573}
Paul Bakker33b43f12013-08-20 11:48:36 +0200574/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000575
Paul Bakker821fb082009-07-12 13:26:42 +0000576
Paul Bakker33b43f12013-08-20 11:48:36 +0200577/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100578void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100579 int padding_mode, int mod, int radix_P,
580 char * input_P, int radix_Q, char * input_Q,
581 int radix_N, char * input_N, int radix_E,
Ronald Cronaea41df2020-06-26 14:33:03 +0200582 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000583{
Ron Eldore4c5fa72018-11-22 15:47:51 +0200584 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100586 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200587 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100590 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
591 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000592
Ron Eldore4c5fa72018-11-22 15:47:51 +0200593 memset( output, 0x00, sizeof( output ) );
Paul Bakker548957d2013-08-30 10:30:02 +0200594 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000595
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100596 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
597 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
598 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
599 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000600
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100601 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
602 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100603 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000605
Paul Bakker821fb082009-07-12 13:26:42 +0000606
Hanno Becker8fd55482017-08-23 14:07:48 +0100607 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
608 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
Azim Khand30ca132017-06-09 04:32:58 +0100609 hash_result->len, hash_result->x,
610 output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000611
Paul Bakker821fb082009-07-12 13:26:42 +0000612
Ronald Cronaea41df2020-06-26 14:33:03 +0200613 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
614 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000615
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200616#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100617 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100619 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100620 int res;
Ron Eldore4c5fa72018-11-22 15:47:51 +0200621 memset( output, 0x00, sizeof( output) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100622
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100623 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Azim Khand30ca132017-06-09 04:32:58 +0100625 hash_result->len, hash_result->x, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100626
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100627#if !defined(MBEDTLS_RSA_ALT)
628 TEST_ASSERT( res == 0 );
629#else
630 TEST_ASSERT( ( res == 0 ) ||
631 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
632#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100633
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100634 if( res == 0 )
635 {
Ronald Cronaea41df2020-06-26 14:33:03 +0200636 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron9fde3532020-06-10 11:42:32 +0200637 ctx.len,
Ronald Cronaea41df2020-06-26 14:33:03 +0200638 result_str->len ) == 0 );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100639 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100640 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200641#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100642
Paul Bakkerbd51b262014-07-10 15:26:12 +0200643exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100644 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
645 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000648}
Paul Bakker33b43f12013-08-20 11:48:36 +0200649/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000650
Paul Bakker33b43f12013-08-20 11:48:36 +0200651/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100652void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200653 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100654 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100655 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000656{
Ron Eldore4c5fa72018-11-22 15:47:51 +0200657 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000659
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100660 mbedtls_mpi N, E;
661 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100664 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000665
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100666 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
667 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000668
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100669 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
670 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000672
Paul Bakker821fb082009-07-12 13:26:42 +0000673
Azim Khand30ca132017-06-09 04:32:58 +0100674 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 +0100675
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200676#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100677 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100679 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100680 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100681 int ok;
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200682 size_t olen;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100683
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100684 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Azim Khand30ca132017-06-09 04:32:58 +0100686 &olen, result_str->x, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100687
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100688#if !defined(MBEDTLS_RSA_ALT)
689 TEST_ASSERT( res == 0 );
690#else
691 TEST_ASSERT( ( res == 0 ) ||
692 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
693#endif
694
695 if( res == 0 )
696 {
Azim Khand30ca132017-06-09 04:32:58 +0100697 ok = olen == hash_result->len && memcmp( output, hash_result->x, olen ) == 0;
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100698 if( correct == 0 )
699 TEST_ASSERT( ok == 1 );
700 else
701 TEST_ASSERT( ok == 0 );
702 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100703 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200704#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100705
Paul Bakkerbd51b262014-07-10 15:26:12 +0200706exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100707 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000709}
Paul Bakker33b43f12013-08-20 11:48:36 +0200710/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000711
Paul Bakker33b43f12013-08-20 11:48:36 +0200712/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100713void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100714 int mod, int radix_N, char * input_N,
715 int radix_E, char * input_E,
Ronald Cronaea41df2020-06-26 14:33:03 +0200716 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000717{
Ron Eldore4c5fa72018-11-22 15:47:51 +0200718 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_rsa_context ctx;
Paul Bakker997bbd12011-03-13 15:45:42 +0000720 rnd_pseudo_info rnd_info;
721
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100722 mbedtls_mpi N, E;
723 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
724
Paul Bakker997bbd12011-03-13 15:45:42 +0000725 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldore4c5fa72018-11-22 15:47:51 +0200728 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000729
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100730 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
731 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000732
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100733 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
734 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000736
Paul Bakker42a29bf2009-07-07 20:18:41 +0000737
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100738 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100739 MBEDTLS_RSA_PUBLIC, message_str->len,
740 message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200741 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000742 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000743
Ronald Cronaea41df2020-06-26 14:33:03 +0200744 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
745 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000746 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100747
Paul Bakkerbd51b262014-07-10 15:26:12 +0200748exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100749 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000751}
Paul Bakker33b43f12013-08-20 11:48:36 +0200752/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000753
Paul Bakker33b43f12013-08-20 11:48:36 +0200754/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100755void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100756 int mod, int radix_N, char * input_N,
757 int radix_E, char * input_E,
Ronald Cronaea41df2020-06-26 14:33:03 +0200758 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000759{
Ron Eldore4c5fa72018-11-22 15:47:51 +0200760 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000762
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100763 mbedtls_mpi N, E;
764
765 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldore4c5fa72018-11-22 15:47:51 +0200767 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000768
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100769 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
770 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000771
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100772 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
773 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000775
Paul Bakkera6656852010-07-18 19:47:14 +0000776
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100777 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL,
Azim Khand30ca132017-06-09 04:32:58 +0100778 MBEDTLS_RSA_PUBLIC, message_str->len,
779 message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200780 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000781 {
Paul Bakkera6656852010-07-18 19:47:14 +0000782
Ronald Cronaea41df2020-06-26 14:33:03 +0200783 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
784 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000785 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100786
Paul Bakkerbd51b262014-07-10 15:26:12 +0200787exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100788 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000790}
Paul Bakker33b43f12013-08-20 11:48:36 +0200791/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000792
Paul Bakker33b43f12013-08-20 11:48:36 +0200793/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100794void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100795 int mod, int radix_P, char * input_P,
796 int radix_Q, char * input_Q, int radix_N,
797 char * input_N, int radix_E, char * input_E,
Ronald Cronaea41df2020-06-26 14:33:03 +0200798 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100799 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000800{
Ron Eldore4c5fa72018-11-22 15:47:51 +0200801 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000803 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200804 rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100805 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000806
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100807 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
808 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000811
Ron Eldore4c5fa72018-11-22 15:47:51 +0200812 memset( output, 0x00, sizeof( output ) );
Paul Bakker548957d2013-08-30 10:30:02 +0200813 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000814
Paul Bakker42a29bf2009-07-07 20:18:41 +0000815
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100816 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
817 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
818 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
819 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000820
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100821 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
822 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100823 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000825
Paul Bakker69998dd2009-07-11 19:15:20 +0000826 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000827
Azim Khand30ca132017-06-09 04:32:58 +0100828 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str->x, output, max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200829 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000830 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000831
Ronald Cronaea41df2020-06-26 14:33:03 +0200832 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron9fde3532020-06-10 11:42:32 +0200833 output_len,
Ronald Cronaea41df2020-06-26 14:33:03 +0200834 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000835 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000836
Paul Bakkerbd51b262014-07-10 15:26:12 +0200837exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100838 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
839 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000841}
Paul Bakker33b43f12013-08-20 11:48:36 +0200842/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000843
Paul Bakker33b43f12013-08-20 11:48:36 +0200844/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100845void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100846 char * input_N, int radix_E, char * input_E,
Ronald Cronaea41df2020-06-26 14:33:03 +0200847 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000848{
Ron Eldore4c5fa72018-11-22 15:47:51 +0200849 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000851
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100852 mbedtls_mpi N, E;
853
854 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
856 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldore4c5fa72018-11-22 15:47:51 +0200857 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000858
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100859 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
860 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000861
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100862 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
863 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000865
Paul Bakker821fb082009-07-12 13:26:42 +0000866
Azim Khand30ca132017-06-09 04:32:58 +0100867 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200868 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000869 {
Paul Bakker821fb082009-07-12 13:26:42 +0000870
Ronald Cronaea41df2020-06-26 14:33:03 +0200871 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
872 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000873 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100874
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100875 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200877 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100881
Ron Eldore4c5fa72018-11-22 15:47:51 +0200882 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100883 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100884 if( result == 0 )
885 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100886
Ronald Cronaea41df2020-06-26 14:33:03 +0200887 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
888 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100889 }
890
Paul Bakkerbd51b262014-07-10 15:26:12 +0200891exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100892 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_rsa_free( &ctx );
894 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000895}
Paul Bakker33b43f12013-08-20 11:48:36 +0200896/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000897
Paul Bakker33b43f12013-08-20 11:48:36 +0200898/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100899void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100900 char * input_P, int radix_Q, char * input_Q,
901 int radix_N, char * input_N, int radix_E,
Ronald Cronaea41df2020-06-26 14:33:03 +0200902 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100903 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000904{
Ron Eldore4c5fa72018-11-22 15:47:51 +0200905 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100907 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200908 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200909 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000910
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100911 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
912 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
914 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000915
Paul Bakker548957d2013-08-30 10:30:02 +0200916 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000917
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100918 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
919 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
920 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
921 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000922
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100923 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
924 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100925 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000927
Paul Bakker821fb082009-07-12 13:26:42 +0000928
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200929 /* repeat three times to test updating of blinding values */
930 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000931 {
Ron Eldore4c5fa72018-11-22 15:47:51 +0200932 memset( output, 0x00, sizeof( output ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100934 message_str->x, output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200935 if( result == 0 )
936 {
Paul Bakker821fb082009-07-12 13:26:42 +0000937
Ronald Cronaea41df2020-06-26 14:33:03 +0200938 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron9fde3532020-06-10 11:42:32 +0200939 ctx.len,
Ronald Cronaea41df2020-06-26 14:33:03 +0200940 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200941 }
Paul Bakker821fb082009-07-12 13:26:42 +0000942 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000943
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100944 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200946 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100950
Ron Eldore4c5fa72018-11-22 15:47:51 +0200951 memset( output, 0x00, sizeof( output ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100953 message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100954 if( result == 0 )
955 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100956
Ronald Cronaea41df2020-06-26 14:33:03 +0200957 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron9fde3532020-06-10 11:42:32 +0200958 ctx2.len,
Ronald Cronaea41df2020-06-26 14:33:03 +0200959 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100960 }
961
Paul Bakkerbd51b262014-07-10 15:26:12 +0200962exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100963 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
964 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000967}
Paul Bakker33b43f12013-08-20 11:48:36 +0200968/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000969
Paul Bakker33b43f12013-08-20 11:48:36 +0200970/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100971void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_rsa_context ctx;
974 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000977}
Paul Bakker33b43f12013-08-20 11:48:36 +0200978/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000979
Paul Bakker33b43f12013-08-20 11:48:36 +0200980/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100981void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
982 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000983{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100985 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000986
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100987 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000989
Paul Bakker33b43f12013-08-20 11:48:36 +0200990 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000991 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100992 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000993 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200994 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000995 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100996 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000997 }
998
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100999 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001001
Paul Bakkerbd51b262014-07-10 15:26:12 +02001002exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001003 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001005}
Paul Bakker33b43f12013-08-20 11:48:36 +02001006/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001007
Paul Bakker33b43f12013-08-20 11:48:36 +02001008/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001009void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
1010 int radix_Q, char * input_Q, int radix_N,
1011 char * input_N, int radix_E, char * input_E,
1012 int radix_D, char * input_D, int radix_DP,
1013 char * input_DP, int radix_DQ,
1014 char * input_DQ, int radix_QP,
1015 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +00001016{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +00001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001020
Paul Bakker33b43f12013-08-20 11:48:36 +02001021 ctx.len = mod / 8;
1022 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001025 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001026 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001029 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001030 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001033 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001034 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001037 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001038 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001041 }
Hanno Becker131134f2017-08-23 08:31:07 +01001042#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +02001043 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001046 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001047 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001050 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001051 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001054 }
Hanno Becker131134f2017-08-23 08:31:07 +01001055#else
1056 ((void) radix_DP); ((void) input_DP);
1057 ((void) radix_DQ); ((void) input_DQ);
1058 ((void) radix_QP); ((void) input_QP);
1059#endif
Paul Bakker821fb082009-07-12 13:26:42 +00001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001062
Paul Bakkerbd51b262014-07-10 15:26:12 +02001063exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001065}
Paul Bakker33b43f12013-08-20 11:48:36 +02001066/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001067
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001068/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001069void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
1070 int radix_Epub, char * input_Epub, int radix_P,
1071 char * input_P, int radix_Q, char * input_Q,
1072 int radix_N, char * input_N, int radix_E,
1073 char * input_E, int radix_D, char * input_D,
1074 int radix_DP, char * input_DP, int radix_DQ,
1075 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001076 int result )
1077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1081 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001082
1083 pub.len = mod / 8;
1084 prv.len = mod / 8;
1085
1086 if( strlen( input_Npub ) )
1087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001089 }
1090 if( strlen( input_Epub ) )
1091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001093 }
1094
1095 if( strlen( input_P ) )
1096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001098 }
1099 if( strlen( input_Q ) )
1100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001102 }
1103 if( strlen( input_N ) )
1104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001106 }
1107 if( strlen( input_E ) )
1108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001110 }
1111 if( strlen( input_D ) )
1112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001114 }
Hanno Becker131134f2017-08-23 08:31:07 +01001115#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001116 if( strlen( input_DP ) )
1117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001119 }
1120 if( strlen( input_DQ ) )
1121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001123 }
1124 if( strlen( input_QP ) )
1125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001127 }
Hanno Becker131134f2017-08-23 08:31:07 +01001128#else
1129 ((void) radix_DP); ((void) input_DP);
1130 ((void) radix_DQ); ((void) input_DQ);
1131 ((void) radix_QP); ((void) input_QP);
1132#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001135
1136exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 mbedtls_rsa_free( &pub );
1138 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001139}
1140/* END_CASE */
1141
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001142/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001144{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 mbedtls_rsa_context ctx;
1146 mbedtls_entropy_context entropy;
1147 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001148 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001149
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001150 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001152 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001153
Hanno Beckera47023e2017-12-22 17:08:03 +00001154 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1155 &entropy, (const unsigned char *) pers,
1156 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001159 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001162 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001163 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001164
Paul Bakkerbd51b262014-07-10 15:26:12 +02001165exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_rsa_free( &ctx );
1167 mbedtls_ctr_drbg_free( &ctr_drbg );
1168 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001169}
Paul Bakker33b43f12013-08-20 11:48:36 +02001170/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001171
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001172/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001173void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001174 int radix_D, char *input_D,
1175 int radix_E, char *input_E,
1176 int radix_P, char *output_P,
1177 int radix_Q, char *output_Q,
1178 int corrupt, int result )
1179{
1180 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1181
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001182 mbedtls_mpi_init( &N );
1183 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1184 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1185 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1186
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001187 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1188 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1189 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1190 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1191 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1192
1193 if( corrupt )
1194 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1195
1196 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001197 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001198
1199 if( !corrupt )
1200 {
1201 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1202 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1203 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1204 }
1205
1206exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001207 mbedtls_mpi_free( &N );
1208 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1209 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1210 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001211}
1212/* END_CASE */
1213
Hanno Becker6b4ce492017-08-23 11:00:21 +01001214/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001215void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1216 int radix_Q, char *input_Q,
1217 int radix_E, char *input_E,
1218 int radix_D, char *output_D,
1219 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001220{
1221 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1222
1223 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1224 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1225 mbedtls_mpi_init( &E );
1226 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1227
1228 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1229 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1230 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1231 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1232
1233 if( corrupt )
1234 {
1235 /* Make E even */
1236 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1237 }
1238
1239 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001240 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1241 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001242
1243 if( !corrupt )
1244 {
1245 /*
1246 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1247 */
1248
1249 /* Replace P,Q by P-1, Q-1 */
1250 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1251 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1252
1253 /* Check D == Dp modulo P-1 */
1254 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1255 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1256 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1257
1258 /* Check D == Dp modulo Q-1 */
1259 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1260 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1261 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1262 }
1263
1264exit:
1265
1266 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1267 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1268 mbedtls_mpi_free( &E );
1269 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1270}
1271/* END_CASE */
1272
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001273/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001274void mbedtls_rsa_import( int radix_N, char *input_N,
1275 int radix_P, char *input_P,
1276 int radix_Q, char *input_Q,
1277 int radix_D, char *input_D,
1278 int radix_E, char *input_E,
1279 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001280 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001281 int res_check,
1282 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001283{
1284 mbedtls_mpi N, P, Q, D, E;
1285 mbedtls_rsa_context ctx;
1286
Hanno Beckere1582a82017-09-29 11:51:05 +01001287 /* Buffers used for encryption-decryption test */
1288 unsigned char *buf_orig = NULL;
1289 unsigned char *buf_enc = NULL;
1290 unsigned char *buf_dec = NULL;
1291
Hanno Beckerc77ab892017-08-23 11:01:06 +01001292 mbedtls_entropy_context entropy;
1293 mbedtls_ctr_drbg_context ctr_drbg;
1294 const char *pers = "test_suite_rsa";
1295
Hanno Becker4d6e8342017-09-29 11:50:18 +01001296 const int have_N = ( strlen( input_N ) > 0 );
1297 const int have_P = ( strlen( input_P ) > 0 );
1298 const int have_Q = ( strlen( input_Q ) > 0 );
1299 const int have_D = ( strlen( input_D ) > 0 );
1300 const int have_E = ( strlen( input_E ) > 0 );
1301
Hanno Beckerc77ab892017-08-23 11:01:06 +01001302 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001303 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001304 mbedtls_rsa_init( &ctx, 0, 0 );
1305
1306 mbedtls_mpi_init( &N );
1307 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1308 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1309
Hanno Beckerd4d60572018-01-10 07:12:01 +00001310 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1311 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1312
Hanno Becker4d6e8342017-09-29 11:50:18 +01001313 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001314 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1315
Hanno Becker4d6e8342017-09-29 11:50:18 +01001316 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001317 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1318
Hanno Becker4d6e8342017-09-29 11:50:18 +01001319 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001320 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1321
Hanno Becker4d6e8342017-09-29 11:50:18 +01001322 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001323 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1324
Hanno Becker4d6e8342017-09-29 11:50:18 +01001325 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001326 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1327
1328 if( !successive )
1329 {
1330 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001331 have_N ? &N : NULL,
1332 have_P ? &P : NULL,
1333 have_Q ? &Q : NULL,
1334 have_D ? &D : NULL,
1335 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001336 }
1337 else
1338 {
1339 /* Import N, P, Q, D, E separately.
1340 * This should make no functional difference. */
1341
1342 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001343 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001344 NULL, NULL, NULL, NULL ) == 0 );
1345
1346 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1347 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001348 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001349 NULL, NULL, NULL ) == 0 );
1350
1351 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1352 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001353 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001354 NULL, NULL ) == 0 );
1355
1356 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1357 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001358 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001359 NULL ) == 0 );
1360
1361 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1362 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001363 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001364 }
1365
Hanno Becker04877a42017-10-11 10:01:33 +01001366 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001367
Hanno Beckere1582a82017-09-29 11:51:05 +01001368 /* On expected success, perform some public and private
1369 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001370 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001371 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001372 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001373 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1374 else
1375 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1376
1377 if( res_check != 0 )
1378 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001379
1380 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1381 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1382 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1383 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1384 goto exit;
1385
1386 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1387 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1388
1389 /* Make sure the number we're generating is smaller than the modulus */
1390 buf_orig[0] = 0x00;
1391
1392 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1393
1394 if( is_priv )
1395 {
1396 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1397 &ctr_drbg, buf_enc,
1398 buf_dec ) == 0 );
1399
1400 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1401 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1402 }
1403 }
1404
Hanno Beckerc77ab892017-08-23 11:01:06 +01001405exit:
1406
Hanno Beckere1582a82017-09-29 11:51:05 +01001407 mbedtls_free( buf_orig );
1408 mbedtls_free( buf_enc );
1409 mbedtls_free( buf_dec );
1410
Hanno Beckerc77ab892017-08-23 11:01:06 +01001411 mbedtls_rsa_free( &ctx );
1412
1413 mbedtls_ctr_drbg_free( &ctr_drbg );
1414 mbedtls_entropy_free( &entropy );
1415
1416 mbedtls_mpi_free( &N );
1417 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1418 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1419}
1420/* END_CASE */
1421
Hanno Becker417f2d62017-08-23 11:44:51 +01001422/* BEGIN_CASE */
1423void mbedtls_rsa_export( int radix_N, char *input_N,
1424 int radix_P, char *input_P,
1425 int radix_Q, char *input_Q,
1426 int radix_D, char *input_D,
1427 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001428 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001429 int successive )
1430{
1431 /* Original MPI's with which we set up the RSA context */
1432 mbedtls_mpi N, P, Q, D, E;
1433
1434 /* Exported MPI's */
1435 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1436
1437 const int have_N = ( strlen( input_N ) > 0 );
1438 const int have_P = ( strlen( input_P ) > 0 );
1439 const int have_Q = ( strlen( input_Q ) > 0 );
1440 const int have_D = ( strlen( input_D ) > 0 );
1441 const int have_E = ( strlen( input_E ) > 0 );
1442
Hanno Becker417f2d62017-08-23 11:44:51 +01001443 mbedtls_rsa_context ctx;
1444
1445 mbedtls_rsa_init( &ctx, 0, 0 );
1446
1447 mbedtls_mpi_init( &N );
1448 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1449 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1450
1451 mbedtls_mpi_init( &Ne );
1452 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1453 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1454
1455 /* Setup RSA context */
1456
1457 if( have_N )
1458 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1459
1460 if( have_P )
1461 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1462
1463 if( have_Q )
1464 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1465
1466 if( have_D )
1467 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1468
1469 if( have_E )
1470 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1471
1472 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1473 strlen( input_N ) ? &N : NULL,
1474 strlen( input_P ) ? &P : NULL,
1475 strlen( input_Q ) ? &Q : NULL,
1476 strlen( input_D ) ? &D : NULL,
1477 strlen( input_E ) ? &E : NULL ) == 0 );
1478
Hanno Becker7f25f852017-10-10 16:56:22 +01001479 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001480
1481 /*
1482 * Export parameters and compare to original ones.
1483 */
1484
1485 /* N and E must always be present. */
1486 if( !successive )
1487 {
1488 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1489 }
1490 else
1491 {
1492 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1493 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1494 }
1495 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1496 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1497
1498 /* If we were providing enough information to setup a complete private context,
1499 * we expect to be able to export all core parameters. */
1500
1501 if( is_priv )
1502 {
1503 if( !successive )
1504 {
1505 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1506 &De, NULL ) == 0 );
1507 }
1508 else
1509 {
1510 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1511 NULL, NULL ) == 0 );
1512 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1513 NULL, NULL ) == 0 );
1514 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1515 &De, NULL ) == 0 );
1516 }
1517
1518 if( have_P )
1519 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1520
1521 if( have_Q )
1522 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1523
1524 if( have_D )
1525 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1526
1527 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001528 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1529 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001530 }
1531
1532exit:
1533
1534 mbedtls_rsa_free( &ctx );
1535
1536 mbedtls_mpi_free( &N );
1537 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1538 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1539
1540 mbedtls_mpi_free( &Ne );
1541 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1542 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1543}
1544/* END_CASE */
1545
Manuel Pégourié-Gonnardf2c6e342020-05-20 10:34:25 +02001546/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001547void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1548 int radix_P, char *input_P,
1549 int radix_Q, char *input_Q,
1550 int radix_D, char *input_D,
1551 int radix_E, char *input_E,
1552 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001553{
1554 /* Original MPI's with which we set up the RSA context */
1555 mbedtls_mpi N, P, Q, D, E;
1556
1557 const int have_N = ( strlen( input_N ) > 0 );
1558 const int have_P = ( strlen( input_P ) > 0 );
1559 const int have_Q = ( strlen( input_Q ) > 0 );
1560 const int have_D = ( strlen( input_D ) > 0 );
1561 const int have_E = ( strlen( input_E ) > 0 );
1562
1563 mbedtls_entropy_context entropy;
1564 mbedtls_ctr_drbg_context ctr_drbg;
1565 const char *pers = "test_suite_rsa";
1566
1567 mbedtls_mpi_init( &N );
1568 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1569 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1570
1571 mbedtls_ctr_drbg_init( &ctr_drbg );
1572 mbedtls_entropy_init( &entropy );
1573 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1574 &entropy, (const unsigned char *) pers,
1575 strlen( pers ) ) == 0 );
1576
1577 if( have_N )
1578 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1579
1580 if( have_P )
1581 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1582
1583 if( have_Q )
1584 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1585
1586 if( have_D )
1587 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1588
1589 if( have_E )
1590 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1591
Hanno Becker750e8b42017-08-25 07:54:27 +01001592 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1593 have_P ? &P : NULL,
1594 have_Q ? &Q : NULL,
1595 have_D ? &D : NULL,
1596 have_E ? &E : NULL,
1597 prng ? mbedtls_ctr_drbg_random : NULL,
1598 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001599exit:
1600
1601 mbedtls_ctr_drbg_free( &ctr_drbg );
1602 mbedtls_entropy_free( &entropy );
1603
1604 mbedtls_mpi_free( &N );
1605 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1606 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1607}
1608/* END_CASE */
1609
Hanno Beckerc77ab892017-08-23 11:01:06 +01001610/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001611void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1612 data_t *input_Q, data_t *input_D,
1613 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001614 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001615{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001616 /* Exported buffers */
Ron Eldore4c5fa72018-11-22 15:47:51 +02001617 unsigned char bufNe[256];
1618 unsigned char bufPe[128];
1619 unsigned char bufQe[128];
1620 unsigned char bufDe[256];
1621 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001622
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001623 mbedtls_rsa_context ctx;
1624
1625 mbedtls_rsa_init( &ctx, 0, 0 );
1626
1627 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001628 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001629 input_N->len ? input_N->x : NULL, input_N->len,
1630 input_P->len ? input_P->x : NULL, input_P->len,
1631 input_Q->len ? input_Q->x : NULL, input_Q->len,
1632 input_D->len ? input_D->x : NULL, input_D->len,
1633 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001634
Hanno Becker7f25f852017-10-10 16:56:22 +01001635 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001636
1637 /*
1638 * Export parameters and compare to original ones.
1639 */
1640
1641 /* N and E must always be present. */
1642 if( !successive )
1643 {
Azim Khand30ca132017-06-09 04:32:58 +01001644 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001645 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001646 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001647 }
1648 else
1649 {
Azim Khand30ca132017-06-09 04:32:58 +01001650 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001651 NULL, 0, NULL, 0, NULL, 0,
1652 NULL, 0 ) == 0 );
1653 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1654 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001655 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001656 }
Azim Khand30ca132017-06-09 04:32:58 +01001657 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1658 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001659
1660 /* If we were providing enough information to setup a complete private context,
1661 * we expect to be able to export all core parameters. */
1662
1663 if( is_priv )
1664 {
1665 if( !successive )
1666 {
1667 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001668 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1669 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1670 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001671 NULL, 0 ) == 0 );
1672 }
1673 else
1674 {
1675 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001676 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001677 NULL, 0, NULL, 0,
1678 NULL, 0 ) == 0 );
1679
1680 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001681 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001682 NULL, 0, NULL, 0 ) == 0 );
1683
Azim Khand30ca132017-06-09 04:32:58 +01001684 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1685 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001686 NULL, 0 ) == 0 );
1687 }
1688
Azim Khand30ca132017-06-09 04:32:58 +01001689 if( input_P->len )
1690 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001691
Azim Khand30ca132017-06-09 04:32:58 +01001692 if( input_Q->len )
1693 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001694
Azim Khand30ca132017-06-09 04:32:58 +01001695 if( input_D->len )
1696 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001697
1698 }
1699
1700exit:
1701 mbedtls_rsa_free( &ctx );
1702}
1703/* END_CASE */
1704
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001705/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001706void mbedtls_rsa_import_raw( data_t *input_N,
1707 data_t *input_P, data_t *input_Q,
1708 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001709 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001710 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001711 int res_check,
1712 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001713{
Hanno Beckere1582a82017-09-29 11:51:05 +01001714 /* Buffers used for encryption-decryption test */
1715 unsigned char *buf_orig = NULL;
1716 unsigned char *buf_enc = NULL;
1717 unsigned char *buf_dec = NULL;
1718
Hanno Beckerc77ab892017-08-23 11:01:06 +01001719 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001720 mbedtls_entropy_context entropy;
1721 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001722
Hanno Beckerc77ab892017-08-23 11:01:06 +01001723 const char *pers = "test_suite_rsa";
1724
1725 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001726 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001727 mbedtls_rsa_init( &ctx, 0, 0 );
1728
Hanno Beckerc77ab892017-08-23 11:01:06 +01001729 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1730 &entropy, (const unsigned char *) pers,
1731 strlen( pers ) ) == 0 );
1732
Hanno Beckerc77ab892017-08-23 11:01:06 +01001733 if( !successive )
1734 {
1735 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001736 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1737 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1738 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1739 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1740 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001741 }
1742 else
1743 {
1744 /* Import N, P, Q, D, E separately.
1745 * This should make no functional difference. */
1746
1747 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001748 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001749 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1750
1751 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1752 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001753 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001754 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1755
1756 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1757 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001758 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001759 NULL, 0, NULL, 0 ) == 0 );
1760
1761 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1762 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001763 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001764 NULL, 0 ) == 0 );
1765
1766 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1767 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001768 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001769 }
1770
Hanno Becker04877a42017-10-11 10:01:33 +01001771 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001772
Hanno Beckere1582a82017-09-29 11:51:05 +01001773 /* On expected success, perform some public and private
1774 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001775 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001776 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001777 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001778 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1779 else
1780 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1781
1782 if( res_check != 0 )
1783 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001784
1785 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1786 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1787 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1788 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1789 goto exit;
1790
1791 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1792 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1793
1794 /* Make sure the number we're generating is smaller than the modulus */
1795 buf_orig[0] = 0x00;
1796
1797 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1798
1799 if( is_priv )
1800 {
1801 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1802 &ctr_drbg, buf_enc,
1803 buf_dec ) == 0 );
1804
1805 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1806 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1807 }
1808 }
1809
Hanno Beckerc77ab892017-08-23 11:01:06 +01001810exit:
1811
Hanno Becker3f3ae852017-10-02 10:08:39 +01001812 mbedtls_free( buf_orig );
1813 mbedtls_free( buf_enc );
1814 mbedtls_free( buf_dec );
1815
Hanno Beckerc77ab892017-08-23 11:01:06 +01001816 mbedtls_rsa_free( &ctx );
1817
1818 mbedtls_ctr_drbg_free( &ctr_drbg );
1819 mbedtls_entropy_free( &entropy );
1820
1821}
1822/* END_CASE */
1823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001825void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001826{
Andres AG93012e82016-09-09 09:10:28 +01001827 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001828}
Paul Bakker33b43f12013-08-20 11:48:36 +02001829/* END_CASE */