blob: 1eca3148a3f5f145c8757b06a5500ac4bc7deef9 [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,
Thomas Daubneyc32e2b02021-05-13 17:03:47 +0100106 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500107 sizeof( buf ), buf,
108 buf ) );
109 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
110 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
Thomas Daubneyc32e2b02021-05-13 17:03:47 +0100111 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500112 sizeof( buf ), NULL,
113 buf ) );
114 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
115 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
Thomas Daubneyc32e2b02021-05-13 17:03:47 +0100116 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500117 sizeof( buf ), buf,
118 NULL ) );
119
120 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
121 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
122 NULL,
123 valid_mode,
124 sizeof( buf ), buf,
125 buf ) );
126 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
127 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
128 NULL,
129 invalid_mode,
130 sizeof( buf ), buf,
131 buf ) );
132 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
133 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
134 NULL,
135 valid_mode,
136 sizeof( buf ), NULL,
137 buf ) );
138 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
139 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
140 NULL,
141 valid_mode,
142 sizeof( buf ), buf,
143 NULL ) );
144
145 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
146 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
147 valid_mode,
148 buf, sizeof( buf ),
149 sizeof( buf ), buf,
150 buf ) );
151 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
152 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
153 invalid_mode,
154 buf, sizeof( buf ),
155 sizeof( buf ), buf,
156 buf ) );
157 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
158 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
159 valid_mode,
160 NULL, sizeof( buf ),
161 sizeof( buf ), buf,
162 buf ) );
163 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
164 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
165 valid_mode,
166 buf, sizeof( buf ),
167 sizeof( buf ), NULL,
168 buf ) );
169 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
170 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
171 valid_mode,
172 buf, sizeof( buf ),
173 sizeof( buf ), buf,
174 NULL ) );
175
176 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
177 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100178 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500179 buf, buf, 42 ) );
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
181 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100182 NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 buf, buf, 42 ) );
184 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
185 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100186 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187 NULL, buf, 42 ) );
188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
189 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100190 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 buf, NULL, 42 ) );
192
193 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
194 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100195 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500196 buf, buf, 42 ) );
197 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
198 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100199 NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500200 buf, buf, 42 ) );
201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
202 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100203 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500204 NULL, buf, 42 ) );
205 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
206 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100207 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500208 buf, NULL, 42 ) );
209
210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
211 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 buf, sizeof( buf ),
213 &olen,
214 buf, buf, 42 ) );
215 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
216 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500217 NULL, sizeof( buf ),
218 NULL,
219 buf, buf, 42 ) );
220 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
221 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500222 buf, sizeof( buf ),
223 &olen,
224 NULL, buf, 42 ) );
225 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
226 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227 buf, sizeof( buf ),
228 &olen,
229 buf, NULL, 42 ) );
230
231 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
232 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
233 valid_mode,
234 0, sizeof( buf ), buf,
235 buf ) );
236 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
237 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
238 invalid_mode,
239 0, sizeof( buf ), buf,
240 buf ) );
241 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
242 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
243 valid_mode,
244 0, sizeof( buf ), NULL,
245 buf ) );
246 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
247 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
248 valid_mode,
249 0, sizeof( buf ), buf,
250 NULL ) );
251 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
252 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
253 valid_mode,
254 MBEDTLS_MD_SHA1,
255 0, NULL,
256 buf ) );
257
258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
259 mbedtls_rsa_rsassa_pkcs1_v15_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_rsassa_pkcs1_v15_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_rsassa_pkcs1_v15_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_rsassa_pkcs1_v15_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_rsassa_pkcs1_v15_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_pss_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_pss_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_pss_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_pss_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_pss_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,
Cédric Meutera05cbec2020-04-25 15:02:34 +0200313 mbedtls_rsa_rsassa_pss_sign_ext( NULL, NULL, NULL,
314 0, sizeof( buf ), buf,
315 MBEDTLS_RSA_SALT_LEN_ANY,
316 buf ) );
317 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
318 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
319 0, sizeof( buf ), NULL,
320 MBEDTLS_RSA_SALT_LEN_ANY,
321 buf ) );
322 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
323 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
324 0, sizeof( buf ), buf,
325 MBEDTLS_RSA_SALT_LEN_ANY,
326 NULL ) );
327 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
328 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
329 MBEDTLS_MD_SHA1,
330 0, NULL,
331 MBEDTLS_RSA_SALT_LEN_ANY,
332 buf ) );
333
334 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500335 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
336 valid_mode,
337 0, sizeof( buf ), buf,
338 buf ) );
339 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
340 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
341 invalid_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 valid_mode,
347 0, sizeof( buf ), NULL,
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 ), buf,
353 NULL ) );
354 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
355 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
356 valid_mode,
357 MBEDTLS_MD_SHA1, 0, NULL,
358 buf ) );
359
360 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
361 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
362 NULL,
363 valid_mode,
364 0, sizeof( buf ), buf,
365 buf ) );
366 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
367 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
368 NULL,
369 invalid_mode,
370 0, sizeof( buf ), buf,
371 buf ) );
372 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
373 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
374 NULL,
375 valid_mode,
376 0, sizeof( buf ),
377 NULL, buf ) );
378 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
379 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
380 NULL,
381 valid_mode,
382 0, sizeof( buf ), buf,
383 NULL ) );
384 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
385 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
386 NULL,
387 valid_mode,
388 MBEDTLS_MD_SHA1,
389 0, NULL,
390 buf ) );
391
392 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
393 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
394 valid_mode,
395 0, sizeof( buf ),
396 buf, buf ) );
397 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
398 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
399 invalid_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 valid_mode,
405 0, sizeof( buf ),
406 NULL, 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 buf, NULL ) );
412 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
413 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
414 valid_mode,
415 MBEDTLS_MD_SHA1,
416 0, NULL,
417 buf ) );
418
419 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
420 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
421 valid_mode,
422 0, sizeof( buf ),
423 buf,
424 0, 0,
425 buf ) );
426 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
427 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
428 invalid_mode,
429 0, sizeof( buf ),
430 buf,
431 0, 0,
432 buf ) );
433 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
434 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
435 valid_mode,
436 0, sizeof( buf ),
437 NULL, 0, 0,
438 buf ) );
439 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
440 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
441 valid_mode,
442 0, sizeof( buf ),
443 buf, 0, 0,
444 NULL ) );
445 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
446 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
447 valid_mode,
448 MBEDTLS_MD_SHA1,
449 0, NULL,
450 0, 0,
451 buf ) );
452
453 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
454 mbedtls_rsa_copy( NULL, &ctx ) );
455 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
456 mbedtls_rsa_copy( &ctx, NULL ) );
457
458exit:
459 return;
460}
461/* END_CASE */
462
Paul Bakker33b43f12013-08-20 11:48:36 +0200463/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100464void rsa_init_free( int reinit )
465{
466 mbedtls_rsa_context ctx;
467
468 /* Double free is not explicitly documented to work, but we rely on it
469 * even inside the library so that you can call mbedtls_rsa_free()
470 * unconditionally on an error path without checking whether it has
471 * already been called in the success path. */
472
473 mbedtls_rsa_init( &ctx, 0, 0 );
474 mbedtls_rsa_free( &ctx );
475
476 if( reinit )
477 mbedtls_rsa_init( &ctx, 0, 0 );
478 mbedtls_rsa_free( &ctx );
479
480 /* This test case always succeeds, functionally speaking. A plausible
481 * bug might trigger an invalid pointer dereference or a memory leak. */
482 goto exit;
483}
484/* END_CASE */
485
486/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100487void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100488 int digest, int mod, int radix_P, char * input_P,
489 int radix_Q, char * input_Q, int radix_N,
490 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200491 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000492{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200493 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
494 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100496 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200497 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000498
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100499 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
500 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000502
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200503 memset( hash_result, 0x00, sizeof( hash_result ) );
504 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200505 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000506
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100507 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
508 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
509 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
510 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000511
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100512 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
513 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100514 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000516
Paul Bakker42a29bf2009-07-07 20:18:41 +0000517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100519 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 +0000520
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200521 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
522 &rnd_info, MBEDTLS_RSA_PRIVATE, digest,
523 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200524 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000525 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000526
Ronald Cronac6ae352020-06-26 14:33:03 +0200527 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
528 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000529 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000530
Paul Bakkerbd51b262014-07-10 15:26:12 +0200531exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100532 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
533 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000535}
Paul Bakker33b43f12013-08-20 11:48:36 +0200536/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000537
Paul Bakker33b43f12013-08-20 11:48:36 +0200538/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100539void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100540 int digest, int mod, int radix_N,
541 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100542 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000543{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200544 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000546
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100547 mbedtls_mpi N, E;
548
549 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200551 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000552
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100553 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
554 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
555 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
556 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000558
Paul Bakker42a29bf2009-07-07 20:18:41 +0000559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100561 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 +0000562
Azim Khand30ca132017-06-09 04:32:58 +0100563 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 +0100564
Paul Bakkerbd51b262014-07-10 15:26:12 +0200565exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100566 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000568}
Paul Bakker33b43f12013-08-20 11:48:36 +0200569/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000570
Paul Bakker821fb082009-07-12 13:26:42 +0000571
Paul Bakker33b43f12013-08-20 11:48:36 +0200572/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100573void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100574 int padding_mode, int mod, int radix_P,
575 char * input_P, int radix_Q, char * input_Q,
576 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200577 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000578{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200579 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100581 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200582 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100585 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
586 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000587
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200588 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200589 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000590
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100591 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
592 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
593 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
594 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000595
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100596 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
597 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100598 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000600
Paul Bakker821fb082009-07-12 13:26:42 +0000601
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200602 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
603 &rnd_info, MBEDTLS_RSA_PRIVATE,
604 MBEDTLS_MD_NONE, hash_result->len,
605 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000606
Paul Bakker821fb082009-07-12 13:26:42 +0000607
Ronald Cronac6ae352020-06-26 14:33:03 +0200608 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
609 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000610
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200611#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100612 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100614 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100615 int res;
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200616 memset( output, 0x00, sizeof( output) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100617
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100618 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200619 &mbedtls_test_rnd_pseudo_rand, &rnd_info,
620 MBEDTLS_RSA_PRIVATE, hash_result->len,
621 hash_result->x, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100622
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100623#if !defined(MBEDTLS_RSA_ALT)
624 TEST_ASSERT( res == 0 );
625#else
626 TEST_ASSERT( ( res == 0 ) ||
TRodziewiczb579ccd2021-04-13 14:28:28 +0200627 ( res == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100628#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100629
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100630 if( res == 0 )
631 {
Ronald Cronac6ae352020-06-26 14:33:03 +0200632 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200633 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200634 result_str->len ) == 0 );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100635 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100636 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200637#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100638
Paul Bakkerbd51b262014-07-10 15:26:12 +0200639exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100640 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
641 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000644}
Paul Bakker33b43f12013-08-20 11:48:36 +0200645/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000646
Paul Bakker33b43f12013-08-20 11:48:36 +0200647/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100648void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200649 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100650 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100651 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000652{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200653 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000655
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100656 mbedtls_mpi N, E;
657 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100660 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000661
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100662 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
663 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000664
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100665 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
666 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000668
Paul Bakker821fb082009-07-12 13:26:42 +0000669
Azim Khand30ca132017-06-09 04:32:58 +0100670 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 +0100671
Paul Bakkerbd51b262014-07-10 15:26:12 +0200672exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100673 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000675}
Paul Bakker33b43f12013-08-20 11:48:36 +0200676/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000677
Paul Bakker33b43f12013-08-20 11:48:36 +0200678/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100679void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100680 int mod, int radix_N, char * input_N,
681 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200682 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000683{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200684 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200686 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000687
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100688 mbedtls_mpi N, E;
689 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
690
Ronald Cron351f0ee2020-06-10 12:12:18 +0200691 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200694 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000695
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100696 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
697 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000698
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100699 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
700 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000702
Paul Bakker42a29bf2009-07-07 20:18:41 +0000703
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200704 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
705 &mbedtls_test_rnd_pseudo_rand,
706 &rnd_info, MBEDTLS_RSA_PUBLIC,
707 message_str->len, message_str->x,
708 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200709 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000710 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000711
Ronald Cronac6ae352020-06-26 14:33:03 +0200712 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
713 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000714 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100715
Paul Bakkerbd51b262014-07-10 15:26:12 +0200716exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100717 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000719}
Paul Bakker33b43f12013-08-20 11:48:36 +0200720/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000721
Paul Bakker33b43f12013-08-20 11:48:36 +0200722/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100723void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100724 int mod, int radix_N, char * input_N,
725 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200726 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000727{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200728 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000730
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100731 mbedtls_mpi N, E;
732
733 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200735 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000736
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100737 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
738 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000739
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100740 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
741 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000743
Paul Bakkera6656852010-07-18 19:47:14 +0000744
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200745 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
746 NULL, MBEDTLS_RSA_PUBLIC,
747 message_str->len, message_str->x,
748 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200749 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000750 {
Paul Bakkera6656852010-07-18 19:47:14 +0000751
Ronald Cronac6ae352020-06-26 14:33:03 +0200752 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
753 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000754 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100755
Paul Bakkerbd51b262014-07-10 15:26:12 +0200756exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100757 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000759}
Paul Bakker33b43f12013-08-20 11:48:36 +0200760/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000761
Paul Bakker33b43f12013-08-20 11:48:36 +0200762/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100763void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100764 int mod, int radix_P, char * input_P,
765 int radix_Q, char * input_Q, int radix_N,
766 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200767 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100768 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000769{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200770 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000772 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200773 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100774 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000775
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100776 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
777 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000780
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200781 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200782 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000783
Paul Bakker42a29bf2009-07-07 20:18:41 +0000784
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100785 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
786 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
787 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
788 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000789
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100790 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
791 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100792 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000794
Paul Bakker69998dd2009-07-11 19:15:20 +0000795 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000796
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200797 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100798 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200799 &output_len, message_str->x, output,
800 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200801 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000802 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000803
Ronald Cronac6ae352020-06-26 14:33:03 +0200804 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200805 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200806 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000807 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000808
Paul Bakkerbd51b262014-07-10 15:26:12 +0200809exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100810 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
811 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000813}
Paul Bakker33b43f12013-08-20 11:48:36 +0200814/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000815
Paul Bakker33b43f12013-08-20 11:48:36 +0200816/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100817void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100818 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200819 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000820{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200821 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000823
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100824 mbedtls_mpi N, E;
825
826 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
828 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200829 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000830
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100831 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
832 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000833
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100834 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
835 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000837
Paul Bakker821fb082009-07-12 13:26:42 +0000838
Azim Khand30ca132017-06-09 04:32:58 +0100839 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200840 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000841 {
Paul Bakker821fb082009-07-12 13:26:42 +0000842
Ronald Cronac6ae352020-06-26 14:33:03 +0200843 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
844 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000845 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100846
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100847 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200849 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100853
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200854 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100855 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100856 if( result == 0 )
857 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100858
Ronald Cronac6ae352020-06-26 14:33:03 +0200859 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
860 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100861 }
862
Paul Bakkerbd51b262014-07-10 15:26:12 +0200863exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100864 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_rsa_free( &ctx );
866 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000867}
Paul Bakker33b43f12013-08-20 11:48:36 +0200868/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000869
Paul Bakker33b43f12013-08-20 11:48:36 +0200870/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100871void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100872 char * input_P, int radix_Q, char * input_Q,
873 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200874 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100875 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000876{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200877 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100879 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200880 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200881 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000882
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100883 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
884 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
886 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000887
Ronald Cron351f0ee2020-06-10 12:12:18 +0200888 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000889
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100890 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
891 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
892 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
893 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000894
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100895 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
896 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100897 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000899
Paul Bakker821fb082009-07-12 13:26:42 +0000900
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200901 /* repeat three times to test updating of blinding values */
902 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000903 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200904 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200905 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
906 &rnd_info, message_str->x,
907 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200908 if( result == 0 )
909 {
Paul Bakker821fb082009-07-12 13:26:42 +0000910
Ronald Cronac6ae352020-06-26 14:33:03 +0200911 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200912 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200913 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200914 }
Paul Bakker821fb082009-07-12 13:26:42 +0000915 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000916
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100917 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200919 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100923
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200924 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200925 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
926 &rnd_info, message_str->x,
927 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100928 if( result == 0 )
929 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100930
Ronald Cronac6ae352020-06-26 14:33:03 +0200931 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200932 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200933 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100934 }
935
Paul Bakkerbd51b262014-07-10 15:26:12 +0200936exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100937 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
938 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000941}
Paul Bakker33b43f12013-08-20 11:48:36 +0200942/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000943
Paul Bakker33b43f12013-08-20 11:48:36 +0200944/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100945void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000946{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_rsa_context ctx;
948 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000951}
Paul Bakker33b43f12013-08-20 11:48:36 +0200952/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000953
Paul Bakker33b43f12013-08-20 11:48:36 +0200954/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100955void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
956 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000957{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100959 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000960
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100961 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000963
Paul Bakker33b43f12013-08-20 11:48:36 +0200964 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000965 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100966 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000967 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200968 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000969 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100970 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000971 }
972
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100973 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100975
Paul Bakkerbd51b262014-07-10 15:26:12 +0200976exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100977 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000979}
Paul Bakker33b43f12013-08-20 11:48:36 +0200980/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000981
Paul Bakker33b43f12013-08-20 11:48:36 +0200982/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100983void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
984 int radix_Q, char * input_Q, int radix_N,
985 char * input_N, int radix_E, char * input_E,
986 int radix_D, char * input_D, int radix_DP,
987 char * input_DP, int radix_DQ,
988 char * input_DQ, int radix_QP,
989 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000990{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000994
Paul Bakker33b43f12013-08-20 11:48:36 +0200995 ctx.len = mod / 8;
996 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000999 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001000 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001003 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001004 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001007 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001008 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001011 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001012 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +00001013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001015 }
Hanno Becker131134f2017-08-23 08:31:07 +01001016#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +02001017 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001020 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001021 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001024 }
Paul Bakker33b43f12013-08-20 11:48:36 +02001025 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +00001026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +00001028 }
Hanno Becker131134f2017-08-23 08:31:07 +01001029#else
1030 ((void) radix_DP); ((void) input_DP);
1031 ((void) radix_DQ); ((void) input_DQ);
1032 ((void) radix_QP); ((void) input_QP);
1033#endif
Paul Bakker821fb082009-07-12 13:26:42 +00001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001036
Paul Bakkerbd51b262014-07-10 15:26:12 +02001037exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +00001039}
Paul Bakker33b43f12013-08-20 11:48:36 +02001040/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001041
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001042/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001043void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
1044 int radix_Epub, char * input_Epub, int radix_P,
1045 char * input_P, int radix_Q, char * input_Q,
1046 int radix_N, char * input_N, int radix_E,
1047 char * input_E, int radix_D, char * input_D,
1048 int radix_DP, char * input_DP, int radix_DQ,
1049 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001050 int result )
1051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1055 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001056
1057 pub.len = mod / 8;
1058 prv.len = mod / 8;
1059
1060 if( strlen( input_Npub ) )
1061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001063 }
1064 if( strlen( input_Epub ) )
1065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001067 }
1068
1069 if( strlen( input_P ) )
1070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001072 }
1073 if( strlen( input_Q ) )
1074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001076 }
1077 if( strlen( input_N ) )
1078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001080 }
1081 if( strlen( input_E ) )
1082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001084 }
1085 if( strlen( input_D ) )
1086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001088 }
Hanno Becker131134f2017-08-23 08:31:07 +01001089#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001090 if( strlen( input_DP ) )
1091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001093 }
1094 if( strlen( input_DQ ) )
1095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001097 }
1098 if( strlen( input_QP ) )
1099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001101 }
Hanno Becker131134f2017-08-23 08:31:07 +01001102#else
1103 ((void) radix_DP); ((void) input_DP);
1104 ((void) radix_DQ); ((void) input_DQ);
1105 ((void) radix_QP); ((void) input_QP);
1106#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001109
1110exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 mbedtls_rsa_free( &pub );
1112 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001113}
1114/* END_CASE */
1115
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001116/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001118{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_rsa_context ctx;
1120 mbedtls_entropy_context entropy;
1121 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001122 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001123
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001124 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001126 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001127
Hanno Beckera47023e2017-12-22 17:08:03 +00001128 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1129 &entropy, (const unsigned char *) pers,
1130 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001133 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001136 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001137 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001138
Paul Bakkerbd51b262014-07-10 15:26:12 +02001139exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 mbedtls_rsa_free( &ctx );
1141 mbedtls_ctr_drbg_free( &ctr_drbg );
1142 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001143}
Paul Bakker33b43f12013-08-20 11:48:36 +02001144/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001145
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001146/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001147void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001148 int radix_D, char *input_D,
1149 int radix_E, char *input_E,
1150 int radix_P, char *output_P,
1151 int radix_Q, char *output_Q,
1152 int corrupt, int result )
1153{
1154 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1155
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001156 mbedtls_mpi_init( &N );
1157 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1158 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1159 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1160
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001161 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1162 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1163 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1164 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1165 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1166
1167 if( corrupt )
1168 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1169
1170 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001171 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001172
1173 if( !corrupt )
1174 {
1175 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1176 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1177 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1178 }
1179
1180exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001181 mbedtls_mpi_free( &N );
1182 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1183 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1184 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001185}
1186/* END_CASE */
1187
Hanno Becker6b4ce492017-08-23 11:00:21 +01001188/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001189void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1190 int radix_Q, char *input_Q,
1191 int radix_E, char *input_E,
1192 int radix_D, char *output_D,
1193 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001194{
1195 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1196
1197 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1198 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1199 mbedtls_mpi_init( &E );
1200 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1201
1202 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1203 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1204 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1205 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1206
1207 if( corrupt )
1208 {
1209 /* Make E even */
1210 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1211 }
1212
1213 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001214 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1215 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001216
1217 if( !corrupt )
1218 {
1219 /*
1220 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1221 */
1222
1223 /* Replace P,Q by P-1, Q-1 */
1224 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1225 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1226
1227 /* Check D == Dp modulo P-1 */
1228 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1229 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1230 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1231
1232 /* Check D == Dp modulo Q-1 */
1233 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1234 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1235 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1236 }
1237
1238exit:
1239
1240 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1241 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1242 mbedtls_mpi_free( &E );
1243 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1244}
1245/* END_CASE */
1246
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001247/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001248void mbedtls_rsa_import( int radix_N, char *input_N,
1249 int radix_P, char *input_P,
1250 int radix_Q, char *input_Q,
1251 int radix_D, char *input_D,
1252 int radix_E, char *input_E,
1253 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001254 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001255 int res_check,
1256 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001257{
1258 mbedtls_mpi N, P, Q, D, E;
1259 mbedtls_rsa_context ctx;
1260
Hanno Beckere1582a82017-09-29 11:51:05 +01001261 /* Buffers used for encryption-decryption test */
1262 unsigned char *buf_orig = NULL;
1263 unsigned char *buf_enc = NULL;
1264 unsigned char *buf_dec = NULL;
1265
Hanno Beckerc77ab892017-08-23 11:01:06 +01001266 mbedtls_entropy_context entropy;
1267 mbedtls_ctr_drbg_context ctr_drbg;
1268 const char *pers = "test_suite_rsa";
1269
Hanno Becker4d6e8342017-09-29 11:50:18 +01001270 const int have_N = ( strlen( input_N ) > 0 );
1271 const int have_P = ( strlen( input_P ) > 0 );
1272 const int have_Q = ( strlen( input_Q ) > 0 );
1273 const int have_D = ( strlen( input_D ) > 0 );
1274 const int have_E = ( strlen( input_E ) > 0 );
1275
Hanno Beckerc77ab892017-08-23 11:01:06 +01001276 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001277 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001278 mbedtls_rsa_init( &ctx, 0, 0 );
1279
1280 mbedtls_mpi_init( &N );
1281 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1282 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1283
Hanno Beckerd4d60572018-01-10 07:12:01 +00001284 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1285 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1286
Hanno Becker4d6e8342017-09-29 11:50:18 +01001287 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001288 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1289
Hanno Becker4d6e8342017-09-29 11:50:18 +01001290 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001291 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1292
Hanno Becker4d6e8342017-09-29 11:50:18 +01001293 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001294 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1295
Hanno Becker4d6e8342017-09-29 11:50:18 +01001296 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001297 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1298
Hanno Becker4d6e8342017-09-29 11:50:18 +01001299 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001300 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1301
1302 if( !successive )
1303 {
1304 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001305 have_N ? &N : NULL,
1306 have_P ? &P : NULL,
1307 have_Q ? &Q : NULL,
1308 have_D ? &D : NULL,
1309 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001310 }
1311 else
1312 {
1313 /* Import N, P, Q, D, E separately.
1314 * This should make no functional difference. */
1315
1316 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001317 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001318 NULL, NULL, NULL, NULL ) == 0 );
1319
1320 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1321 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001322 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001323 NULL, NULL, NULL ) == 0 );
1324
1325 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1326 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001327 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001328 NULL, NULL ) == 0 );
1329
1330 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1331 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001332 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001333 NULL ) == 0 );
1334
1335 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1336 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001337 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001338 }
1339
Hanno Becker04877a42017-10-11 10:01:33 +01001340 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001341
Hanno Beckere1582a82017-09-29 11:51:05 +01001342 /* On expected success, perform some public and private
1343 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001344 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001345 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001346 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001347 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1348 else
1349 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1350
1351 if( res_check != 0 )
1352 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001353
1354 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1355 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1356 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1357 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1358 goto exit;
1359
1360 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1361 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1362
1363 /* Make sure the number we're generating is smaller than the modulus */
1364 buf_orig[0] = 0x00;
1365
1366 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1367
1368 if( is_priv )
1369 {
1370 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1371 &ctr_drbg, buf_enc,
1372 buf_dec ) == 0 );
1373
1374 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1375 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1376 }
1377 }
1378
Hanno Beckerc77ab892017-08-23 11:01:06 +01001379exit:
1380
Hanno Beckere1582a82017-09-29 11:51:05 +01001381 mbedtls_free( buf_orig );
1382 mbedtls_free( buf_enc );
1383 mbedtls_free( buf_dec );
1384
Hanno Beckerc77ab892017-08-23 11:01:06 +01001385 mbedtls_rsa_free( &ctx );
1386
1387 mbedtls_ctr_drbg_free( &ctr_drbg );
1388 mbedtls_entropy_free( &entropy );
1389
1390 mbedtls_mpi_free( &N );
1391 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1392 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1393}
1394/* END_CASE */
1395
Hanno Becker417f2d62017-08-23 11:44:51 +01001396/* BEGIN_CASE */
1397void mbedtls_rsa_export( int radix_N, char *input_N,
1398 int radix_P, char *input_P,
1399 int radix_Q, char *input_Q,
1400 int radix_D, char *input_D,
1401 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001402 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001403 int successive )
1404{
1405 /* Original MPI's with which we set up the RSA context */
1406 mbedtls_mpi N, P, Q, D, E;
1407
1408 /* Exported MPI's */
1409 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1410
1411 const int have_N = ( strlen( input_N ) > 0 );
1412 const int have_P = ( strlen( input_P ) > 0 );
1413 const int have_Q = ( strlen( input_Q ) > 0 );
1414 const int have_D = ( strlen( input_D ) > 0 );
1415 const int have_E = ( strlen( input_E ) > 0 );
1416
Hanno Becker417f2d62017-08-23 11:44:51 +01001417 mbedtls_rsa_context ctx;
1418
1419 mbedtls_rsa_init( &ctx, 0, 0 );
1420
1421 mbedtls_mpi_init( &N );
1422 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1423 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1424
1425 mbedtls_mpi_init( &Ne );
1426 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1427 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1428
1429 /* Setup RSA context */
1430
1431 if( have_N )
1432 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1433
1434 if( have_P )
1435 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1436
1437 if( have_Q )
1438 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1439
1440 if( have_D )
1441 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1442
1443 if( have_E )
1444 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1445
1446 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1447 strlen( input_N ) ? &N : NULL,
1448 strlen( input_P ) ? &P : NULL,
1449 strlen( input_Q ) ? &Q : NULL,
1450 strlen( input_D ) ? &D : NULL,
1451 strlen( input_E ) ? &E : NULL ) == 0 );
1452
Hanno Becker7f25f852017-10-10 16:56:22 +01001453 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001454
1455 /*
1456 * Export parameters and compare to original ones.
1457 */
1458
1459 /* N and E must always be present. */
1460 if( !successive )
1461 {
1462 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1463 }
1464 else
1465 {
1466 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1467 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1468 }
1469 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1470 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1471
1472 /* If we were providing enough information to setup a complete private context,
1473 * we expect to be able to export all core parameters. */
1474
1475 if( is_priv )
1476 {
1477 if( !successive )
1478 {
1479 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1480 &De, NULL ) == 0 );
1481 }
1482 else
1483 {
1484 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1485 NULL, NULL ) == 0 );
1486 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1487 NULL, NULL ) == 0 );
1488 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1489 &De, NULL ) == 0 );
1490 }
1491
1492 if( have_P )
1493 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1494
1495 if( have_Q )
1496 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1497
1498 if( have_D )
1499 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1500
1501 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001502 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1503 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001504 }
1505
1506exit:
1507
1508 mbedtls_rsa_free( &ctx );
1509
1510 mbedtls_mpi_free( &N );
1511 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1512 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1513
1514 mbedtls_mpi_free( &Ne );
1515 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1516 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1517}
1518/* END_CASE */
1519
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001520/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001521void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1522 int radix_P, char *input_P,
1523 int radix_Q, char *input_Q,
1524 int radix_D, char *input_D,
1525 int radix_E, char *input_E,
1526 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001527{
1528 /* Original MPI's with which we set up the RSA context */
1529 mbedtls_mpi N, P, Q, D, E;
1530
1531 const int have_N = ( strlen( input_N ) > 0 );
1532 const int have_P = ( strlen( input_P ) > 0 );
1533 const int have_Q = ( strlen( input_Q ) > 0 );
1534 const int have_D = ( strlen( input_D ) > 0 );
1535 const int have_E = ( strlen( input_E ) > 0 );
1536
1537 mbedtls_entropy_context entropy;
1538 mbedtls_ctr_drbg_context ctr_drbg;
1539 const char *pers = "test_suite_rsa";
1540
1541 mbedtls_mpi_init( &N );
1542 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1543 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1544
1545 mbedtls_ctr_drbg_init( &ctr_drbg );
1546 mbedtls_entropy_init( &entropy );
1547 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1548 &entropy, (const unsigned char *) pers,
1549 strlen( pers ) ) == 0 );
1550
1551 if( have_N )
1552 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1553
1554 if( have_P )
1555 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1556
1557 if( have_Q )
1558 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1559
1560 if( have_D )
1561 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1562
1563 if( have_E )
1564 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1565
Hanno Becker750e8b42017-08-25 07:54:27 +01001566 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1567 have_P ? &P : NULL,
1568 have_Q ? &Q : NULL,
1569 have_D ? &D : NULL,
1570 have_E ? &E : NULL,
1571 prng ? mbedtls_ctr_drbg_random : NULL,
1572 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001573exit:
1574
1575 mbedtls_ctr_drbg_free( &ctr_drbg );
1576 mbedtls_entropy_free( &entropy );
1577
1578 mbedtls_mpi_free( &N );
1579 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1580 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1581}
1582/* END_CASE */
1583
Hanno Beckerc77ab892017-08-23 11:01:06 +01001584/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001585void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1586 data_t *input_Q, data_t *input_D,
1587 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001588 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001589{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001590 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001591 unsigned char bufNe[256];
1592 unsigned char bufPe[128];
1593 unsigned char bufQe[128];
1594 unsigned char bufDe[256];
1595 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001596
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001597 mbedtls_rsa_context ctx;
1598
1599 mbedtls_rsa_init( &ctx, 0, 0 );
1600
1601 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001602 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001603 input_N->len ? input_N->x : NULL, input_N->len,
1604 input_P->len ? input_P->x : NULL, input_P->len,
1605 input_Q->len ? input_Q->x : NULL, input_Q->len,
1606 input_D->len ? input_D->x : NULL, input_D->len,
1607 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001608
Hanno Becker7f25f852017-10-10 16:56:22 +01001609 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001610
1611 /*
1612 * Export parameters and compare to original ones.
1613 */
1614
1615 /* N and E must always be present. */
1616 if( !successive )
1617 {
Azim Khand30ca132017-06-09 04:32:58 +01001618 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001619 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001620 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001621 }
1622 else
1623 {
Azim Khand30ca132017-06-09 04:32:58 +01001624 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001625 NULL, 0, NULL, 0, NULL, 0,
1626 NULL, 0 ) == 0 );
1627 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1628 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001629 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001630 }
Azim Khand30ca132017-06-09 04:32:58 +01001631 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1632 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001633
1634 /* If we were providing enough information to setup a complete private context,
1635 * we expect to be able to export all core parameters. */
1636
1637 if( is_priv )
1638 {
1639 if( !successive )
1640 {
1641 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001642 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1643 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1644 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001645 NULL, 0 ) == 0 );
1646 }
1647 else
1648 {
1649 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001650 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001651 NULL, 0, NULL, 0,
1652 NULL, 0 ) == 0 );
1653
1654 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001655 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001656 NULL, 0, NULL, 0 ) == 0 );
1657
Azim Khand30ca132017-06-09 04:32:58 +01001658 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1659 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001660 NULL, 0 ) == 0 );
1661 }
1662
Azim Khand30ca132017-06-09 04:32:58 +01001663 if( input_P->len )
1664 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001665
Azim Khand30ca132017-06-09 04:32:58 +01001666 if( input_Q->len )
1667 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001668
Azim Khand30ca132017-06-09 04:32:58 +01001669 if( input_D->len )
1670 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001671
1672 }
1673
1674exit:
1675 mbedtls_rsa_free( &ctx );
1676}
1677/* END_CASE */
1678
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001679/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001680void mbedtls_rsa_import_raw( data_t *input_N,
1681 data_t *input_P, data_t *input_Q,
1682 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001683 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001684 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001685 int res_check,
1686 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001687{
Hanno Beckere1582a82017-09-29 11:51:05 +01001688 /* Buffers used for encryption-decryption test */
1689 unsigned char *buf_orig = NULL;
1690 unsigned char *buf_enc = NULL;
1691 unsigned char *buf_dec = NULL;
1692
Hanno Beckerc77ab892017-08-23 11:01:06 +01001693 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001694 mbedtls_entropy_context entropy;
1695 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001696
Hanno Beckerc77ab892017-08-23 11:01:06 +01001697 const char *pers = "test_suite_rsa";
1698
1699 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001700 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001701 mbedtls_rsa_init( &ctx, 0, 0 );
1702
Hanno Beckerc77ab892017-08-23 11:01:06 +01001703 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1704 &entropy, (const unsigned char *) pers,
1705 strlen( pers ) ) == 0 );
1706
Hanno Beckerc77ab892017-08-23 11:01:06 +01001707 if( !successive )
1708 {
1709 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001710 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1711 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1712 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1713 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1714 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001715 }
1716 else
1717 {
1718 /* Import N, P, Q, D, E separately.
1719 * This should make no functional difference. */
1720
1721 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001722 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001723 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1724
1725 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1726 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001727 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001728 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1729
1730 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1731 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001732 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001733 NULL, 0, NULL, 0 ) == 0 );
1734
1735 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1736 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001737 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001738 NULL, 0 ) == 0 );
1739
1740 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1741 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001742 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001743 }
1744
Hanno Becker04877a42017-10-11 10:01:33 +01001745 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001746
Hanno Beckere1582a82017-09-29 11:51:05 +01001747 /* On expected success, perform some public and private
1748 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001749 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001750 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001751 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001752 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1753 else
1754 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1755
1756 if( res_check != 0 )
1757 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001758
1759 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1760 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1761 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1762 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1763 goto exit;
1764
1765 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1766 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1767
1768 /* Make sure the number we're generating is smaller than the modulus */
1769 buf_orig[0] = 0x00;
1770
1771 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1772
1773 if( is_priv )
1774 {
1775 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1776 &ctr_drbg, buf_enc,
1777 buf_dec ) == 0 );
1778
1779 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1780 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1781 }
1782 }
1783
Hanno Beckerc77ab892017-08-23 11:01:06 +01001784exit:
1785
Hanno Becker3f3ae852017-10-02 10:08:39 +01001786 mbedtls_free( buf_orig );
1787 mbedtls_free( buf_enc );
1788 mbedtls_free( buf_dec );
1789
Hanno Beckerc77ab892017-08-23 11:01:06 +01001790 mbedtls_rsa_free( &ctx );
1791
1792 mbedtls_ctr_drbg_free( &ctr_drbg );
1793 mbedtls_entropy_free( &entropy );
1794
1795}
1796/* END_CASE */
1797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001799void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001800{
Andres AG93012e82016-09-09 09:10:28 +01001801 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001802}
Paul Bakker33b43f12013-08-20 11:48:36 +02001803/* END_CASE */