blob: 4096e4d4d0e4ed9f4bb1b679b2664ab095e4219d [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/rsa.h"
Hanno Beckera565f542017-10-11 11:00:19 +01003#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/md2.h"
5#include "mbedtls/md4.h"
6#include "mbedtls/md5.h"
7#include "mbedtls/sha1.h"
8#include "mbedtls/sha256.h"
9#include "mbedtls/sha512.h"
10#include "mbedtls/entropy.h"
11#include "mbedtls/ctr_drbg.h"
Hanno Becker47deec42017-07-24 12:27:09 +010012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +000014
Paul Bakker33b43f12013-08-20 11:48:36 +020015/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +020017 * END_DEPENDENCIES
18 */
Paul Bakker5690efc2011-05-26 13:16:06 +000019
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050020/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
21void rsa_invalid_param( )
22{
23 mbedtls_rsa_context ctx;
24 const int valid_padding = MBEDTLS_RSA_PKCS_V21;
25 const int invalid_padding = 42;
26 const int valid_mode = MBEDTLS_RSA_PRIVATE;
27 const int invalid_mode = 42;
28 unsigned char buf[42] = { 0 };
29 size_t olen;
30
31 TEST_INVALID_PARAM( mbedtls_rsa_init( NULL, valid_padding, 0 ) );
32 TEST_INVALID_PARAM( mbedtls_rsa_init( &ctx, invalid_padding, 0 ) );
33 TEST_VALID_PARAM( mbedtls_rsa_free( NULL ) );
34
35 /* No more variants because only the first argument must be non-NULL. */
36 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
37 mbedtls_rsa_import( NULL, NULL, NULL,
38 NULL, NULL, NULL ) );
39 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
40 mbedtls_rsa_import_raw( NULL,
41 NULL, 0,
42 NULL, 0,
43 NULL, 0,
44 NULL, 0,
45 NULL, 0 ) );
46
47 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
48 mbedtls_rsa_complete( NULL ) );
49
50 /* No more variants because only the first argument must be non-NULL. */
51 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
52 mbedtls_rsa_export( NULL, NULL, NULL,
53 NULL, NULL, NULL ) );
54 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
55 mbedtls_rsa_export_raw( NULL,
56 NULL, 0,
57 NULL, 0,
58 NULL, 0,
59 NULL, 0,
60 NULL, 0 ) );
61 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
62 mbedtls_rsa_export_crt( NULL, NULL, NULL, NULL ) );
63
64 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( NULL,
65 valid_padding, 0 ) );
66 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( &ctx,
67 invalid_padding, 0 ) );
68
69 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Ronald Cron351f0ee2020-06-10 12:12:18 +020070 mbedtls_rsa_gen_key( NULL, mbedtls_test_rnd_std_rand,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050071 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 ) );
75
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 ) );
278 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 ) );
284
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 ) );
305 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 ) );
311
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 ) );
332 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 ) );
338
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 ) );
359 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 ) );
364
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 ) );
389 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 ) );
396
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 ) );
417 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 ) );
423
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 ) );
450 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 ) );
457
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 */
Azim Khan5fcca462018-06-29 11:05:32 +0100469void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100470 int digest, int mod, int radix_P, char * input_P,
471 int radix_Q, char * input_Q, int radix_N,
472 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100473 data_t * result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000474{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200475 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
476 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100478 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200479 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000480
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100481 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
482 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000484
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200485 memset( hash_result, 0x00, sizeof( hash_result ) );
486 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200487 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000488
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100489 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
490 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
491 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
492 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000493
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100494 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
495 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100496 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000498
Paul Bakker42a29bf2009-07-07 20:18:41 +0000499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100501 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 +0000502
Ronald Cron351f0ee2020-06-10 12:12:18 +0200503 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100504 MBEDTLS_RSA_PRIVATE, digest, 0,
505 hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200506 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000507 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000508
Ronald Cron2dbba992020-06-10 11:42:32 +0200509 TEST_ASSERT( mbedtls_test_hexcmp( output, result_hex_str->x,
510 ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000511 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000512
Paul Bakkerbd51b262014-07-10 15:26:12 +0200513exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100514 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
515 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000517}
Paul Bakker33b43f12013-08-20 11:48:36 +0200518/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000519
Paul Bakker33b43f12013-08-20 11:48:36 +0200520/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100521void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100522 int digest, int mod, int radix_N,
523 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100524 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000525{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200526 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000528
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100529 mbedtls_mpi N, E;
530
531 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200533 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000534
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100535 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
536 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
537 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
538 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 TEST_ASSERT( mbedtls_rsa_check_pubkey( &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
Azim Khand30ca132017-06-09 04:32:58 +0100545 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 +0100546
Paul Bakkerbd51b262014-07-10 15:26:12 +0200547exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100548 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000550}
Paul Bakker33b43f12013-08-20 11:48:36 +0200551/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000552
Paul Bakker821fb082009-07-12 13:26:42 +0000553
Paul Bakker33b43f12013-08-20 11:48:36 +0200554/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100555void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100556 int padding_mode, int mod, int radix_P,
557 char * input_P, int radix_Q, char * input_Q,
558 int radix_N, char * input_N, int radix_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100559 char * input_E, data_t * result_hex_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000560{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200561 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100563 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200564 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100567 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
568 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000569
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200570 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200571 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000572
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100573 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
574 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
575 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
576 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000577
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100578 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
579 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100580 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000582
Paul Bakker821fb082009-07-12 13:26:42 +0000583
Ronald Cron351f0ee2020-06-10 12:12:18 +0200584 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
Hanno Becker8fd55482017-08-23 14:07:48 +0100585 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
Azim Khand30ca132017-06-09 04:32:58 +0100586 hash_result->len, hash_result->x,
587 output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000588
Paul Bakker821fb082009-07-12 13:26:42 +0000589
Ronald Cron2dbba992020-06-10 11:42:32 +0200590 TEST_ASSERT( mbedtls_test_hexcmp( output, result_hex_str->x,
591 ctx.len, result_hex_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000592
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200593#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100594 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100596 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100597 int res;
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200598 memset( output, 0x00, sizeof( output) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100599
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100600 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Ronald Cron351f0ee2020-06-10 12:12:18 +0200601 &mbedtls_test_rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Azim Khand30ca132017-06-09 04:32:58 +0100602 hash_result->len, hash_result->x, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100603
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100604#if !defined(MBEDTLS_RSA_ALT)
605 TEST_ASSERT( res == 0 );
606#else
607 TEST_ASSERT( ( res == 0 ) ||
608 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
609#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100610
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100611 if( res == 0 )
612 {
Ronald Cron2dbba992020-06-10 11:42:32 +0200613 TEST_ASSERT( mbedtls_test_hexcmp( output, result_hex_str->x,
614 ctx.len,
615 result_hex_str->len ) == 0 );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100616 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100617 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200618#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100619
Paul Bakkerbd51b262014-07-10 15:26:12 +0200620exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100621 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
622 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000625}
Paul Bakker33b43f12013-08-20 11:48:36 +0200626/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000627
Paul Bakker33b43f12013-08-20 11:48:36 +0200628/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100629void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200630 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100631 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100632 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000633{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200634 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000636
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100637 mbedtls_mpi N, E;
638 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100641 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000642
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100643 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
644 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000645
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100646 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
647 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000649
Paul Bakker821fb082009-07-12 13:26:42 +0000650
Azim Khand30ca132017-06-09 04:32:58 +0100651 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 +0100652
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200653#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100654 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100656 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100657 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100658 int ok;
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200659 size_t olen;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100660
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100661 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Azim Khand30ca132017-06-09 04:32:58 +0100663 &olen, result_str->x, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100664
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100665#if !defined(MBEDTLS_RSA_ALT)
666 TEST_ASSERT( res == 0 );
667#else
668 TEST_ASSERT( ( res == 0 ) ||
669 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
670#endif
671
672 if( res == 0 )
673 {
Azim Khand30ca132017-06-09 04:32:58 +0100674 ok = olen == hash_result->len && memcmp( output, hash_result->x, olen ) == 0;
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100675 if( correct == 0 )
676 TEST_ASSERT( ok == 1 );
677 else
678 TEST_ASSERT( ok == 0 );
679 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100680 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200681#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100682
Paul Bakkerbd51b262014-07-10 15:26:12 +0200683exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100684 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000686}
Paul Bakker33b43f12013-08-20 11:48:36 +0200687/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000688
Paul Bakker33b43f12013-08-20 11:48:36 +0200689/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100690void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100691 int mod, int radix_N, char * input_N,
692 int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100693 data_t * result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000694{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200695 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200697 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000698
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100699 mbedtls_mpi N, E;
700 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
701
Ronald Cron351f0ee2020-06-10 12:12:18 +0200702 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200705 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000706
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100707 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
708 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000709
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100710 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
711 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000713
Paul Bakker42a29bf2009-07-07 20:18:41 +0000714
Ronald Cron351f0ee2020-06-10 12:12:18 +0200715 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100716 MBEDTLS_RSA_PUBLIC, message_str->len,
717 message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200718 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000719 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000720
Ronald Cron2dbba992020-06-10 11:42:32 +0200721 TEST_ASSERT( mbedtls_test_hexcmp( output, result_hex_str->x,
722 ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000723 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100724
Paul Bakkerbd51b262014-07-10 15:26:12 +0200725exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100726 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000728}
Paul Bakker33b43f12013-08-20 11:48:36 +0200729/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000730
Paul Bakker33b43f12013-08-20 11:48:36 +0200731/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100732void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100733 int mod, int radix_N, char * input_N,
734 int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100735 data_t * result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000736{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200737 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000739
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100740 mbedtls_mpi N, E;
741
742 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200744 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000745
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100746 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
747 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000748
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100749 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
750 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000752
Paul Bakkera6656852010-07-18 19:47:14 +0000753
Ronald Cron351f0ee2020-06-10 12:12:18 +0200754 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand, NULL,
Azim Khand30ca132017-06-09 04:32:58 +0100755 MBEDTLS_RSA_PUBLIC, message_str->len,
756 message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200757 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000758 {
Paul Bakkera6656852010-07-18 19:47:14 +0000759
Ronald Cron2dbba992020-06-10 11:42:32 +0200760 TEST_ASSERT( mbedtls_test_hexcmp( output, result_hex_str->x,
761 ctx.len, result_hex_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000762 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100763
Paul Bakkerbd51b262014-07-10 15:26:12 +0200764exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100765 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000767}
Paul Bakker33b43f12013-08-20 11:48:36 +0200768/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000769
Paul Bakker33b43f12013-08-20 11:48:36 +0200770/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100771void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100772 int mod, int radix_P, char * input_P,
773 int radix_Q, char * input_Q, int radix_N,
774 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100775 int max_output, data_t * result_hex_str,
Azim Khand30ca132017-06-09 04:32:58 +0100776 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000777{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200778 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000780 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200781 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100782 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000783
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100784 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
785 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000788
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200789 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200790 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000791
Paul Bakker42a29bf2009-07-07 20:18:41 +0000792
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100793 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
794 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
795 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
796 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000797
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100798 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
799 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100800 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000802
Paul Bakker69998dd2009-07-11 19:15:20 +0000803 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000804
Ronald Cron351f0ee2020-06-10 12:12:18 +0200805 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str->x, output, max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200806 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000807 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000808
Ronald Cron2dbba992020-06-10 11:42:32 +0200809 TEST_ASSERT( mbedtls_test_hexcmp( output, result_hex_str->x,
810 output_len,
811 result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000812 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000813
Paul Bakkerbd51b262014-07-10 15:26:12 +0200814exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100815 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
816 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000818}
Paul Bakker33b43f12013-08-20 11:48:36 +0200819/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000820
Paul Bakker33b43f12013-08-20 11:48:36 +0200821/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100822void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100823 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100824 data_t * result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000825{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200826 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000828
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100829 mbedtls_mpi N, E;
830
831 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
833 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200834 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000835
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100836 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
837 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000838
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100839 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
840 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000842
Paul Bakker821fb082009-07-12 13:26:42 +0000843
Azim Khand30ca132017-06-09 04:32:58 +0100844 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200845 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000846 {
Paul Bakker821fb082009-07-12 13:26:42 +0000847
Ronald Cron2dbba992020-06-10 11:42:32 +0200848 TEST_ASSERT( mbedtls_test_hexcmp( output, result_hex_str->x,
849 ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000850 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100851
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100852 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200854 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100858
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200859 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100860 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100861 if( result == 0 )
862 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100863
Ronald Cron2dbba992020-06-10 11:42:32 +0200864 TEST_ASSERT( mbedtls_test_hexcmp( output, result_hex_str->x,
865 ctx.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100866 }
867
Paul Bakkerbd51b262014-07-10 15:26:12 +0200868exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100869 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_rsa_free( &ctx );
871 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000872}
Paul Bakker33b43f12013-08-20 11:48:36 +0200873/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000874
Paul Bakker33b43f12013-08-20 11:48:36 +0200875/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100876void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100877 char * input_P, int radix_Q, char * input_Q,
878 int radix_N, char * input_N, int radix_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100879 char * input_E, data_t * result_hex_str,
Azim Khand30ca132017-06-09 04:32:58 +0100880 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000881{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200882 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100884 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200885 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200886 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000887
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100888 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
889 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
891 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000892
Ronald Cron351f0ee2020-06-10 12:12:18 +0200893 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000894
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100895 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
896 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
897 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
898 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000899
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100900 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
901 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100902 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000904
Paul Bakker821fb082009-07-12 13:26:42 +0000905
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200906 /* repeat three times to test updating of blinding values */
907 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000908 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200909 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200910 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100911 message_str->x, output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200912 if( result == 0 )
913 {
Paul Bakker821fb082009-07-12 13:26:42 +0000914
Ronald Cron2dbba992020-06-10 11:42:32 +0200915 TEST_ASSERT( mbedtls_test_hexcmp( output, result_hex_str->x,
916 ctx.len,
917 result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200918 }
Paul Bakker821fb082009-07-12 13:26:42 +0000919 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000920
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100921 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200923 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100927
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200928 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200929 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100930 message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100931 if( result == 0 )
932 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100933
Ronald Cron2dbba992020-06-10 11:42:32 +0200934 TEST_ASSERT( mbedtls_test_hexcmp( output, result_hex_str->x,
935 ctx2.len,
936 result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100937 }
938
Paul Bakkerbd51b262014-07-10 15:26:12 +0200939exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100940 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
941 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000944}
Paul Bakker33b43f12013-08-20 11:48:36 +0200945/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000946
Paul Bakker33b43f12013-08-20 11:48:36 +0200947/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100948void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000949{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_rsa_context ctx;
951 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000954}
Paul Bakker33b43f12013-08-20 11:48:36 +0200955/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000956
Paul Bakker33b43f12013-08-20 11:48:36 +0200957/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100958void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
959 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100962 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000963
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100964 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000966
Paul Bakker33b43f12013-08-20 11:48:36 +0200967 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000968 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100969 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000970 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200971 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000972 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100973 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000974 }
975
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100976 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100978
Paul Bakkerbd51b262014-07-10 15:26:12 +0200979exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100980 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000982}
Paul Bakker33b43f12013-08-20 11:48:36 +0200983/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000984
Paul Bakker33b43f12013-08-20 11:48:36 +0200985/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100986void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
987 int radix_Q, char * input_Q, int radix_N,
988 char * input_N, int radix_E, char * input_E,
989 int radix_D, char * input_D, int radix_DP,
990 char * input_DP, int radix_DQ,
991 char * input_DQ, int radix_QP,
992 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000993{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000997
Paul Bakker33b43f12013-08-20 11:48:36 +0200998 ctx.len = mod / 8;
999 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001002 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001003 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001006 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001007 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001010 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001011 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001014 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001015 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001018 }
Hanno Becker131134f2017-08-23 08:31:07 +01001019#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +02001020 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001023 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001024 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001027 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001028 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001031 }
Hanno Becker131134f2017-08-23 08:31:07 +01001032#else
1033 ((void) radix_DP); ((void) input_DP);
1034 ((void) radix_DQ); ((void) input_DQ);
1035 ((void) radix_QP); ((void) input_QP);
1036#endif
Paul Bakker821fb082009-07-12 13:26:42 +00001037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001039
Paul Bakkerbd51b262014-07-10 15:26:12 +02001040exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001042}
Paul Bakker33b43f12013-08-20 11:48:36 +02001043/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001044
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001045/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001046void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
1047 int radix_Epub, char * input_Epub, int radix_P,
1048 char * input_P, int radix_Q, char * input_Q,
1049 int radix_N, char * input_N, int radix_E,
1050 char * input_E, int radix_D, char * input_D,
1051 int radix_DP, char * input_DP, int radix_DQ,
1052 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001053 int result )
1054{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1058 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001059
1060 pub.len = mod / 8;
1061 prv.len = mod / 8;
1062
1063 if( strlen( input_Npub ) )
1064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001066 }
1067 if( strlen( input_Epub ) )
1068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001070 }
1071
1072 if( strlen( input_P ) )
1073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001075 }
1076 if( strlen( input_Q ) )
1077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001079 }
1080 if( strlen( input_N ) )
1081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001083 }
1084 if( strlen( input_E ) )
1085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001087 }
1088 if( strlen( input_D ) )
1089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001091 }
Hanno Becker131134f2017-08-23 08:31:07 +01001092#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001093 if( strlen( input_DP ) )
1094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001096 }
1097 if( strlen( input_DQ ) )
1098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001100 }
1101 if( strlen( input_QP ) )
1102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001104 }
Hanno Becker131134f2017-08-23 08:31:07 +01001105#else
1106 ((void) radix_DP); ((void) input_DP);
1107 ((void) radix_DQ); ((void) input_DQ);
1108 ((void) radix_QP); ((void) input_QP);
1109#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001112
1113exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 mbedtls_rsa_free( &pub );
1115 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001116}
1117/* END_CASE */
1118
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001119/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_rsa_context ctx;
1123 mbedtls_entropy_context entropy;
1124 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001125 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001126
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001127 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001129 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001130
Hanno Beckera47023e2017-12-22 17:08:03 +00001131 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1132 &entropy, (const unsigned char *) pers,
1133 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001136 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001139 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001140 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001141
Paul Bakkerbd51b262014-07-10 15:26:12 +02001142exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_rsa_free( &ctx );
1144 mbedtls_ctr_drbg_free( &ctr_drbg );
1145 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001146}
Paul Bakker33b43f12013-08-20 11:48:36 +02001147/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001148
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001149/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001150void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001151 int radix_D, char *input_D,
1152 int radix_E, char *input_E,
1153 int radix_P, char *output_P,
1154 int radix_Q, char *output_Q,
1155 int corrupt, int result )
1156{
1157 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1158
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001159 mbedtls_mpi_init( &N );
1160 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1161 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1162 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1163
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001164 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1165 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1166 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1167 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1168 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1169
1170 if( corrupt )
1171 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1172
1173 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001174 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001175
1176 if( !corrupt )
1177 {
1178 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1179 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1180 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1181 }
1182
1183exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001184 mbedtls_mpi_free( &N );
1185 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1186 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1187 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001188}
1189/* END_CASE */
1190
Hanno Becker6b4ce492017-08-23 11:00:21 +01001191/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001192void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1193 int radix_Q, char *input_Q,
1194 int radix_E, char *input_E,
1195 int radix_D, char *output_D,
1196 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001197{
1198 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1199
1200 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1201 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1202 mbedtls_mpi_init( &E );
1203 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1204
1205 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1206 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1207 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1208 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1209
1210 if( corrupt )
1211 {
1212 /* Make E even */
1213 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1214 }
1215
1216 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001217 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1218 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001219
1220 if( !corrupt )
1221 {
1222 /*
1223 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1224 */
1225
1226 /* Replace P,Q by P-1, Q-1 */
1227 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1228 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1229
1230 /* Check D == Dp modulo P-1 */
1231 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1232 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1233 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1234
1235 /* Check D == Dp modulo Q-1 */
1236 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1237 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1238 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1239 }
1240
1241exit:
1242
1243 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1244 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1245 mbedtls_mpi_free( &E );
1246 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1247}
1248/* END_CASE */
1249
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001250/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001251void mbedtls_rsa_import( int radix_N, char *input_N,
1252 int radix_P, char *input_P,
1253 int radix_Q, char *input_Q,
1254 int radix_D, char *input_D,
1255 int radix_E, char *input_E,
1256 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001257 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001258 int res_check,
1259 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001260{
1261 mbedtls_mpi N, P, Q, D, E;
1262 mbedtls_rsa_context ctx;
1263
Hanno Beckere1582a82017-09-29 11:51:05 +01001264 /* Buffers used for encryption-decryption test */
1265 unsigned char *buf_orig = NULL;
1266 unsigned char *buf_enc = NULL;
1267 unsigned char *buf_dec = NULL;
1268
Hanno Beckerc77ab892017-08-23 11:01:06 +01001269 mbedtls_entropy_context entropy;
1270 mbedtls_ctr_drbg_context ctr_drbg;
1271 const char *pers = "test_suite_rsa";
1272
Hanno Becker4d6e8342017-09-29 11:50:18 +01001273 const int have_N = ( strlen( input_N ) > 0 );
1274 const int have_P = ( strlen( input_P ) > 0 );
1275 const int have_Q = ( strlen( input_Q ) > 0 );
1276 const int have_D = ( strlen( input_D ) > 0 );
1277 const int have_E = ( strlen( input_E ) > 0 );
1278
Hanno Beckerc77ab892017-08-23 11:01:06 +01001279 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001280 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001281 mbedtls_rsa_init( &ctx, 0, 0 );
1282
1283 mbedtls_mpi_init( &N );
1284 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1285 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1286
Hanno Beckerd4d60572018-01-10 07:12:01 +00001287 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1288 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1289
Hanno Becker4d6e8342017-09-29 11:50:18 +01001290 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001291 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1292
Hanno Becker4d6e8342017-09-29 11:50:18 +01001293 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001294 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1295
Hanno Becker4d6e8342017-09-29 11:50:18 +01001296 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001297 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1298
Hanno Becker4d6e8342017-09-29 11:50:18 +01001299 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001300 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1301
Hanno Becker4d6e8342017-09-29 11:50:18 +01001302 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001303 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1304
1305 if( !successive )
1306 {
1307 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001308 have_N ? &N : NULL,
1309 have_P ? &P : NULL,
1310 have_Q ? &Q : NULL,
1311 have_D ? &D : NULL,
1312 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001313 }
1314 else
1315 {
1316 /* Import N, P, Q, D, E separately.
1317 * This should make no functional difference. */
1318
1319 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001320 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001321 NULL, NULL, NULL, NULL ) == 0 );
1322
1323 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1324 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001325 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001326 NULL, NULL, NULL ) == 0 );
1327
1328 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1329 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001330 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001331 NULL, NULL ) == 0 );
1332
1333 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1334 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001335 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001336 NULL ) == 0 );
1337
1338 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1339 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001340 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001341 }
1342
Hanno Becker04877a42017-10-11 10:01:33 +01001343 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001344
Hanno Beckere1582a82017-09-29 11:51:05 +01001345 /* On expected success, perform some public and private
1346 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001347 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001348 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001349 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001350 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1351 else
1352 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1353
1354 if( res_check != 0 )
1355 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001356
1357 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1358 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1359 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1360 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1361 goto exit;
1362
1363 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1364 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1365
1366 /* Make sure the number we're generating is smaller than the modulus */
1367 buf_orig[0] = 0x00;
1368
1369 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1370
1371 if( is_priv )
1372 {
1373 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1374 &ctr_drbg, buf_enc,
1375 buf_dec ) == 0 );
1376
1377 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1378 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1379 }
1380 }
1381
Hanno Beckerc77ab892017-08-23 11:01:06 +01001382exit:
1383
Hanno Beckere1582a82017-09-29 11:51:05 +01001384 mbedtls_free( buf_orig );
1385 mbedtls_free( buf_enc );
1386 mbedtls_free( buf_dec );
1387
Hanno Beckerc77ab892017-08-23 11:01:06 +01001388 mbedtls_rsa_free( &ctx );
1389
1390 mbedtls_ctr_drbg_free( &ctr_drbg );
1391 mbedtls_entropy_free( &entropy );
1392
1393 mbedtls_mpi_free( &N );
1394 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1395 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1396}
1397/* END_CASE */
1398
Hanno Becker417f2d62017-08-23 11:44:51 +01001399/* BEGIN_CASE */
1400void mbedtls_rsa_export( int radix_N, char *input_N,
1401 int radix_P, char *input_P,
1402 int radix_Q, char *input_Q,
1403 int radix_D, char *input_D,
1404 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001405 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001406 int successive )
1407{
1408 /* Original MPI's with which we set up the RSA context */
1409 mbedtls_mpi N, P, Q, D, E;
1410
1411 /* Exported MPI's */
1412 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1413
1414 const int have_N = ( strlen( input_N ) > 0 );
1415 const int have_P = ( strlen( input_P ) > 0 );
1416 const int have_Q = ( strlen( input_Q ) > 0 );
1417 const int have_D = ( strlen( input_D ) > 0 );
1418 const int have_E = ( strlen( input_E ) > 0 );
1419
Hanno Becker417f2d62017-08-23 11:44:51 +01001420 mbedtls_rsa_context ctx;
1421
1422 mbedtls_rsa_init( &ctx, 0, 0 );
1423
1424 mbedtls_mpi_init( &N );
1425 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1426 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1427
1428 mbedtls_mpi_init( &Ne );
1429 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1430 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1431
1432 /* Setup RSA context */
1433
1434 if( have_N )
1435 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1436
1437 if( have_P )
1438 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1439
1440 if( have_Q )
1441 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1442
1443 if( have_D )
1444 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1445
1446 if( have_E )
1447 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1448
1449 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1450 strlen( input_N ) ? &N : NULL,
1451 strlen( input_P ) ? &P : NULL,
1452 strlen( input_Q ) ? &Q : NULL,
1453 strlen( input_D ) ? &D : NULL,
1454 strlen( input_E ) ? &E : NULL ) == 0 );
1455
Hanno Becker7f25f852017-10-10 16:56:22 +01001456 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001457
1458 /*
1459 * Export parameters and compare to original ones.
1460 */
1461
1462 /* N and E must always be present. */
1463 if( !successive )
1464 {
1465 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1466 }
1467 else
1468 {
1469 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1470 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1471 }
1472 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1473 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1474
1475 /* If we were providing enough information to setup a complete private context,
1476 * we expect to be able to export all core parameters. */
1477
1478 if( is_priv )
1479 {
1480 if( !successive )
1481 {
1482 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1483 &De, NULL ) == 0 );
1484 }
1485 else
1486 {
1487 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1488 NULL, NULL ) == 0 );
1489 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1490 NULL, NULL ) == 0 );
1491 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1492 &De, NULL ) == 0 );
1493 }
1494
1495 if( have_P )
1496 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1497
1498 if( have_Q )
1499 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1500
1501 if( have_D )
1502 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1503
1504 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001505 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1506 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001507 }
1508
1509exit:
1510
1511 mbedtls_rsa_free( &ctx );
1512
1513 mbedtls_mpi_free( &N );
1514 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1515 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1516
1517 mbedtls_mpi_free( &Ne );
1518 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1519 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1520}
1521/* END_CASE */
1522
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001523/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001524void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1525 int radix_P, char *input_P,
1526 int radix_Q, char *input_Q,
1527 int radix_D, char *input_D,
1528 int radix_E, char *input_E,
1529 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001530{
1531 /* Original MPI's with which we set up the RSA context */
1532 mbedtls_mpi N, P, Q, D, E;
1533
1534 const int have_N = ( strlen( input_N ) > 0 );
1535 const int have_P = ( strlen( input_P ) > 0 );
1536 const int have_Q = ( strlen( input_Q ) > 0 );
1537 const int have_D = ( strlen( input_D ) > 0 );
1538 const int have_E = ( strlen( input_E ) > 0 );
1539
1540 mbedtls_entropy_context entropy;
1541 mbedtls_ctr_drbg_context ctr_drbg;
1542 const char *pers = "test_suite_rsa";
1543
1544 mbedtls_mpi_init( &N );
1545 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1546 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1547
1548 mbedtls_ctr_drbg_init( &ctr_drbg );
1549 mbedtls_entropy_init( &entropy );
1550 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1551 &entropy, (const unsigned char *) pers,
1552 strlen( pers ) ) == 0 );
1553
1554 if( have_N )
1555 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1556
1557 if( have_P )
1558 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1559
1560 if( have_Q )
1561 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1562
1563 if( have_D )
1564 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1565
1566 if( have_E )
1567 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1568
Hanno Becker750e8b42017-08-25 07:54:27 +01001569 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1570 have_P ? &P : NULL,
1571 have_Q ? &Q : NULL,
1572 have_D ? &D : NULL,
1573 have_E ? &E : NULL,
1574 prng ? mbedtls_ctr_drbg_random : NULL,
1575 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001576exit:
1577
1578 mbedtls_ctr_drbg_free( &ctr_drbg );
1579 mbedtls_entropy_free( &entropy );
1580
1581 mbedtls_mpi_free( &N );
1582 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1583 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1584}
1585/* END_CASE */
1586
Hanno Beckerc77ab892017-08-23 11:01:06 +01001587/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001588void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1589 data_t *input_Q, data_t *input_D,
1590 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001591 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001592{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001593 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001594 unsigned char bufNe[256];
1595 unsigned char bufPe[128];
1596 unsigned char bufQe[128];
1597 unsigned char bufDe[256];
1598 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001599
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001600 mbedtls_rsa_context ctx;
1601
1602 mbedtls_rsa_init( &ctx, 0, 0 );
1603
1604 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001605 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001606 input_N->len ? input_N->x : NULL, input_N->len,
1607 input_P->len ? input_P->x : NULL, input_P->len,
1608 input_Q->len ? input_Q->x : NULL, input_Q->len,
1609 input_D->len ? input_D->x : NULL, input_D->len,
1610 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001611
Hanno Becker7f25f852017-10-10 16:56:22 +01001612 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001613
1614 /*
1615 * Export parameters and compare to original ones.
1616 */
1617
1618 /* N and E must always be present. */
1619 if( !successive )
1620 {
Azim Khand30ca132017-06-09 04:32:58 +01001621 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001622 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001623 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001624 }
1625 else
1626 {
Azim Khand30ca132017-06-09 04:32:58 +01001627 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001628 NULL, 0, NULL, 0, NULL, 0,
1629 NULL, 0 ) == 0 );
1630 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1631 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001632 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001633 }
Azim Khand30ca132017-06-09 04:32:58 +01001634 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1635 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001636
1637 /* If we were providing enough information to setup a complete private context,
1638 * we expect to be able to export all core parameters. */
1639
1640 if( is_priv )
1641 {
1642 if( !successive )
1643 {
1644 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001645 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1646 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1647 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001648 NULL, 0 ) == 0 );
1649 }
1650 else
1651 {
1652 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001653 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001654 NULL, 0, NULL, 0,
1655 NULL, 0 ) == 0 );
1656
1657 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001658 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001659 NULL, 0, NULL, 0 ) == 0 );
1660
Azim Khand30ca132017-06-09 04:32:58 +01001661 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1662 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001663 NULL, 0 ) == 0 );
1664 }
1665
Azim Khand30ca132017-06-09 04:32:58 +01001666 if( input_P->len )
1667 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001668
Azim Khand30ca132017-06-09 04:32:58 +01001669 if( input_Q->len )
1670 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001671
Azim Khand30ca132017-06-09 04:32:58 +01001672 if( input_D->len )
1673 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001674
1675 }
1676
1677exit:
1678 mbedtls_rsa_free( &ctx );
1679}
1680/* END_CASE */
1681
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001682/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001683void mbedtls_rsa_import_raw( data_t *input_N,
1684 data_t *input_P, data_t *input_Q,
1685 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001686 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001687 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001688 int res_check,
1689 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001690{
Hanno Beckere1582a82017-09-29 11:51:05 +01001691 /* Buffers used for encryption-decryption test */
1692 unsigned char *buf_orig = NULL;
1693 unsigned char *buf_enc = NULL;
1694 unsigned char *buf_dec = NULL;
1695
Hanno Beckerc77ab892017-08-23 11:01:06 +01001696 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001697 mbedtls_entropy_context entropy;
1698 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001699
Hanno Beckerc77ab892017-08-23 11:01:06 +01001700 const char *pers = "test_suite_rsa";
1701
1702 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001703 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001704 mbedtls_rsa_init( &ctx, 0, 0 );
1705
Hanno Beckerc77ab892017-08-23 11:01:06 +01001706 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1707 &entropy, (const unsigned char *) pers,
1708 strlen( pers ) ) == 0 );
1709
Hanno Beckerc77ab892017-08-23 11:01:06 +01001710 if( !successive )
1711 {
1712 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001713 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1714 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1715 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1716 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1717 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001718 }
1719 else
1720 {
1721 /* Import N, P, Q, D, E separately.
1722 * This should make no functional difference. */
1723
1724 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001725 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001726 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1727
1728 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1729 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001730 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001731 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1732
1733 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1734 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001735 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001736 NULL, 0, NULL, 0 ) == 0 );
1737
1738 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1739 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001740 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001741 NULL, 0 ) == 0 );
1742
1743 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1744 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001745 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001746 }
1747
Hanno Becker04877a42017-10-11 10:01:33 +01001748 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001749
Hanno Beckere1582a82017-09-29 11:51:05 +01001750 /* On expected success, perform some public and private
1751 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001752 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001753 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001754 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001755 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1756 else
1757 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1758
1759 if( res_check != 0 )
1760 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001761
1762 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1763 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1764 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1765 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1766 goto exit;
1767
1768 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1769 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1770
1771 /* Make sure the number we're generating is smaller than the modulus */
1772 buf_orig[0] = 0x00;
1773
1774 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1775
1776 if( is_priv )
1777 {
1778 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1779 &ctr_drbg, buf_enc,
1780 buf_dec ) == 0 );
1781
1782 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1783 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1784 }
1785 }
1786
Hanno Beckerc77ab892017-08-23 11:01:06 +01001787exit:
1788
Hanno Becker3f3ae852017-10-02 10:08:39 +01001789 mbedtls_free( buf_orig );
1790 mbedtls_free( buf_enc );
1791 mbedtls_free( buf_dec );
1792
Hanno Beckerc77ab892017-08-23 11:01:06 +01001793 mbedtls_rsa_free( &ctx );
1794
1795 mbedtls_ctr_drbg_free( &ctr_drbg );
1796 mbedtls_entropy_free( &entropy );
1797
1798}
1799/* END_CASE */
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001802void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001803{
Andres AG93012e82016-09-09 09:10:28 +01001804 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001805}
Paul Bakker33b43f12013-08-20 11:48:36 +02001806/* END_CASE */