blob: 1313f55a4f36ded3532e242946d3ca44cbc2b27c [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/rsa.h"
Chris Jones66a4cd42021-03-09 16:04:12 +00003#include "rsa_alt_helpers.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/md2.h"
5#include "mbedtls/md4.h"
6#include "mbedtls/md5.h"
7#include "mbedtls/sha1.h"
8#include "mbedtls/sha256.h"
9#include "mbedtls/sha512.h"
10#include "mbedtls/entropy.h"
11#include "mbedtls/ctr_drbg.h"
Hanno Becker47deec42017-07-24 12:27:09 +010012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +000014
Paul Bakker33b43f12013-08-20 11:48:36 +020015/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +020017 * END_DEPENDENCIES
18 */
Paul Bakker5690efc2011-05-26 13:16:06 +000019
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050020/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
21void rsa_invalid_param( )
22{
23 mbedtls_rsa_context ctx;
24 const int valid_padding = MBEDTLS_RSA_PKCS_V21;
25 const int invalid_padding = 42;
26 const int valid_mode = MBEDTLS_RSA_PRIVATE;
27 const int invalid_mode = 42;
28 unsigned char buf[42] = { 0 };
29 size_t olen;
30
31 TEST_INVALID_PARAM( mbedtls_rsa_init( NULL, valid_padding, 0 ) );
32 TEST_INVALID_PARAM( mbedtls_rsa_init( &ctx, invalid_padding, 0 ) );
33 TEST_VALID_PARAM( mbedtls_rsa_free( NULL ) );
34
35 /* No more variants because only the first argument must be non-NULL. */
36 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
37 mbedtls_rsa_import( NULL, NULL, NULL,
38 NULL, NULL, NULL ) );
39 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
40 mbedtls_rsa_import_raw( NULL,
41 NULL, 0,
42 NULL, 0,
43 NULL, 0,
44 NULL, 0,
45 NULL, 0 ) );
46
47 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
48 mbedtls_rsa_complete( NULL ) );
49
50 /* No more variants because only the first argument must be non-NULL. */
51 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
52 mbedtls_rsa_export( NULL, NULL, NULL,
53 NULL, NULL, NULL ) );
54 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
55 mbedtls_rsa_export_raw( NULL,
56 NULL, 0,
57 NULL, 0,
58 NULL, 0,
59 NULL, 0,
60 NULL, 0 ) );
61 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
62 mbedtls_rsa_export_crt( NULL, NULL, NULL, NULL ) );
63
64 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( NULL,
65 valid_padding, 0 ) );
66 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( &ctx,
67 invalid_padding, 0 ) );
68
69 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +020070 mbedtls_rsa_gen_key( NULL,
71 mbedtls_test_rnd_std_rand,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072 NULL, 0, 0 ) );
73 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
74 mbedtls_rsa_gen_key( &ctx, NULL,
75 NULL, 0, 0 ) );
76
77 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
78 mbedtls_rsa_check_pubkey( NULL ) );
79 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
80 mbedtls_rsa_check_privkey( NULL ) );
81
82 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
83 mbedtls_rsa_check_pub_priv( NULL, &ctx ) );
84 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
85 mbedtls_rsa_check_pub_priv( &ctx, NULL ) );
86
87 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
88 mbedtls_rsa_public( NULL, buf, buf ) );
89 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
90 mbedtls_rsa_public( &ctx, NULL, buf ) );
91 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
92 mbedtls_rsa_public( &ctx, buf, NULL ) );
93
94 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
95 mbedtls_rsa_private( NULL, NULL, NULL,
96 buf, buf ) );
97 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
98 mbedtls_rsa_private( &ctx, NULL, NULL,
99 NULL, buf ) );
100 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
101 mbedtls_rsa_private( &ctx, NULL, NULL,
102 buf, NULL ) );
103
104 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
105 mbedtls_rsa_pkcs1_encrypt( NULL, NULL, NULL,
106 valid_mode,
107 sizeof( buf ), buf,
108 buf ) );
109 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
110 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
111 invalid_mode,
112 sizeof( buf ), buf,
113 buf ) );
114 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
115 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
116 valid_mode,
117 sizeof( buf ), NULL,
118 buf ) );
119 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
120 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
121 valid_mode,
122 sizeof( buf ), buf,
123 NULL ) );
124
125 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
126 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
127 NULL,
128 valid_mode,
129 sizeof( buf ), buf,
130 buf ) );
131 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
132 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
133 NULL,
134 invalid_mode,
135 sizeof( buf ), buf,
136 buf ) );
137 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
138 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
139 NULL,
140 valid_mode,
141 sizeof( buf ), NULL,
142 buf ) );
143 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
144 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
145 NULL,
146 valid_mode,
147 sizeof( buf ), buf,
148 NULL ) );
149
150 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
151 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
152 valid_mode,
153 buf, sizeof( buf ),
154 sizeof( buf ), buf,
155 buf ) );
156 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
157 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
158 invalid_mode,
159 buf, sizeof( buf ),
160 sizeof( buf ), buf,
161 buf ) );
162 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
163 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
164 valid_mode,
165 NULL, sizeof( buf ),
166 sizeof( buf ), buf,
167 buf ) );
168 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
169 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
170 valid_mode,
171 buf, sizeof( buf ),
172 sizeof( buf ), NULL,
173 buf ) );
174 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
175 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
176 valid_mode,
177 buf, sizeof( buf ),
178 sizeof( buf ), buf,
179 NULL ) );
180
181 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
182 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100183 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500184 buf, buf, 42 ) );
185 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
186 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100187 NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500188 buf, buf, 42 ) );
189 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
190 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100191 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500192 NULL, buf, 42 ) );
193 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
194 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100195 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500196 buf, NULL, 42 ) );
197
198 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
199 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
200 NULL,
201 valid_mode, &olen,
202 buf, buf, 42 ) );
203 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
204 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
205 NULL,
206 invalid_mode, &olen,
207 buf, buf, 42 ) );
208 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
209 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
210 NULL,
211 valid_mode, NULL,
212 buf, buf, 42 ) );
213 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
214 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
215 NULL,
216 valid_mode, &olen,
217 NULL, buf, 42 ) );
218 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
219 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
220 NULL,
221 valid_mode, &olen,
222 buf, NULL, 42 ) );
223
224 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
225 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
226 valid_mode,
227 buf, sizeof( buf ),
228 &olen,
229 buf, buf, 42 ) );
230 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
231 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
232 invalid_mode,
233 buf, sizeof( buf ),
234 &olen,
235 buf, buf, 42 ) );
236 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
237 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
238 valid_mode,
239 NULL, sizeof( buf ),
240 NULL,
241 buf, buf, 42 ) );
242 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
243 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
244 valid_mode,
245 buf, sizeof( buf ),
246 &olen,
247 NULL, buf, 42 ) );
248 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
249 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
250 valid_mode,
251 buf, sizeof( buf ),
252 &olen,
253 buf, NULL, 42 ) );
254
255 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
256 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
257 valid_mode,
258 0, sizeof( buf ), buf,
259 buf ) );
260 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
261 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
262 invalid_mode,
263 0, sizeof( buf ), buf,
264 buf ) );
265 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
266 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
267 valid_mode,
268 0, sizeof( buf ), NULL,
269 buf ) );
270 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
271 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
272 valid_mode,
273 0, sizeof( buf ), buf,
274 NULL ) );
275 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
276 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
277 valid_mode,
278 MBEDTLS_MD_SHA1,
279 0, NULL,
280 buf ) );
281
282 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
283 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
284 valid_mode,
285 0, sizeof( buf ), buf,
286 buf ) );
287 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
288 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
289 invalid_mode,
290 0, sizeof( buf ), buf,
291 buf ) );
292 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
293 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
294 valid_mode,
295 0, sizeof( buf ), NULL,
296 buf ) );
297 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
298 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
299 valid_mode,
300 0, sizeof( buf ), buf,
301 NULL ) );
302 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
303 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
304 valid_mode,
305 MBEDTLS_MD_SHA1,
306 0, NULL,
307 buf ) );
308
309 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
310 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
311 valid_mode,
312 0, sizeof( buf ), buf,
313 buf ) );
314 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
315 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
316 invalid_mode,
317 0, sizeof( buf ), buf,
318 buf ) );
319 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
320 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
321 valid_mode,
322 0, sizeof( buf ), NULL,
323 buf ) );
324 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
325 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
326 valid_mode,
327 0, sizeof( buf ), buf,
328 NULL ) );
329 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
330 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
331 valid_mode,
332 MBEDTLS_MD_SHA1,
333 0, NULL,
334 buf ) );
335
336 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Cédric Meutera05cbec2020-04-25 15:02:34 +0200337 mbedtls_rsa_rsassa_pss_sign_ext( NULL, NULL, NULL,
338 0, sizeof( buf ), buf,
339 MBEDTLS_RSA_SALT_LEN_ANY,
340 buf ) );
341 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
342 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
343 0, sizeof( buf ), NULL,
344 MBEDTLS_RSA_SALT_LEN_ANY,
345 buf ) );
346 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
347 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
348 0, sizeof( buf ), buf,
349 MBEDTLS_RSA_SALT_LEN_ANY,
350 NULL ) );
351 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
352 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
353 MBEDTLS_MD_SHA1,
354 0, NULL,
355 MBEDTLS_RSA_SALT_LEN_ANY,
356 buf ) );
357
358 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500359 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
360 valid_mode,
361 0, sizeof( buf ), buf,
362 buf ) );
363 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
364 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
365 invalid_mode,
366 0, sizeof( buf ), buf,
367 buf ) );
368 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
369 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
370 valid_mode,
371 0, sizeof( buf ), NULL,
372 buf ) );
373 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
374 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
375 valid_mode,
376 0, sizeof( buf ), buf,
377 NULL ) );
378 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
379 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
380 valid_mode,
381 MBEDTLS_MD_SHA1, 0, NULL,
382 buf ) );
383
384 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
385 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
386 NULL,
387 valid_mode,
388 0, sizeof( buf ), buf,
389 buf ) );
390 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
391 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
392 NULL,
393 invalid_mode,
394 0, sizeof( buf ), buf,
395 buf ) );
396 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
397 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
398 NULL,
399 valid_mode,
400 0, sizeof( buf ),
401 NULL, buf ) );
402 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
403 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
404 NULL,
405 valid_mode,
406 0, sizeof( buf ), buf,
407 NULL ) );
408 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
409 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
410 NULL,
411 valid_mode,
412 MBEDTLS_MD_SHA1,
413 0, NULL,
414 buf ) );
415
416 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
417 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
418 valid_mode,
419 0, sizeof( buf ),
420 buf, buf ) );
421 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
422 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
423 invalid_mode,
424 0, sizeof( buf ),
425 buf, buf ) );
426 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
427 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
428 valid_mode,
429 0, sizeof( buf ),
430 NULL, buf ) );
431 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
432 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
433 valid_mode,
434 0, sizeof( buf ),
435 buf, NULL ) );
436 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
437 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
438 valid_mode,
439 MBEDTLS_MD_SHA1,
440 0, NULL,
441 buf ) );
442
443 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
444 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
445 valid_mode,
446 0, sizeof( buf ),
447 buf,
448 0, 0,
449 buf ) );
450 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
451 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
452 invalid_mode,
453 0, sizeof( buf ),
454 buf,
455 0, 0,
456 buf ) );
457 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
458 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
459 valid_mode,
460 0, sizeof( buf ),
461 NULL, 0, 0,
462 buf ) );
463 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
464 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
465 valid_mode,
466 0, sizeof( buf ),
467 buf, 0, 0,
468 NULL ) );
469 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
470 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
471 valid_mode,
472 MBEDTLS_MD_SHA1,
473 0, NULL,
474 0, 0,
475 buf ) );
476
477 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
478 mbedtls_rsa_copy( NULL, &ctx ) );
479 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
480 mbedtls_rsa_copy( &ctx, NULL ) );
481
482exit:
483 return;
484}
485/* END_CASE */
486
Paul Bakker33b43f12013-08-20 11:48:36 +0200487/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100488void rsa_init_free( int reinit )
489{
490 mbedtls_rsa_context ctx;
491
492 /* Double free is not explicitly documented to work, but we rely on it
493 * even inside the library so that you can call mbedtls_rsa_free()
494 * unconditionally on an error path without checking whether it has
495 * already been called in the success path. */
496
497 mbedtls_rsa_init( &ctx, 0, 0 );
498 mbedtls_rsa_free( &ctx );
499
500 if( reinit )
501 mbedtls_rsa_init( &ctx, 0, 0 );
502 mbedtls_rsa_free( &ctx );
503
504 /* This test case always succeeds, functionally speaking. A plausible
505 * bug might trigger an invalid pointer dereference or a memory leak. */
506 goto exit;
507}
508/* END_CASE */
509
510/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100511void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100512 int digest, int mod, int radix_P, char * input_P,
513 int radix_Q, char * input_Q, int radix_N,
514 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200515 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000516{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200517 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
518 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100520 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200521 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000522
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100523 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
524 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000526
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200527 memset( hash_result, 0x00, sizeof( hash_result ) );
528 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200529 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000530
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100531 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
532 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
533 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
534 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000535
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100536 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
537 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100538 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000540
Paul Bakker42a29bf2009-07-07 20:18:41 +0000541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100543 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 +0000544
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200545 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
546 &rnd_info, MBEDTLS_RSA_PRIVATE, digest,
547 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200548 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000549 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000550
Ronald Cronac6ae352020-06-26 14:33:03 +0200551 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
552 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000553 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000554
Paul Bakkerbd51b262014-07-10 15:26:12 +0200555exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100556 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
557 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000559}
Paul Bakker33b43f12013-08-20 11:48:36 +0200560/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000561
Paul Bakker33b43f12013-08-20 11:48:36 +0200562/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100563void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100564 int digest, int mod, int radix_N,
565 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100566 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000567{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200568 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000570
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100571 mbedtls_mpi N, E;
572
573 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200575 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000576
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100577 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
578 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
579 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
580 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000582
Paul Bakker42a29bf2009-07-07 20:18:41 +0000583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100585 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 +0000586
Azim Khand30ca132017-06-09 04:32:58 +0100587 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 +0100588
Paul Bakkerbd51b262014-07-10 15:26:12 +0200589exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100590 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000592}
Paul Bakker33b43f12013-08-20 11:48:36 +0200593/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000594
Paul Bakker821fb082009-07-12 13:26:42 +0000595
Paul Bakker33b43f12013-08-20 11:48:36 +0200596/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100597void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100598 int padding_mode, int mod, int radix_P,
599 char * input_P, int radix_Q, char * input_Q,
600 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200601 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000602{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200603 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100605 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200606 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100609 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
610 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000611
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200612 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200613 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000614
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100615 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
616 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
617 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
618 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000619
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100620 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
621 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100622 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000624
Paul Bakker821fb082009-07-12 13:26:42 +0000625
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200626 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
627 &rnd_info, MBEDTLS_RSA_PRIVATE,
628 MBEDTLS_MD_NONE, hash_result->len,
629 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000630
Paul Bakker821fb082009-07-12 13:26:42 +0000631
Ronald Cronac6ae352020-06-26 14:33:03 +0200632 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
633 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000634
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200635#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100636 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100638 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100639 int res;
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200640 memset( output, 0x00, sizeof( output) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100641
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100642 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200643 &mbedtls_test_rnd_pseudo_rand, &rnd_info,
644 MBEDTLS_RSA_PRIVATE, hash_result->len,
645 hash_result->x, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100646
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100647#if !defined(MBEDTLS_RSA_ALT)
648 TEST_ASSERT( res == 0 );
649#else
650 TEST_ASSERT( ( res == 0 ) ||
TRodziewiczb579ccd2021-04-13 14:28:28 +0200651 ( res == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100652#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100653
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100654 if( res == 0 )
655 {
Ronald Cronac6ae352020-06-26 14:33:03 +0200656 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200657 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200658 result_str->len ) == 0 );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100659 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100660 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200661#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100662
Paul Bakkerbd51b262014-07-10 15:26:12 +0200663exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100664 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
665 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000668}
Paul Bakker33b43f12013-08-20 11:48:36 +0200669/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000670
Paul Bakker33b43f12013-08-20 11:48:36 +0200671/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100672void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200673 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100674 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100675 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000676{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200677 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000679
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100680 mbedtls_mpi N, E;
681 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100684 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000685
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100686 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
687 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000688
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100689 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
690 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000692
Paul Bakker821fb082009-07-12 13:26:42 +0000693
Azim Khand30ca132017-06-09 04:32:58 +0100694 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 +0100695
Paul Bakkerbd51b262014-07-10 15:26:12 +0200696exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100697 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000699}
Paul Bakker33b43f12013-08-20 11:48:36 +0200700/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000701
Paul Bakker33b43f12013-08-20 11:48:36 +0200702/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100703void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100704 int mod, int radix_N, char * input_N,
705 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200706 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000707{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200708 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200710 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000711
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100712 mbedtls_mpi N, E;
713 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
714
Ronald Cron351f0ee2020-06-10 12:12:18 +0200715 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200718 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000719
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100720 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
721 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000722
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100723 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
724 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000726
Paul Bakker42a29bf2009-07-07 20:18:41 +0000727
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200728 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
729 &mbedtls_test_rnd_pseudo_rand,
730 &rnd_info, MBEDTLS_RSA_PUBLIC,
731 message_str->len, message_str->x,
732 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200733 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000734 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000735
Ronald Cronac6ae352020-06-26 14:33:03 +0200736 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
737 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000738 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100739
Paul Bakkerbd51b262014-07-10 15:26:12 +0200740exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100741 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000743}
Paul Bakker33b43f12013-08-20 11:48:36 +0200744/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000745
Paul Bakker33b43f12013-08-20 11:48:36 +0200746/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100747void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100748 int mod, int radix_N, char * input_N,
749 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200750 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000751{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200752 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000754
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100755 mbedtls_mpi N, E;
756
757 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200759 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000760
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100761 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
762 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000763
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100764 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
765 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000767
Paul Bakkera6656852010-07-18 19:47:14 +0000768
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200769 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
770 NULL, MBEDTLS_RSA_PUBLIC,
771 message_str->len, message_str->x,
772 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200773 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000774 {
Paul Bakkera6656852010-07-18 19:47:14 +0000775
Ronald Cronac6ae352020-06-26 14:33:03 +0200776 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
777 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000778 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100779
Paul Bakkerbd51b262014-07-10 15:26:12 +0200780exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100781 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000783}
Paul Bakker33b43f12013-08-20 11:48:36 +0200784/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000785
Paul Bakker33b43f12013-08-20 11:48:36 +0200786/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100787void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100788 int mod, int radix_P, char * input_P,
789 int radix_Q, char * input_Q, int radix_N,
790 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200791 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100792 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000793{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200794 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000796 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200797 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100798 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000799
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100800 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
801 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000804
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200805 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200806 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000807
Paul Bakker42a29bf2009-07-07 20:18:41 +0000808
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100809 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
810 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
811 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
812 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000813
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100814 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
815 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100816 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000818
Paul Bakker69998dd2009-07-11 19:15:20 +0000819 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000820
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200821 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100822 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200823 &output_len, message_str->x, output,
824 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200825 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000826 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000827
Ronald Cronac6ae352020-06-26 14:33:03 +0200828 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200829 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200830 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000831 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000832
Paul Bakkerbd51b262014-07-10 15:26:12 +0200833exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100834 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
835 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000837}
Paul Bakker33b43f12013-08-20 11:48:36 +0200838/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000839
Paul Bakker33b43f12013-08-20 11:48:36 +0200840/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100841void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100842 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200843 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000844{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200845 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000847
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100848 mbedtls_mpi N, E;
849
850 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
852 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200853 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000854
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100855 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
856 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000857
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100858 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
859 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000861
Paul Bakker821fb082009-07-12 13:26:42 +0000862
Azim Khand30ca132017-06-09 04:32:58 +0100863 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200864 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000865 {
Paul Bakker821fb082009-07-12 13:26:42 +0000866
Ronald Cronac6ae352020-06-26 14:33:03 +0200867 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
868 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000869 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100870
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100871 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200873 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100877
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200878 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100879 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100880 if( result == 0 )
881 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100882
Ronald Cronac6ae352020-06-26 14:33:03 +0200883 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
884 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100885 }
886
Paul Bakkerbd51b262014-07-10 15:26:12 +0200887exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100888 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_rsa_free( &ctx );
890 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000891}
Paul Bakker33b43f12013-08-20 11:48:36 +0200892/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000893
Paul Bakker33b43f12013-08-20 11:48:36 +0200894/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100895void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100896 char * input_P, int radix_Q, char * input_Q,
897 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200898 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100899 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000900{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200901 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100903 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200904 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200905 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000906
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100907 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
908 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
910 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000911
Ronald Cron351f0ee2020-06-10 12:12:18 +0200912 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000913
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100914 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
915 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
916 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
917 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000918
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100919 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
920 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100921 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000923
Paul Bakker821fb082009-07-12 13:26:42 +0000924
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200925 /* repeat three times to test updating of blinding values */
926 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000927 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200928 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200929 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
930 &rnd_info, message_str->x,
931 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200932 if( result == 0 )
933 {
Paul Bakker821fb082009-07-12 13:26:42 +0000934
Ronald Cronac6ae352020-06-26 14:33:03 +0200935 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200936 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200937 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200938 }
Paul Bakker821fb082009-07-12 13:26:42 +0000939 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000940
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100941 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200943 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100947
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200948 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200949 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
950 &rnd_info, message_str->x,
951 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100952 if( result == 0 )
953 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100954
Ronald Cronac6ae352020-06-26 14:33:03 +0200955 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200956 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200957 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100958 }
959
Paul Bakkerbd51b262014-07-10 15:26:12 +0200960exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100961 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
962 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000965}
Paul Bakker33b43f12013-08-20 11:48:36 +0200966/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000967
Paul Bakker33b43f12013-08-20 11:48:36 +0200968/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100969void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000970{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_rsa_context ctx;
972 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000975}
Paul Bakker33b43f12013-08-20 11:48:36 +0200976/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000977
Paul Bakker33b43f12013-08-20 11:48:36 +0200978/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100979void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
980 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000981{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100983 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000984
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100985 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000987
Paul Bakker33b43f12013-08-20 11:48:36 +0200988 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000989 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100990 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000991 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200992 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000993 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100994 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000995 }
996
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100997 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100999
Paul Bakkerbd51b262014-07-10 15:26:12 +02001000exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +01001001 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001003}
Paul Bakker33b43f12013-08-20 11:48:36 +02001004/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001005
Paul Bakker33b43f12013-08-20 11:48:36 +02001006/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001007void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
1008 int radix_Q, char * input_Q, int radix_N,
1009 char * input_N, int radix_E, char * input_E,
1010 int radix_D, char * input_D, int radix_DP,
1011 char * input_DP, int radix_DQ,
1012 char * input_DQ, int radix_QP,
1013 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +00001014{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +00001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001018
Paul Bakker33b43f12013-08-20 11:48:36 +02001019 ctx.len = mod / 8;
1020 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001023 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001024 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001027 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001028 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001031 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001032 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001035 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001036 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001039 }
Hanno Becker131134f2017-08-23 08:31:07 +01001040#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +02001041 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001044 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001045 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001048 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001049 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001052 }
Hanno Becker131134f2017-08-23 08:31:07 +01001053#else
1054 ((void) radix_DP); ((void) input_DP);
1055 ((void) radix_DQ); ((void) input_DQ);
1056 ((void) radix_QP); ((void) input_QP);
1057#endif
Paul Bakker821fb082009-07-12 13:26:42 +00001058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001060
Paul Bakkerbd51b262014-07-10 15:26:12 +02001061exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001063}
Paul Bakker33b43f12013-08-20 11:48:36 +02001064/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001065
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001066/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001067void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
1068 int radix_Epub, char * input_Epub, int radix_P,
1069 char * input_P, int radix_Q, char * input_Q,
1070 int radix_N, char * input_N, int radix_E,
1071 char * input_E, int radix_D, char * input_D,
1072 int radix_DP, char * input_DP, int radix_DQ,
1073 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001074 int result )
1075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1079 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001080
1081 pub.len = mod / 8;
1082 prv.len = mod / 8;
1083
1084 if( strlen( input_Npub ) )
1085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001087 }
1088 if( strlen( input_Epub ) )
1089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001091 }
1092
1093 if( strlen( input_P ) )
1094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001096 }
1097 if( strlen( input_Q ) )
1098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001100 }
1101 if( strlen( input_N ) )
1102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001104 }
1105 if( strlen( input_E ) )
1106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001108 }
1109 if( strlen( input_D ) )
1110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001112 }
Hanno Becker131134f2017-08-23 08:31:07 +01001113#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001114 if( strlen( input_DP ) )
1115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001117 }
1118 if( strlen( input_DQ ) )
1119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001121 }
1122 if( strlen( input_QP ) )
1123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001125 }
Hanno Becker131134f2017-08-23 08:31:07 +01001126#else
1127 ((void) radix_DP); ((void) input_DP);
1128 ((void) radix_DQ); ((void) input_DQ);
1129 ((void) radix_QP); ((void) input_QP);
1130#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001133
1134exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 mbedtls_rsa_free( &pub );
1136 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001137}
1138/* END_CASE */
1139
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001140/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001142{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_rsa_context ctx;
1144 mbedtls_entropy_context entropy;
1145 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001146 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001147
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001148 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001150 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001151
Hanno Beckera47023e2017-12-22 17:08:03 +00001152 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1153 &entropy, (const unsigned char *) pers,
1154 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001157 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001160 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001161 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001162
Paul Bakkerbd51b262014-07-10 15:26:12 +02001163exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 mbedtls_rsa_free( &ctx );
1165 mbedtls_ctr_drbg_free( &ctr_drbg );
1166 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001167}
Paul Bakker33b43f12013-08-20 11:48:36 +02001168/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001169
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001170/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001171void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001172 int radix_D, char *input_D,
1173 int radix_E, char *input_E,
1174 int radix_P, char *output_P,
1175 int radix_Q, char *output_Q,
1176 int corrupt, int result )
1177{
1178 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1179
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001180 mbedtls_mpi_init( &N );
1181 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1182 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1183 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1184
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001185 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1186 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1187 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1188 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1189 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1190
1191 if( corrupt )
1192 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1193
1194 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001195 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001196
1197 if( !corrupt )
1198 {
1199 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1200 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1201 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1202 }
1203
1204exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001205 mbedtls_mpi_free( &N );
1206 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1207 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1208 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001209}
1210/* END_CASE */
1211
Hanno Becker6b4ce492017-08-23 11:00:21 +01001212/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001213void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1214 int radix_Q, char *input_Q,
1215 int radix_E, char *input_E,
1216 int radix_D, char *output_D,
1217 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001218{
1219 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1220
1221 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1222 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1223 mbedtls_mpi_init( &E );
1224 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1225
1226 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1227 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1228 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1229 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1230
1231 if( corrupt )
1232 {
1233 /* Make E even */
1234 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1235 }
1236
1237 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001238 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1239 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001240
1241 if( !corrupt )
1242 {
1243 /*
1244 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1245 */
1246
1247 /* Replace P,Q by P-1, Q-1 */
1248 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1249 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1250
1251 /* Check D == Dp modulo P-1 */
1252 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1253 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1254 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1255
1256 /* Check D == Dp modulo Q-1 */
1257 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1258 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1259 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1260 }
1261
1262exit:
1263
1264 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1265 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1266 mbedtls_mpi_free( &E );
1267 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1268}
1269/* END_CASE */
1270
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001271/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001272void mbedtls_rsa_import( int radix_N, char *input_N,
1273 int radix_P, char *input_P,
1274 int radix_Q, char *input_Q,
1275 int radix_D, char *input_D,
1276 int radix_E, char *input_E,
1277 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001278 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001279 int res_check,
1280 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001281{
1282 mbedtls_mpi N, P, Q, D, E;
1283 mbedtls_rsa_context ctx;
1284
Hanno Beckere1582a82017-09-29 11:51:05 +01001285 /* Buffers used for encryption-decryption test */
1286 unsigned char *buf_orig = NULL;
1287 unsigned char *buf_enc = NULL;
1288 unsigned char *buf_dec = NULL;
1289
Hanno Beckerc77ab892017-08-23 11:01:06 +01001290 mbedtls_entropy_context entropy;
1291 mbedtls_ctr_drbg_context ctr_drbg;
1292 const char *pers = "test_suite_rsa";
1293
Hanno Becker4d6e8342017-09-29 11:50:18 +01001294 const int have_N = ( strlen( input_N ) > 0 );
1295 const int have_P = ( strlen( input_P ) > 0 );
1296 const int have_Q = ( strlen( input_Q ) > 0 );
1297 const int have_D = ( strlen( input_D ) > 0 );
1298 const int have_E = ( strlen( input_E ) > 0 );
1299
Hanno Beckerc77ab892017-08-23 11:01:06 +01001300 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001301 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001302 mbedtls_rsa_init( &ctx, 0, 0 );
1303
1304 mbedtls_mpi_init( &N );
1305 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1306 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1307
Hanno Beckerd4d60572018-01-10 07:12:01 +00001308 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1309 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1310
Hanno Becker4d6e8342017-09-29 11:50:18 +01001311 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001312 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1313
Hanno Becker4d6e8342017-09-29 11:50:18 +01001314 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001315 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1316
Hanno Becker4d6e8342017-09-29 11:50:18 +01001317 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001318 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1319
Hanno Becker4d6e8342017-09-29 11:50:18 +01001320 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001321 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1322
Hanno Becker4d6e8342017-09-29 11:50:18 +01001323 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001324 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1325
1326 if( !successive )
1327 {
1328 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001329 have_N ? &N : NULL,
1330 have_P ? &P : NULL,
1331 have_Q ? &Q : NULL,
1332 have_D ? &D : NULL,
1333 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001334 }
1335 else
1336 {
1337 /* Import N, P, Q, D, E separately.
1338 * This should make no functional difference. */
1339
1340 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001341 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001342 NULL, NULL, NULL, NULL ) == 0 );
1343
1344 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1345 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001346 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001347 NULL, NULL, NULL ) == 0 );
1348
1349 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1350 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001351 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001352 NULL, NULL ) == 0 );
1353
1354 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1355 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001356 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001357 NULL ) == 0 );
1358
1359 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1360 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001361 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001362 }
1363
Hanno Becker04877a42017-10-11 10:01:33 +01001364 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001365
Hanno Beckere1582a82017-09-29 11:51:05 +01001366 /* On expected success, perform some public and private
1367 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001368 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001369 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001370 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001371 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1372 else
1373 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1374
1375 if( res_check != 0 )
1376 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001377
1378 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1379 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1380 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1381 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1382 goto exit;
1383
1384 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1385 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1386
1387 /* Make sure the number we're generating is smaller than the modulus */
1388 buf_orig[0] = 0x00;
1389
1390 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1391
1392 if( is_priv )
1393 {
1394 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1395 &ctr_drbg, buf_enc,
1396 buf_dec ) == 0 );
1397
1398 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1399 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1400 }
1401 }
1402
Hanno Beckerc77ab892017-08-23 11:01:06 +01001403exit:
1404
Hanno Beckere1582a82017-09-29 11:51:05 +01001405 mbedtls_free( buf_orig );
1406 mbedtls_free( buf_enc );
1407 mbedtls_free( buf_dec );
1408
Hanno Beckerc77ab892017-08-23 11:01:06 +01001409 mbedtls_rsa_free( &ctx );
1410
1411 mbedtls_ctr_drbg_free( &ctr_drbg );
1412 mbedtls_entropy_free( &entropy );
1413
1414 mbedtls_mpi_free( &N );
1415 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1416 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1417}
1418/* END_CASE */
1419
Hanno Becker417f2d62017-08-23 11:44:51 +01001420/* BEGIN_CASE */
1421void mbedtls_rsa_export( int radix_N, char *input_N,
1422 int radix_P, char *input_P,
1423 int radix_Q, char *input_Q,
1424 int radix_D, char *input_D,
1425 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001426 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001427 int successive )
1428{
1429 /* Original MPI's with which we set up the RSA context */
1430 mbedtls_mpi N, P, Q, D, E;
1431
1432 /* Exported MPI's */
1433 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1434
1435 const int have_N = ( strlen( input_N ) > 0 );
1436 const int have_P = ( strlen( input_P ) > 0 );
1437 const int have_Q = ( strlen( input_Q ) > 0 );
1438 const int have_D = ( strlen( input_D ) > 0 );
1439 const int have_E = ( strlen( input_E ) > 0 );
1440
Hanno Becker417f2d62017-08-23 11:44:51 +01001441 mbedtls_rsa_context ctx;
1442
1443 mbedtls_rsa_init( &ctx, 0, 0 );
1444
1445 mbedtls_mpi_init( &N );
1446 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1447 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1448
1449 mbedtls_mpi_init( &Ne );
1450 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1451 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1452
1453 /* Setup RSA context */
1454
1455 if( have_N )
1456 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1457
1458 if( have_P )
1459 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1460
1461 if( have_Q )
1462 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1463
1464 if( have_D )
1465 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1466
1467 if( have_E )
1468 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1469
1470 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1471 strlen( input_N ) ? &N : NULL,
1472 strlen( input_P ) ? &P : NULL,
1473 strlen( input_Q ) ? &Q : NULL,
1474 strlen( input_D ) ? &D : NULL,
1475 strlen( input_E ) ? &E : NULL ) == 0 );
1476
Hanno Becker7f25f852017-10-10 16:56:22 +01001477 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001478
1479 /*
1480 * Export parameters and compare to original ones.
1481 */
1482
1483 /* N and E must always be present. */
1484 if( !successive )
1485 {
1486 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1487 }
1488 else
1489 {
1490 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1491 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1492 }
1493 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1494 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1495
1496 /* If we were providing enough information to setup a complete private context,
1497 * we expect to be able to export all core parameters. */
1498
1499 if( is_priv )
1500 {
1501 if( !successive )
1502 {
1503 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1504 &De, NULL ) == 0 );
1505 }
1506 else
1507 {
1508 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1509 NULL, NULL ) == 0 );
1510 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1511 NULL, NULL ) == 0 );
1512 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1513 &De, NULL ) == 0 );
1514 }
1515
1516 if( have_P )
1517 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1518
1519 if( have_Q )
1520 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1521
1522 if( have_D )
1523 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1524
1525 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001526 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1527 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001528 }
1529
1530exit:
1531
1532 mbedtls_rsa_free( &ctx );
1533
1534 mbedtls_mpi_free( &N );
1535 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1536 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1537
1538 mbedtls_mpi_free( &Ne );
1539 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1540 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1541}
1542/* END_CASE */
1543
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001544/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001545void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1546 int radix_P, char *input_P,
1547 int radix_Q, char *input_Q,
1548 int radix_D, char *input_D,
1549 int radix_E, char *input_E,
1550 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001551{
1552 /* Original MPI's with which we set up the RSA context */
1553 mbedtls_mpi N, P, Q, D, E;
1554
1555 const int have_N = ( strlen( input_N ) > 0 );
1556 const int have_P = ( strlen( input_P ) > 0 );
1557 const int have_Q = ( strlen( input_Q ) > 0 );
1558 const int have_D = ( strlen( input_D ) > 0 );
1559 const int have_E = ( strlen( input_E ) > 0 );
1560
1561 mbedtls_entropy_context entropy;
1562 mbedtls_ctr_drbg_context ctr_drbg;
1563 const char *pers = "test_suite_rsa";
1564
1565 mbedtls_mpi_init( &N );
1566 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1567 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1568
1569 mbedtls_ctr_drbg_init( &ctr_drbg );
1570 mbedtls_entropy_init( &entropy );
1571 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1572 &entropy, (const unsigned char *) pers,
1573 strlen( pers ) ) == 0 );
1574
1575 if( have_N )
1576 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1577
1578 if( have_P )
1579 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1580
1581 if( have_Q )
1582 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1583
1584 if( have_D )
1585 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1586
1587 if( have_E )
1588 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1589
Hanno Becker750e8b42017-08-25 07:54:27 +01001590 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1591 have_P ? &P : NULL,
1592 have_Q ? &Q : NULL,
1593 have_D ? &D : NULL,
1594 have_E ? &E : NULL,
1595 prng ? mbedtls_ctr_drbg_random : NULL,
1596 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001597exit:
1598
1599 mbedtls_ctr_drbg_free( &ctr_drbg );
1600 mbedtls_entropy_free( &entropy );
1601
1602 mbedtls_mpi_free( &N );
1603 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1604 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1605}
1606/* END_CASE */
1607
Hanno Beckerc77ab892017-08-23 11:01:06 +01001608/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001609void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1610 data_t *input_Q, data_t *input_D,
1611 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001612 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001613{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001614 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001615 unsigned char bufNe[256];
1616 unsigned char bufPe[128];
1617 unsigned char bufQe[128];
1618 unsigned char bufDe[256];
1619 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001620
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001621 mbedtls_rsa_context ctx;
1622
1623 mbedtls_rsa_init( &ctx, 0, 0 );
1624
1625 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001626 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001627 input_N->len ? input_N->x : NULL, input_N->len,
1628 input_P->len ? input_P->x : NULL, input_P->len,
1629 input_Q->len ? input_Q->x : NULL, input_Q->len,
1630 input_D->len ? input_D->x : NULL, input_D->len,
1631 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001632
Hanno Becker7f25f852017-10-10 16:56:22 +01001633 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001634
1635 /*
1636 * Export parameters and compare to original ones.
1637 */
1638
1639 /* N and E must always be present. */
1640 if( !successive )
1641 {
Azim Khand30ca132017-06-09 04:32:58 +01001642 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001643 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001644 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001645 }
1646 else
1647 {
Azim Khand30ca132017-06-09 04:32:58 +01001648 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001649 NULL, 0, NULL, 0, NULL, 0,
1650 NULL, 0 ) == 0 );
1651 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1652 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001653 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001654 }
Azim Khand30ca132017-06-09 04:32:58 +01001655 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1656 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001657
1658 /* If we were providing enough information to setup a complete private context,
1659 * we expect to be able to export all core parameters. */
1660
1661 if( is_priv )
1662 {
1663 if( !successive )
1664 {
1665 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001666 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1667 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1668 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001669 NULL, 0 ) == 0 );
1670 }
1671 else
1672 {
1673 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001674 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001675 NULL, 0, NULL, 0,
1676 NULL, 0 ) == 0 );
1677
1678 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001679 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001680 NULL, 0, NULL, 0 ) == 0 );
1681
Azim Khand30ca132017-06-09 04:32:58 +01001682 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1683 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001684 NULL, 0 ) == 0 );
1685 }
1686
Azim Khand30ca132017-06-09 04:32:58 +01001687 if( input_P->len )
1688 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001689
Azim Khand30ca132017-06-09 04:32:58 +01001690 if( input_Q->len )
1691 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001692
Azim Khand30ca132017-06-09 04:32:58 +01001693 if( input_D->len )
1694 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001695
1696 }
1697
1698exit:
1699 mbedtls_rsa_free( &ctx );
1700}
1701/* END_CASE */
1702
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001703/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001704void mbedtls_rsa_import_raw( data_t *input_N,
1705 data_t *input_P, data_t *input_Q,
1706 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001707 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001708 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001709 int res_check,
1710 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001711{
Hanno Beckere1582a82017-09-29 11:51:05 +01001712 /* Buffers used for encryption-decryption test */
1713 unsigned char *buf_orig = NULL;
1714 unsigned char *buf_enc = NULL;
1715 unsigned char *buf_dec = NULL;
1716
Hanno Beckerc77ab892017-08-23 11:01:06 +01001717 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001718 mbedtls_entropy_context entropy;
1719 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001720
Hanno Beckerc77ab892017-08-23 11:01:06 +01001721 const char *pers = "test_suite_rsa";
1722
1723 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001724 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001725 mbedtls_rsa_init( &ctx, 0, 0 );
1726
Hanno Beckerc77ab892017-08-23 11:01:06 +01001727 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1728 &entropy, (const unsigned char *) pers,
1729 strlen( pers ) ) == 0 );
1730
Hanno Beckerc77ab892017-08-23 11:01:06 +01001731 if( !successive )
1732 {
1733 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001734 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1735 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1736 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1737 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1738 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001739 }
1740 else
1741 {
1742 /* Import N, P, Q, D, E separately.
1743 * This should make no functional difference. */
1744
1745 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001746 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001747 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1748
1749 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1750 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001751 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001752 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1753
1754 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1755 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001756 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001757 NULL, 0, NULL, 0 ) == 0 );
1758
1759 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1760 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001761 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001762 NULL, 0 ) == 0 );
1763
1764 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1765 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001766 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001767 }
1768
Hanno Becker04877a42017-10-11 10:01:33 +01001769 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001770
Hanno Beckere1582a82017-09-29 11:51:05 +01001771 /* On expected success, perform some public and private
1772 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001773 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001774 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001775 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001776 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1777 else
1778 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1779
1780 if( res_check != 0 )
1781 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001782
1783 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1784 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1785 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1786 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1787 goto exit;
1788
1789 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1790 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1791
1792 /* Make sure the number we're generating is smaller than the modulus */
1793 buf_orig[0] = 0x00;
1794
1795 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1796
1797 if( is_priv )
1798 {
1799 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1800 &ctr_drbg, buf_enc,
1801 buf_dec ) == 0 );
1802
1803 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1804 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1805 }
1806 }
1807
Hanno Beckerc77ab892017-08-23 11:01:06 +01001808exit:
1809
Hanno Becker3f3ae852017-10-02 10:08:39 +01001810 mbedtls_free( buf_orig );
1811 mbedtls_free( buf_enc );
1812 mbedtls_free( buf_dec );
1813
Hanno Beckerc77ab892017-08-23 11:01:06 +01001814 mbedtls_rsa_free( &ctx );
1815
1816 mbedtls_ctr_drbg_free( &ctr_drbg );
1817 mbedtls_entropy_free( &entropy );
1818
1819}
1820/* END_CASE */
1821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001823void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001824{
Andres AG93012e82016-09-09 09:10:28 +01001825 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001826}
Paul Bakker33b43f12013-08-20 11:48:36 +02001827/* END_CASE */