blob: 4a818c59a5c41948e703225ba5f4bb588aba63f9 [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,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500106 sizeof( buf ), buf,
107 buf ) );
108 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
109 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 sizeof( buf ), NULL,
111 buf ) );
112 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
113 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500114 sizeof( buf ), buf,
115 NULL ) );
116
117 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
118 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
119 NULL,
Thomas Daubney69a8c382021-05-13 17:59:50 +0100120 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121 sizeof( buf ), buf,
122 buf ) );
123 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
124 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
125 NULL,
Thomas Daubney69a8c382021-05-13 17:59:50 +0100126 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500127 sizeof( buf ), NULL,
128 buf ) );
129 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
130 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
131 NULL,
Thomas Daubney69a8c382021-05-13 17:59:50 +0100132 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 sizeof( buf ), buf,
134 NULL ) );
135
136 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
137 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
138 valid_mode,
139 buf, sizeof( buf ),
140 sizeof( buf ), buf,
141 buf ) );
142 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
143 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
144 invalid_mode,
145 buf, sizeof( buf ),
146 sizeof( buf ), buf,
147 buf ) );
148 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
149 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
150 valid_mode,
151 NULL, sizeof( buf ),
152 sizeof( buf ), buf,
153 buf ) );
154 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
155 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
156 valid_mode,
157 buf, sizeof( buf ),
158 sizeof( buf ), NULL,
159 buf ) );
160 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
161 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
162 valid_mode,
163 buf, sizeof( buf ),
164 sizeof( buf ), buf,
165 NULL ) );
166
167 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
168 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100169 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500170 buf, buf, 42 ) );
171 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
172 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100173 NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500174 buf, buf, 42 ) );
175 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
176 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100177 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500178 NULL, buf, 42 ) );
179 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
180 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100181 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500182 buf, NULL, 42 ) );
183
184 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
185 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100186 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187 buf, buf, 42 ) );
188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
189 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100190 NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 buf, buf, 42 ) );
192 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
193 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100194 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500195 NULL, buf, 42 ) );
196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
197 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100198 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500199 buf, NULL, 42 ) );
200
201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
202 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500203 buf, sizeof( buf ),
204 &olen,
205 buf, buf, 42 ) );
206 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
207 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500208 NULL, sizeof( buf ),
209 NULL,
210 buf, buf, 42 ) );
211 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
212 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500213 buf, sizeof( buf ),
214 &olen,
215 NULL, buf, 42 ) );
216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
217 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218 buf, sizeof( buf ),
219 &olen,
220 buf, NULL, 42 ) );
221
222 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
223 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
224 valid_mode,
225 0, sizeof( buf ), buf,
226 buf ) );
227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
228 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
229 invalid_mode,
230 0, sizeof( buf ), buf,
231 buf ) );
232 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
233 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
234 valid_mode,
235 0, sizeof( buf ), NULL,
236 buf ) );
237 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
238 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
239 valid_mode,
240 0, sizeof( buf ), buf,
241 NULL ) );
242 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
243 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
244 valid_mode,
245 MBEDTLS_MD_SHA1,
246 0, NULL,
247 buf ) );
248
249 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
250 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
251 valid_mode,
252 0, sizeof( buf ), buf,
253 buf ) );
254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
255 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
256 invalid_mode,
257 0, sizeof( buf ), buf,
258 buf ) );
259 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
260 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
261 valid_mode,
262 0, sizeof( buf ), NULL,
263 buf ) );
264 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
265 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
266 valid_mode,
267 0, sizeof( buf ), buf,
268 NULL ) );
269 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
270 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
271 valid_mode,
272 MBEDTLS_MD_SHA1,
273 0, NULL,
274 buf ) );
275
276 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
277 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
278 valid_mode,
279 0, sizeof( buf ), buf,
280 buf ) );
281 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
282 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
283 invalid_mode,
284 0, sizeof( buf ), buf,
285 buf ) );
286 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
287 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
288 valid_mode,
289 0, sizeof( buf ), NULL,
290 buf ) );
291 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
292 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
293 valid_mode,
294 0, sizeof( buf ), buf,
295 NULL ) );
296 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
297 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
298 valid_mode,
299 MBEDTLS_MD_SHA1,
300 0, NULL,
301 buf ) );
302
303 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Cédric Meutera05cbec2020-04-25 15:02:34 +0200304 mbedtls_rsa_rsassa_pss_sign_ext( NULL, NULL, NULL,
305 0, sizeof( buf ), buf,
306 MBEDTLS_RSA_SALT_LEN_ANY,
307 buf ) );
308 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
309 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
310 0, sizeof( buf ), NULL,
311 MBEDTLS_RSA_SALT_LEN_ANY,
312 buf ) );
313 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
314 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
315 0, sizeof( buf ), buf,
316 MBEDTLS_RSA_SALT_LEN_ANY,
317 NULL ) );
318 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
319 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
320 MBEDTLS_MD_SHA1,
321 0, NULL,
322 MBEDTLS_RSA_SALT_LEN_ANY,
323 buf ) );
324
325 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500326 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
327 valid_mode,
328 0, sizeof( buf ), buf,
329 buf ) );
330 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
331 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
332 invalid_mode,
333 0, sizeof( buf ), buf,
334 buf ) );
335 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
336 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
337 valid_mode,
338 0, sizeof( buf ), NULL,
339 buf ) );
340 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
341 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
342 valid_mode,
343 0, sizeof( buf ), buf,
344 NULL ) );
345 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
346 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
347 valid_mode,
348 MBEDTLS_MD_SHA1, 0, NULL,
349 buf ) );
350
351 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
352 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
353 NULL,
354 valid_mode,
355 0, sizeof( buf ), buf,
356 buf ) );
357 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
358 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
359 NULL,
360 invalid_mode,
361 0, sizeof( buf ), buf,
362 buf ) );
363 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
364 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
365 NULL,
366 valid_mode,
367 0, sizeof( buf ),
368 NULL, buf ) );
369 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
370 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
371 NULL,
372 valid_mode,
373 0, sizeof( buf ), buf,
374 NULL ) );
375 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
376 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
377 NULL,
378 valid_mode,
379 MBEDTLS_MD_SHA1,
380 0, NULL,
381 buf ) );
382
383 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
384 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
385 valid_mode,
386 0, sizeof( buf ),
387 buf, buf ) );
388 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
389 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
390 invalid_mode,
391 0, sizeof( buf ),
392 buf, buf ) );
393 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
394 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
395 valid_mode,
396 0, sizeof( buf ),
397 NULL, buf ) );
398 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
399 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
400 valid_mode,
401 0, sizeof( buf ),
402 buf, NULL ) );
403 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
404 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
405 valid_mode,
406 MBEDTLS_MD_SHA1,
407 0, NULL,
408 buf ) );
409
410 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
411 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
412 valid_mode,
413 0, sizeof( buf ),
414 buf,
415 0, 0,
416 buf ) );
417 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
418 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
419 invalid_mode,
420 0, sizeof( buf ),
421 buf,
422 0, 0,
423 buf ) );
424 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
425 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
426 valid_mode,
427 0, sizeof( buf ),
428 NULL, 0, 0,
429 buf ) );
430 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
431 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
432 valid_mode,
433 0, sizeof( buf ),
434 buf, 0, 0,
435 NULL ) );
436 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
437 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
438 valid_mode,
439 MBEDTLS_MD_SHA1,
440 0, NULL,
441 0, 0,
442 buf ) );
443
444 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
445 mbedtls_rsa_copy( NULL, &ctx ) );
446 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
447 mbedtls_rsa_copy( &ctx, NULL ) );
448
449exit:
450 return;
451}
452/* END_CASE */
453
Paul Bakker33b43f12013-08-20 11:48:36 +0200454/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100455void rsa_init_free( int reinit )
456{
457 mbedtls_rsa_context ctx;
458
459 /* Double free is not explicitly documented to work, but we rely on it
460 * even inside the library so that you can call mbedtls_rsa_free()
461 * unconditionally on an error path without checking whether it has
462 * already been called in the success path. */
463
464 mbedtls_rsa_init( &ctx, 0, 0 );
465 mbedtls_rsa_free( &ctx );
466
467 if( reinit )
468 mbedtls_rsa_init( &ctx, 0, 0 );
469 mbedtls_rsa_free( &ctx );
470
471 /* This test case always succeeds, functionally speaking. A plausible
472 * bug might trigger an invalid pointer dereference or a memory leak. */
473 goto exit;
474}
475/* END_CASE */
476
477/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100478void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100479 int digest, int mod, int radix_P, char * input_P,
480 int radix_Q, char * input_Q, int radix_N,
481 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200482 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000483{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200484 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
485 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100487 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200488 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000489
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100490 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
491 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000493
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200494 memset( hash_result, 0x00, sizeof( hash_result ) );
495 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200496 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000497
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100498 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
499 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
500 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
501 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000502
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100503 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
504 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100505 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000507
Paul Bakker42a29bf2009-07-07 20:18:41 +0000508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100510 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 +0000511
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200512 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
513 &rnd_info, MBEDTLS_RSA_PRIVATE, digest,
514 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200515 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000516 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000517
Ronald Cronac6ae352020-06-26 14:33:03 +0200518 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
519 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000520 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000521
Paul Bakkerbd51b262014-07-10 15:26:12 +0200522exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100523 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
524 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000526}
Paul Bakker33b43f12013-08-20 11:48:36 +0200527/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000528
Paul Bakker33b43f12013-08-20 11:48:36 +0200529/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100530void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100531 int digest, int mod, int radix_N,
532 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100533 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000534{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200535 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000537
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100538 mbedtls_mpi N, E;
539
540 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200542 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000543
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100544 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
545 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
546 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
547 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000549
Paul Bakker42a29bf2009-07-07 20:18:41 +0000550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100552 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 +0000553
Azim Khand30ca132017-06-09 04:32:58 +0100554 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 +0100555
Paul Bakkerbd51b262014-07-10 15:26:12 +0200556exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100557 mbedtls_mpi_free( &N ); 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 Bakker821fb082009-07-12 13:26:42 +0000562
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100564void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100565 int padding_mode, int mod, int radix_P,
566 char * input_P, int radix_Q, char * input_Q,
567 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200568 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000569{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200570 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100572 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200573 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100576 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
577 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000578
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200579 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200580 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000581
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100582 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
583 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
584 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
585 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000586
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100587 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
588 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100589 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000591
Paul Bakker821fb082009-07-12 13:26:42 +0000592
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200593 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
594 &rnd_info, MBEDTLS_RSA_PRIVATE,
595 MBEDTLS_MD_NONE, hash_result->len,
596 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000597
Paul Bakker821fb082009-07-12 13:26:42 +0000598
Ronald Cronac6ae352020-06-26 14:33:03 +0200599 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
600 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000601
Paul Bakkerbd51b262014-07-10 15:26:12 +0200602exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100603 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
604 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000607}
Paul Bakker33b43f12013-08-20 11:48:36 +0200608/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000609
Paul Bakker33b43f12013-08-20 11:48:36 +0200610/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100611void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200612 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100613 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100614 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000615{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200616 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000618
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100619 mbedtls_mpi N, E;
620 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100623 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000624
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100625 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
626 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000627
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100628 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
629 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000631
Paul Bakker821fb082009-07-12 13:26:42 +0000632
Azim Khand30ca132017-06-09 04:32:58 +0100633 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 +0100634
Paul Bakkerbd51b262014-07-10 15:26:12 +0200635exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100636 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000638}
Paul Bakker33b43f12013-08-20 11:48:36 +0200639/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000640
Paul Bakker33b43f12013-08-20 11:48:36 +0200641/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100642void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100643 int mod, int radix_N, char * input_N,
644 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200645 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000646{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200647 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200649 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000650
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100651 mbedtls_mpi N, E;
652 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
653
Ronald Cron351f0ee2020-06-10 12:12:18 +0200654 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200657 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000658
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100659 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
660 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000661
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100662 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
663 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000665
Paul Bakker42a29bf2009-07-07 20:18:41 +0000666
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200667 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
668 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100669 &rnd_info, message_str->len,
670 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200671 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200672 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000673 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000674
Ronald Cronac6ae352020-06-26 14:33:03 +0200675 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
676 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000677 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100678
Paul Bakkerbd51b262014-07-10 15:26:12 +0200679exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100680 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000682}
Paul Bakker33b43f12013-08-20 11:48:36 +0200683/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000684
Paul Bakker33b43f12013-08-20 11:48:36 +0200685/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100686void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100687 int mod, int radix_N, char * input_N,
688 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200689 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000690{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200691 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000693
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100694 mbedtls_mpi N, E;
695
696 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200698 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000699
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100700 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
701 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000702
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100703 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
704 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000706
Paul Bakkera6656852010-07-18 19:47:14 +0000707
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200708 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100709 NULL, message_str->len,
710 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200711 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200712 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000713 {
Paul Bakkera6656852010-07-18 19:47:14 +0000714
Ronald Cronac6ae352020-06-26 14:33:03 +0200715 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
716 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000717 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100718
Paul Bakkerbd51b262014-07-10 15:26:12 +0200719exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100720 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000722}
Paul Bakker33b43f12013-08-20 11:48:36 +0200723/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000724
Paul Bakker33b43f12013-08-20 11:48:36 +0200725/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100726void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100727 int mod, int radix_P, char * input_P,
728 int radix_Q, char * input_Q, int radix_N,
729 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200730 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100731 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000732{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200733 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000735 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200736 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100737 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000738
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100739 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
740 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000743
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200744 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200745 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000746
Paul Bakker42a29bf2009-07-07 20:18:41 +0000747
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100748 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
749 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
750 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
751 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000752
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100753 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
754 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100755 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000757
Paul Bakker69998dd2009-07-11 19:15:20 +0000758 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000759
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200760 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100761 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200762 &output_len, message_str->x, output,
763 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200764 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000765 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000766
Ronald Cronac6ae352020-06-26 14:33:03 +0200767 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200768 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200769 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000770 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000771
Paul Bakkerbd51b262014-07-10 15:26:12 +0200772exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100773 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
774 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000776}
Paul Bakker33b43f12013-08-20 11:48:36 +0200777/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000778
Paul Bakker33b43f12013-08-20 11:48:36 +0200779/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100780void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100781 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200782 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000783{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200784 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000786
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100787 mbedtls_mpi N, E;
788
789 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
791 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200792 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000793
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100794 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
795 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000796
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100797 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
798 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000800
Paul Bakker821fb082009-07-12 13:26:42 +0000801
Azim Khand30ca132017-06-09 04:32:58 +0100802 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200803 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000804 {
Paul Bakker821fb082009-07-12 13:26:42 +0000805
Ronald Cronac6ae352020-06-26 14:33:03 +0200806 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
807 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000808 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100809
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100810 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200812 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100816
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200817 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100818 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100819 if( result == 0 )
820 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100821
Ronald Cronac6ae352020-06-26 14:33:03 +0200822 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
823 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100824 }
825
Paul Bakkerbd51b262014-07-10 15:26:12 +0200826exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100827 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 mbedtls_rsa_free( &ctx );
829 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000830}
Paul Bakker33b43f12013-08-20 11:48:36 +0200831/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000832
Paul Bakker33b43f12013-08-20 11:48:36 +0200833/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100834void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100835 char * input_P, int radix_Q, char * input_Q,
836 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200837 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100838 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000839{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200840 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100842 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200843 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200844 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000845
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100846 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
847 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
849 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000850
Ronald Cron351f0ee2020-06-10 12:12:18 +0200851 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000852
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100853 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
854 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
855 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, &P, &Q, NULL, &E ) == 0 );
859 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100860 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000862
Paul Bakker821fb082009-07-12 13:26:42 +0000863
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200864 /* repeat three times to test updating of blinding values */
865 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000866 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200867 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200868 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
869 &rnd_info, message_str->x,
870 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200871 if( result == 0 )
872 {
Paul Bakker821fb082009-07-12 13:26:42 +0000873
Ronald Cronac6ae352020-06-26 14:33:03 +0200874 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200875 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200876 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200877 }
Paul Bakker821fb082009-07-12 13:26:42 +0000878 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000879
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100880 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200882 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100886
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200887 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200888 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
889 &rnd_info, message_str->x,
890 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100891 if( result == 0 )
892 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100893
Ronald Cronac6ae352020-06-26 14:33:03 +0200894 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200895 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200896 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100897 }
898
Paul Bakkerbd51b262014-07-10 15:26:12 +0200899exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100900 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
901 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000904}
Paul Bakker33b43f12013-08-20 11:48:36 +0200905/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000906
Paul Bakker33b43f12013-08-20 11:48:36 +0200907/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100908void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_rsa_context ctx;
911 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000914}
Paul Bakker33b43f12013-08-20 11:48:36 +0200915/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000916
Paul Bakker33b43f12013-08-20 11:48:36 +0200917/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100918void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
919 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100922 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000923
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100924 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000926
Paul Bakker33b43f12013-08-20 11:48:36 +0200927 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000928 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100929 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000930 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200931 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000932 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100933 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000934 }
935
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100936 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100938
Paul Bakkerbd51b262014-07-10 15:26:12 +0200939exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100940 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000942}
Paul Bakker33b43f12013-08-20 11:48:36 +0200943/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000944
Paul Bakker33b43f12013-08-20 11:48:36 +0200945/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100946void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
947 int radix_Q, char * input_Q, int radix_N,
948 char * input_N, int radix_E, char * input_E,
949 int radix_D, char * input_D, int radix_DP,
950 char * input_DP, int radix_DQ,
951 char * input_DQ, int radix_QP,
952 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000953{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000957
Paul Bakker33b43f12013-08-20 11:48:36 +0200958 ctx.len = mod / 8;
959 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000962 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200963 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 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 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.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 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000974 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200975 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000978 }
Hanno Becker131134f2017-08-23 08:31:07 +0100979#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200980 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000983 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200984 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000987 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200988 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000991 }
Hanno Becker131134f2017-08-23 08:31:07 +0100992#else
993 ((void) radix_DP); ((void) input_DP);
994 ((void) radix_DQ); ((void) input_DQ);
995 ((void) radix_QP); ((void) input_QP);
996#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100999
Paul Bakkerbd51b262014-07-10 15:26:12 +02001000exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001002}
Paul Bakker33b43f12013-08-20 11:48:36 +02001003/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001004
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001005/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001006void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
1007 int radix_Epub, char * input_Epub, int radix_P,
1008 char * input_P, int radix_Q, char * input_Q,
1009 int radix_N, char * input_N, int radix_E,
1010 char * input_E, int radix_D, char * input_D,
1011 int radix_DP, char * input_DP, int radix_DQ,
1012 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001013 int result )
1014{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1018 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001019
1020 pub.len = mod / 8;
1021 prv.len = mod / 8;
1022
1023 if( strlen( input_Npub ) )
1024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001026 }
1027 if( strlen( input_Epub ) )
1028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001030 }
1031
1032 if( strlen( input_P ) )
1033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001035 }
1036 if( strlen( input_Q ) )
1037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001039 }
1040 if( strlen( input_N ) )
1041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001043 }
1044 if( strlen( input_E ) )
1045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001047 }
1048 if( strlen( input_D ) )
1049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001051 }
Hanno Becker131134f2017-08-23 08:31:07 +01001052#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001053 if( strlen( input_DP ) )
1054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001056 }
1057 if( strlen( input_DQ ) )
1058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001060 }
1061 if( strlen( input_QP ) )
1062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001064 }
Hanno Becker131134f2017-08-23 08:31:07 +01001065#else
1066 ((void) radix_DP); ((void) input_DP);
1067 ((void) radix_DQ); ((void) input_DQ);
1068 ((void) radix_QP); ((void) input_QP);
1069#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001072
1073exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 mbedtls_rsa_free( &pub );
1075 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001076}
1077/* END_CASE */
1078
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001079/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001081{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_rsa_context ctx;
1083 mbedtls_entropy_context entropy;
1084 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001085 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001086
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001087 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001089 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001090
Hanno Beckera47023e2017-12-22 17:08:03 +00001091 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1092 &entropy, (const unsigned char *) pers,
1093 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001096 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001099 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001100 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001101
Paul Bakkerbd51b262014-07-10 15:26:12 +02001102exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 mbedtls_rsa_free( &ctx );
1104 mbedtls_ctr_drbg_free( &ctr_drbg );
1105 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001106}
Paul Bakker33b43f12013-08-20 11:48:36 +02001107/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001108
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001109/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001110void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001111 int radix_D, char *input_D,
1112 int radix_E, char *input_E,
1113 int radix_P, char *output_P,
1114 int radix_Q, char *output_Q,
1115 int corrupt, int result )
1116{
1117 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1118
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001119 mbedtls_mpi_init( &N );
1120 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1121 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1122 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1123
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001124 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1125 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1126 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1127 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1128 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1129
1130 if( corrupt )
1131 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1132
1133 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001134 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001135
1136 if( !corrupt )
1137 {
1138 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1139 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1140 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1141 }
1142
1143exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001144 mbedtls_mpi_free( &N );
1145 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1146 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1147 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001148}
1149/* END_CASE */
1150
Hanno Becker6b4ce492017-08-23 11:00:21 +01001151/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001152void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1153 int radix_Q, char *input_Q,
1154 int radix_E, char *input_E,
1155 int radix_D, char *output_D,
1156 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001157{
1158 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1159
1160 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1161 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1162 mbedtls_mpi_init( &E );
1163 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1164
1165 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1166 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1167 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1168 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1169
1170 if( corrupt )
1171 {
1172 /* Make E even */
1173 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1174 }
1175
1176 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001177 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1178 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001179
1180 if( !corrupt )
1181 {
1182 /*
1183 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1184 */
1185
1186 /* Replace P,Q by P-1, Q-1 */
1187 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1188 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1189
1190 /* Check D == Dp modulo P-1 */
1191 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1192 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1193 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1194
1195 /* Check D == Dp modulo Q-1 */
1196 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1197 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1198 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1199 }
1200
1201exit:
1202
1203 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1204 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1205 mbedtls_mpi_free( &E );
1206 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1207}
1208/* END_CASE */
1209
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001210/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001211void mbedtls_rsa_import( int radix_N, char *input_N,
1212 int radix_P, char *input_P,
1213 int radix_Q, char *input_Q,
1214 int radix_D, char *input_D,
1215 int radix_E, char *input_E,
1216 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001217 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001218 int res_check,
1219 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001220{
1221 mbedtls_mpi N, P, Q, D, E;
1222 mbedtls_rsa_context ctx;
1223
Hanno Beckere1582a82017-09-29 11:51:05 +01001224 /* Buffers used for encryption-decryption test */
1225 unsigned char *buf_orig = NULL;
1226 unsigned char *buf_enc = NULL;
1227 unsigned char *buf_dec = NULL;
1228
Hanno Beckerc77ab892017-08-23 11:01:06 +01001229 mbedtls_entropy_context entropy;
1230 mbedtls_ctr_drbg_context ctr_drbg;
1231 const char *pers = "test_suite_rsa";
1232
Hanno Becker4d6e8342017-09-29 11:50:18 +01001233 const int have_N = ( strlen( input_N ) > 0 );
1234 const int have_P = ( strlen( input_P ) > 0 );
1235 const int have_Q = ( strlen( input_Q ) > 0 );
1236 const int have_D = ( strlen( input_D ) > 0 );
1237 const int have_E = ( strlen( input_E ) > 0 );
1238
Hanno Beckerc77ab892017-08-23 11:01:06 +01001239 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001240 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001241 mbedtls_rsa_init( &ctx, 0, 0 );
1242
1243 mbedtls_mpi_init( &N );
1244 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1245 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1246
Hanno Beckerd4d60572018-01-10 07:12:01 +00001247 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1248 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1249
Hanno Becker4d6e8342017-09-29 11:50:18 +01001250 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001251 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1252
Hanno Becker4d6e8342017-09-29 11:50:18 +01001253 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001254 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1255
Hanno Becker4d6e8342017-09-29 11:50:18 +01001256 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001257 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1258
Hanno Becker4d6e8342017-09-29 11:50:18 +01001259 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001260 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1261
Hanno Becker4d6e8342017-09-29 11:50:18 +01001262 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001263 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1264
1265 if( !successive )
1266 {
1267 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001268 have_N ? &N : NULL,
1269 have_P ? &P : NULL,
1270 have_Q ? &Q : NULL,
1271 have_D ? &D : NULL,
1272 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001273 }
1274 else
1275 {
1276 /* Import N, P, Q, D, E separately.
1277 * This should make no functional difference. */
1278
1279 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001280 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001281 NULL, NULL, NULL, NULL ) == 0 );
1282
1283 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1284 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001285 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001286 NULL, NULL, NULL ) == 0 );
1287
1288 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1289 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001290 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001291 NULL, NULL ) == 0 );
1292
1293 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1294 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001295 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001296 NULL ) == 0 );
1297
1298 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1299 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001300 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001301 }
1302
Hanno Becker04877a42017-10-11 10:01:33 +01001303 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001304
Hanno Beckere1582a82017-09-29 11:51:05 +01001305 /* On expected success, perform some public and private
1306 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001307 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001308 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001309 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001310 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1311 else
1312 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1313
1314 if( res_check != 0 )
1315 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001316
1317 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1318 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1319 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1320 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1321 goto exit;
1322
1323 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1324 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1325
1326 /* Make sure the number we're generating is smaller than the modulus */
1327 buf_orig[0] = 0x00;
1328
1329 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1330
1331 if( is_priv )
1332 {
1333 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1334 &ctr_drbg, buf_enc,
1335 buf_dec ) == 0 );
1336
1337 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1338 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1339 }
1340 }
1341
Hanno Beckerc77ab892017-08-23 11:01:06 +01001342exit:
1343
Hanno Beckere1582a82017-09-29 11:51:05 +01001344 mbedtls_free( buf_orig );
1345 mbedtls_free( buf_enc );
1346 mbedtls_free( buf_dec );
1347
Hanno Beckerc77ab892017-08-23 11:01:06 +01001348 mbedtls_rsa_free( &ctx );
1349
1350 mbedtls_ctr_drbg_free( &ctr_drbg );
1351 mbedtls_entropy_free( &entropy );
1352
1353 mbedtls_mpi_free( &N );
1354 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1355 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1356}
1357/* END_CASE */
1358
Hanno Becker417f2d62017-08-23 11:44:51 +01001359/* BEGIN_CASE */
1360void mbedtls_rsa_export( int radix_N, char *input_N,
1361 int radix_P, char *input_P,
1362 int radix_Q, char *input_Q,
1363 int radix_D, char *input_D,
1364 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001365 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001366 int successive )
1367{
1368 /* Original MPI's with which we set up the RSA context */
1369 mbedtls_mpi N, P, Q, D, E;
1370
1371 /* Exported MPI's */
1372 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1373
1374 const int have_N = ( strlen( input_N ) > 0 );
1375 const int have_P = ( strlen( input_P ) > 0 );
1376 const int have_Q = ( strlen( input_Q ) > 0 );
1377 const int have_D = ( strlen( input_D ) > 0 );
1378 const int have_E = ( strlen( input_E ) > 0 );
1379
Hanno Becker417f2d62017-08-23 11:44:51 +01001380 mbedtls_rsa_context ctx;
1381
1382 mbedtls_rsa_init( &ctx, 0, 0 );
1383
1384 mbedtls_mpi_init( &N );
1385 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1386 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1387
1388 mbedtls_mpi_init( &Ne );
1389 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1390 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1391
1392 /* Setup RSA context */
1393
1394 if( have_N )
1395 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1396
1397 if( have_P )
1398 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1399
1400 if( have_Q )
1401 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1402
1403 if( have_D )
1404 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1405
1406 if( have_E )
1407 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1408
1409 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1410 strlen( input_N ) ? &N : NULL,
1411 strlen( input_P ) ? &P : NULL,
1412 strlen( input_Q ) ? &Q : NULL,
1413 strlen( input_D ) ? &D : NULL,
1414 strlen( input_E ) ? &E : NULL ) == 0 );
1415
Hanno Becker7f25f852017-10-10 16:56:22 +01001416 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001417
1418 /*
1419 * Export parameters and compare to original ones.
1420 */
1421
1422 /* N and E must always be present. */
1423 if( !successive )
1424 {
1425 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1426 }
1427 else
1428 {
1429 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1430 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1431 }
1432 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1433 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1434
1435 /* If we were providing enough information to setup a complete private context,
1436 * we expect to be able to export all core parameters. */
1437
1438 if( is_priv )
1439 {
1440 if( !successive )
1441 {
1442 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1443 &De, NULL ) == 0 );
1444 }
1445 else
1446 {
1447 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1448 NULL, NULL ) == 0 );
1449 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1450 NULL, NULL ) == 0 );
1451 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1452 &De, NULL ) == 0 );
1453 }
1454
1455 if( have_P )
1456 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1457
1458 if( have_Q )
1459 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1460
1461 if( have_D )
1462 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1463
1464 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001465 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1466 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001467 }
1468
1469exit:
1470
1471 mbedtls_rsa_free( &ctx );
1472
1473 mbedtls_mpi_free( &N );
1474 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1475 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1476
1477 mbedtls_mpi_free( &Ne );
1478 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1479 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1480}
1481/* END_CASE */
1482
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001483/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001484void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1485 int radix_P, char *input_P,
1486 int radix_Q, char *input_Q,
1487 int radix_D, char *input_D,
1488 int radix_E, char *input_E,
1489 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001490{
1491 /* Original MPI's with which we set up the RSA context */
1492 mbedtls_mpi N, P, Q, D, E;
1493
1494 const int have_N = ( strlen( input_N ) > 0 );
1495 const int have_P = ( strlen( input_P ) > 0 );
1496 const int have_Q = ( strlen( input_Q ) > 0 );
1497 const int have_D = ( strlen( input_D ) > 0 );
1498 const int have_E = ( strlen( input_E ) > 0 );
1499
1500 mbedtls_entropy_context entropy;
1501 mbedtls_ctr_drbg_context ctr_drbg;
1502 const char *pers = "test_suite_rsa";
1503
1504 mbedtls_mpi_init( &N );
1505 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1506 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1507
1508 mbedtls_ctr_drbg_init( &ctr_drbg );
1509 mbedtls_entropy_init( &entropy );
1510 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1511 &entropy, (const unsigned char *) pers,
1512 strlen( pers ) ) == 0 );
1513
1514 if( have_N )
1515 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1516
1517 if( have_P )
1518 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1519
1520 if( have_Q )
1521 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1522
1523 if( have_D )
1524 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1525
1526 if( have_E )
1527 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1528
Hanno Becker750e8b42017-08-25 07:54:27 +01001529 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1530 have_P ? &P : NULL,
1531 have_Q ? &Q : NULL,
1532 have_D ? &D : NULL,
1533 have_E ? &E : NULL,
1534 prng ? mbedtls_ctr_drbg_random : NULL,
1535 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001536exit:
1537
1538 mbedtls_ctr_drbg_free( &ctr_drbg );
1539 mbedtls_entropy_free( &entropy );
1540
1541 mbedtls_mpi_free( &N );
1542 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1543 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1544}
1545/* END_CASE */
1546
Hanno Beckerc77ab892017-08-23 11:01:06 +01001547/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001548void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1549 data_t *input_Q, data_t *input_D,
1550 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001551 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001552{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001553 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001554 unsigned char bufNe[256];
1555 unsigned char bufPe[128];
1556 unsigned char bufQe[128];
1557 unsigned char bufDe[256];
1558 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001559
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001560 mbedtls_rsa_context ctx;
1561
1562 mbedtls_rsa_init( &ctx, 0, 0 );
1563
1564 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001565 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001566 input_N->len ? input_N->x : NULL, input_N->len,
1567 input_P->len ? input_P->x : NULL, input_P->len,
1568 input_Q->len ? input_Q->x : NULL, input_Q->len,
1569 input_D->len ? input_D->x : NULL, input_D->len,
1570 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001571
Hanno Becker7f25f852017-10-10 16:56:22 +01001572 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001573
1574 /*
1575 * Export parameters and compare to original ones.
1576 */
1577
1578 /* N and E must always be present. */
1579 if( !successive )
1580 {
Azim Khand30ca132017-06-09 04:32:58 +01001581 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001582 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001583 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001584 }
1585 else
1586 {
Azim Khand30ca132017-06-09 04:32:58 +01001587 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001588 NULL, 0, NULL, 0, NULL, 0,
1589 NULL, 0 ) == 0 );
1590 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1591 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001592 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001593 }
Azim Khand30ca132017-06-09 04:32:58 +01001594 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1595 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001596
1597 /* If we were providing enough information to setup a complete private context,
1598 * we expect to be able to export all core parameters. */
1599
1600 if( is_priv )
1601 {
1602 if( !successive )
1603 {
1604 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001605 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1606 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1607 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001608 NULL, 0 ) == 0 );
1609 }
1610 else
1611 {
1612 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001613 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001614 NULL, 0, NULL, 0,
1615 NULL, 0 ) == 0 );
1616
1617 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001618 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001619 NULL, 0, NULL, 0 ) == 0 );
1620
Azim Khand30ca132017-06-09 04:32:58 +01001621 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1622 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001623 NULL, 0 ) == 0 );
1624 }
1625
Azim Khand30ca132017-06-09 04:32:58 +01001626 if( input_P->len )
1627 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001628
Azim Khand30ca132017-06-09 04:32:58 +01001629 if( input_Q->len )
1630 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001631
Azim Khand30ca132017-06-09 04:32:58 +01001632 if( input_D->len )
1633 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001634
1635 }
1636
1637exit:
1638 mbedtls_rsa_free( &ctx );
1639}
1640/* END_CASE */
1641
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001642/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001643void mbedtls_rsa_import_raw( data_t *input_N,
1644 data_t *input_P, data_t *input_Q,
1645 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001646 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001647 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001648 int res_check,
1649 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001650{
Hanno Beckere1582a82017-09-29 11:51:05 +01001651 /* Buffers used for encryption-decryption test */
1652 unsigned char *buf_orig = NULL;
1653 unsigned char *buf_enc = NULL;
1654 unsigned char *buf_dec = NULL;
1655
Hanno Beckerc77ab892017-08-23 11:01:06 +01001656 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001657 mbedtls_entropy_context entropy;
1658 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001659
Hanno Beckerc77ab892017-08-23 11:01:06 +01001660 const char *pers = "test_suite_rsa";
1661
1662 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001663 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001664 mbedtls_rsa_init( &ctx, 0, 0 );
1665
Hanno Beckerc77ab892017-08-23 11:01:06 +01001666 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1667 &entropy, (const unsigned char *) pers,
1668 strlen( pers ) ) == 0 );
1669
Hanno Beckerc77ab892017-08-23 11:01:06 +01001670 if( !successive )
1671 {
1672 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001673 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1674 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1675 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1676 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1677 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001678 }
1679 else
1680 {
1681 /* Import N, P, Q, D, E separately.
1682 * This should make no functional difference. */
1683
1684 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001685 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001686 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1687
1688 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1689 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001690 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001691 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1692
1693 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1694 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001695 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001696 NULL, 0, NULL, 0 ) == 0 );
1697
1698 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1699 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001700 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001701 NULL, 0 ) == 0 );
1702
1703 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1704 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001705 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001706 }
1707
Hanno Becker04877a42017-10-11 10:01:33 +01001708 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001709
Hanno Beckere1582a82017-09-29 11:51:05 +01001710 /* On expected success, perform some public and private
1711 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001712 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001713 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001714 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001715 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1716 else
1717 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1718
1719 if( res_check != 0 )
1720 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001721
1722 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1723 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1724 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1725 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1726 goto exit;
1727
1728 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1729 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1730
1731 /* Make sure the number we're generating is smaller than the modulus */
1732 buf_orig[0] = 0x00;
1733
1734 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1735
1736 if( is_priv )
1737 {
1738 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1739 &ctr_drbg, buf_enc,
1740 buf_dec ) == 0 );
1741
1742 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1743 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1744 }
1745 }
1746
Hanno Beckerc77ab892017-08-23 11:01:06 +01001747exit:
1748
Hanno Becker3f3ae852017-10-02 10:08:39 +01001749 mbedtls_free( buf_orig );
1750 mbedtls_free( buf_enc );
1751 mbedtls_free( buf_dec );
1752
Hanno Beckerc77ab892017-08-23 11:01:06 +01001753 mbedtls_rsa_free( &ctx );
1754
1755 mbedtls_ctr_drbg_free( &ctr_drbg );
1756 mbedtls_entropy_free( &entropy );
1757
1758}
1759/* END_CASE */
1760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001762void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001763{
Andres AG93012e82016-09-09 09:10:28 +01001764 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001765}
Paul Bakker33b43f12013-08-20 11:48:36 +02001766/* END_CASE */