blob: d3b65b2056346fdd19671d056f529d8c1d82f097 [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,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100119 NULL, sizeof( buf ),
120 buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
122 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100123 NULL, sizeof( buf ),
124 NULL, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500125 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
126 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100127 NULL, sizeof( buf ),
128 buf, NULL ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500129
130 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
131 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
132 valid_mode,
133 buf, sizeof( buf ),
134 sizeof( buf ), buf,
135 buf ) );
136 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
137 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
138 invalid_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 valid_mode,
145 NULL, 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 buf, sizeof( buf ),
152 sizeof( buf ), NULL,
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 ), buf,
159 NULL ) );
160
161 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
162 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100163 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500164 buf, buf, 42 ) );
165 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
166 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100167 NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500168 buf, buf, 42 ) );
169 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
170 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100171 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500172 NULL, buf, 42 ) );
173 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
174 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100175 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500176 buf, NULL, 42 ) );
177
178 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
179 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100180 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500181 buf, buf, 42 ) );
182 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
183 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100184 NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500185 buf, buf, 42 ) );
186 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
187 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100188 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500189 NULL, buf, 42 ) );
190 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
191 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100192 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500193 buf, NULL, 42 ) );
194
195 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
196 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 buf, sizeof( buf ),
198 &olen,
199 buf, buf, 42 ) );
200 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
201 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 NULL, sizeof( buf ),
203 NULL,
204 buf, buf, 42 ) );
205 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
206 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500207 buf, sizeof( buf ),
208 &olen,
209 NULL, buf, 42 ) );
210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
211 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 buf, sizeof( buf ),
213 &olen,
214 buf, NULL, 42 ) );
215
216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
217 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
218 valid_mode,
219 0, sizeof( buf ), buf,
220 buf ) );
221 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
222 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
223 invalid_mode,
224 0, sizeof( buf ), buf,
225 buf ) );
226 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
227 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
228 valid_mode,
229 0, sizeof( buf ), NULL,
230 buf ) );
231 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
232 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
233 valid_mode,
234 0, sizeof( buf ), buf,
235 NULL ) );
236 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
237 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
238 valid_mode,
239 MBEDTLS_MD_SHA1,
240 0, NULL,
241 buf ) );
242
243 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
244 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
245 valid_mode,
246 0, sizeof( buf ), buf,
247 buf ) );
248 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
249 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
250 invalid_mode,
251 0, sizeof( buf ), buf,
252 buf ) );
253 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
254 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
255 valid_mode,
256 0, sizeof( buf ), NULL,
257 buf ) );
258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
259 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
260 valid_mode,
261 0, sizeof( buf ), buf,
262 NULL ) );
263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
264 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
265 valid_mode,
266 MBEDTLS_MD_SHA1,
267 0, NULL,
268 buf ) );
269
270 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
271 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
272 valid_mode,
273 0, sizeof( buf ), buf,
274 buf ) );
275 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
276 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
277 invalid_mode,
278 0, sizeof( buf ), buf,
279 buf ) );
280 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
281 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
282 valid_mode,
283 0, sizeof( buf ), NULL,
284 buf ) );
285 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
286 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
287 valid_mode,
288 0, sizeof( buf ), buf,
289 NULL ) );
290 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
291 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
292 valid_mode,
293 MBEDTLS_MD_SHA1,
294 0, NULL,
295 buf ) );
296
297 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Cédric Meutera05cbec2020-04-25 15:02:34 +0200298 mbedtls_rsa_rsassa_pss_sign_ext( NULL, NULL, NULL,
299 0, sizeof( buf ), buf,
300 MBEDTLS_RSA_SALT_LEN_ANY,
301 buf ) );
302 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
303 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
304 0, sizeof( buf ), NULL,
305 MBEDTLS_RSA_SALT_LEN_ANY,
306 buf ) );
307 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
308 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
309 0, sizeof( buf ), buf,
310 MBEDTLS_RSA_SALT_LEN_ANY,
311 NULL ) );
312 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
313 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
314 MBEDTLS_MD_SHA1,
315 0, NULL,
316 MBEDTLS_RSA_SALT_LEN_ANY,
317 buf ) );
318
319 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500320 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
321 valid_mode,
322 0, sizeof( buf ), buf,
323 buf ) );
324 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
325 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
326 invalid_mode,
327 0, sizeof( buf ), buf,
328 buf ) );
329 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
330 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
331 valid_mode,
332 0, sizeof( buf ), NULL,
333 buf ) );
334 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
335 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
336 valid_mode,
337 0, sizeof( buf ), buf,
338 NULL ) );
339 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
340 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
341 valid_mode,
342 MBEDTLS_MD_SHA1, 0, NULL,
343 buf ) );
344
345 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
346 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
347 NULL,
348 valid_mode,
349 0, sizeof( buf ), buf,
350 buf ) );
351 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
352 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
353 NULL,
354 invalid_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 valid_mode,
361 0, sizeof( buf ),
362 NULL, 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 ), buf,
368 NULL ) );
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 MBEDTLS_MD_SHA1,
374 0, NULL,
375 buf ) );
376
377 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
378 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
379 valid_mode,
380 0, sizeof( buf ),
381 buf, buf ) );
382 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
383 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
384 invalid_mode,
385 0, sizeof( buf ),
386 buf, buf ) );
387 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
388 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
389 valid_mode,
390 0, sizeof( buf ),
391 NULL, buf ) );
392 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
393 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
394 valid_mode,
395 0, sizeof( buf ),
396 buf, NULL ) );
397 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
398 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
399 valid_mode,
400 MBEDTLS_MD_SHA1,
401 0, NULL,
402 buf ) );
403
404 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
405 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
406 valid_mode,
407 0, sizeof( buf ),
408 buf,
409 0, 0,
410 buf ) );
411 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
412 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
413 invalid_mode,
414 0, sizeof( buf ),
415 buf,
416 0, 0,
417 buf ) );
418 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
419 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
420 valid_mode,
421 0, sizeof( buf ),
422 NULL, 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 buf, 0, 0,
429 NULL ) );
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 MBEDTLS_MD_SHA1,
434 0, NULL,
435 0, 0,
436 buf ) );
437
438 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
439 mbedtls_rsa_copy( NULL, &ctx ) );
440 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
441 mbedtls_rsa_copy( &ctx, NULL ) );
442
443exit:
444 return;
445}
446/* END_CASE */
447
Paul Bakker33b43f12013-08-20 11:48:36 +0200448/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100449void rsa_init_free( int reinit )
450{
451 mbedtls_rsa_context ctx;
452
453 /* Double free is not explicitly documented to work, but we rely on it
454 * even inside the library so that you can call mbedtls_rsa_free()
455 * unconditionally on an error path without checking whether it has
456 * already been called in the success path. */
457
458 mbedtls_rsa_init( &ctx, 0, 0 );
459 mbedtls_rsa_free( &ctx );
460
461 if( reinit )
462 mbedtls_rsa_init( &ctx, 0, 0 );
463 mbedtls_rsa_free( &ctx );
464
465 /* This test case always succeeds, functionally speaking. A plausible
466 * bug might trigger an invalid pointer dereference or a memory leak. */
467 goto exit;
468}
469/* END_CASE */
470
471/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100472void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100473 int digest, int mod, int radix_P, char * input_P,
474 int radix_Q, char * input_Q, int radix_N,
475 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200476 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000477{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200478 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
479 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100481 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200482 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000483
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100484 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
485 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000487
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200488 memset( hash_result, 0x00, sizeof( hash_result ) );
489 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200490 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000491
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100492 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
493 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
494 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
495 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000496
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100497 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
498 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100499 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000501
Paul Bakker42a29bf2009-07-07 20:18:41 +0000502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100504 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 +0000505
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200506 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
507 &rnd_info, MBEDTLS_RSA_PRIVATE, digest,
508 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200509 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000510 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000511
Ronald Cronac6ae352020-06-26 14:33:03 +0200512 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
513 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000514 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000515
Paul Bakkerbd51b262014-07-10 15:26:12 +0200516exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100517 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
518 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000520}
Paul Bakker33b43f12013-08-20 11:48:36 +0200521/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000522
Paul Bakker33b43f12013-08-20 11:48:36 +0200523/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100524void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100525 int digest, int mod, int radix_N,
526 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100527 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000528{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200529 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000531
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100532 mbedtls_mpi N, E;
533
534 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200536 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000537
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100538 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
539 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
540 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
541 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000543
Paul Bakker42a29bf2009-07-07 20:18:41 +0000544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100546 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 +0000547
Azim Khand30ca132017-06-09 04:32:58 +0100548 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 +0100549
Paul Bakkerbd51b262014-07-10 15:26:12 +0200550exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100551 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000553}
Paul Bakker33b43f12013-08-20 11:48:36 +0200554/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000555
Paul Bakker821fb082009-07-12 13:26:42 +0000556
Paul Bakker33b43f12013-08-20 11:48:36 +0200557/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100558void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100559 int padding_mode, int mod, int radix_P,
560 char * input_P, int radix_Q, char * input_Q,
561 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200562 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000563{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200564 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100566 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200567 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100570 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
571 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000572
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200573 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200574 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000575
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100576 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
577 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
578 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
579 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000580
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100581 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
582 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100583 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000585
Paul Bakker821fb082009-07-12 13:26:42 +0000586
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200587 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
588 &rnd_info, MBEDTLS_RSA_PRIVATE,
589 MBEDTLS_MD_NONE, hash_result->len,
590 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000591
Paul Bakker821fb082009-07-12 13:26:42 +0000592
Ronald Cronac6ae352020-06-26 14:33:03 +0200593 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
594 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000595
Paul Bakkerbd51b262014-07-10 15:26:12 +0200596exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100597 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
598 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000601}
Paul Bakker33b43f12013-08-20 11:48:36 +0200602/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000603
Paul Bakker33b43f12013-08-20 11:48:36 +0200604/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100605void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200606 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100607 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100608 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000609{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200610 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000612
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100613 mbedtls_mpi N, E;
614 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100617 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000618
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100619 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
620 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000621
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100622 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
623 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000625
Paul Bakker821fb082009-07-12 13:26:42 +0000626
Azim Khand30ca132017-06-09 04:32:58 +0100627 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 +0100628
Paul Bakkerbd51b262014-07-10 15:26:12 +0200629exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100630 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000632}
Paul Bakker33b43f12013-08-20 11:48:36 +0200633/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000634
Paul Bakker33b43f12013-08-20 11:48:36 +0200635/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100636void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100637 int mod, int radix_N, char * input_N,
638 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200639 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000640{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200641 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200643 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000644
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100645 mbedtls_mpi N, E;
646 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
647
Ronald Cron351f0ee2020-06-10 12:12:18 +0200648 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200651 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000652
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100653 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
654 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000655
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100656 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
657 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000659
Paul Bakker42a29bf2009-07-07 20:18:41 +0000660
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200661 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
662 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100663 &rnd_info, message_str->len,
664 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200665 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200666 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000667 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000668
Ronald Cronac6ae352020-06-26 14:33:03 +0200669 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
670 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000671 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100672
Paul Bakkerbd51b262014-07-10 15:26:12 +0200673exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100674 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000676}
Paul Bakker33b43f12013-08-20 11:48:36 +0200677/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000678
Paul Bakker33b43f12013-08-20 11:48:36 +0200679/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100680void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100681 int mod, int radix_N, char * input_N,
682 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200683 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000684{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200685 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000687
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100688 mbedtls_mpi N, E;
689
690 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200692 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000693
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100694 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
695 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000696
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100697 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
698 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000700
Paul Bakkera6656852010-07-18 19:47:14 +0000701
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200702 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100703 NULL, message_str->len,
704 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200705 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200706 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000707 {
Paul Bakkera6656852010-07-18 19:47:14 +0000708
Ronald Cronac6ae352020-06-26 14:33:03 +0200709 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
710 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000711 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100712
Paul Bakkerbd51b262014-07-10 15:26:12 +0200713exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100714 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000716}
Paul Bakker33b43f12013-08-20 11:48:36 +0200717/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000718
Paul Bakker33b43f12013-08-20 11:48:36 +0200719/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100720void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100721 int mod, int radix_P, char * input_P,
722 int radix_Q, char * input_Q, int radix_N,
723 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200724 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100725 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000726{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200727 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000729 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200730 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100731 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000732
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100733 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
734 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000737
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200738 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200739 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000740
Paul Bakker42a29bf2009-07-07 20:18:41 +0000741
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100742 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
743 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
744 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
745 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000746
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100747 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
748 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100749 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000751
Paul Bakker69998dd2009-07-11 19:15:20 +0000752 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000753
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200754 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100755 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200756 &output_len, message_str->x, output,
757 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200758 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000759 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000760
Ronald Cronac6ae352020-06-26 14:33:03 +0200761 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200762 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200763 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000764 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000765
Paul Bakkerbd51b262014-07-10 15:26:12 +0200766exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100767 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
768 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000770}
Paul Bakker33b43f12013-08-20 11:48:36 +0200771/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000772
Paul Bakker33b43f12013-08-20 11:48:36 +0200773/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100774void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100775 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200776 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000777{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200778 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000780
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100781 mbedtls_mpi N, E;
782
783 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
785 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200786 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000787
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100788 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
789 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000790
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100791 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
792 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000794
Paul Bakker821fb082009-07-12 13:26:42 +0000795
Azim Khand30ca132017-06-09 04:32:58 +0100796 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200797 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000798 {
Paul Bakker821fb082009-07-12 13:26:42 +0000799
Ronald Cronac6ae352020-06-26 14:33:03 +0200800 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
801 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000802 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100803
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100804 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200806 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100810
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200811 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100812 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100813 if( result == 0 )
814 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100815
Ronald Cronac6ae352020-06-26 14:33:03 +0200816 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
817 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100818 }
819
Paul Bakkerbd51b262014-07-10 15:26:12 +0200820exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100821 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_rsa_free( &ctx );
823 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000824}
Paul Bakker33b43f12013-08-20 11:48:36 +0200825/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000826
Paul Bakker33b43f12013-08-20 11:48:36 +0200827/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100828void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100829 char * input_P, int radix_Q, char * input_Q,
830 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200831 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100832 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000833{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200834 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100836 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200837 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200838 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000839
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100840 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
841 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
843 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000844
Ronald Cron351f0ee2020-06-10 12:12:18 +0200845 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000846
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100847 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
848 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
849 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
850 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000851
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100852 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
853 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100854 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000856
Paul Bakker821fb082009-07-12 13:26:42 +0000857
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200858 /* repeat three times to test updating of blinding values */
859 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000860 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200861 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200862 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
863 &rnd_info, message_str->x,
864 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200865 if( result == 0 )
866 {
Paul Bakker821fb082009-07-12 13:26:42 +0000867
Ronald Cronac6ae352020-06-26 14:33:03 +0200868 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200869 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200870 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200871 }
Paul Bakker821fb082009-07-12 13:26:42 +0000872 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000873
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100874 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200876 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100880
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200881 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200882 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
883 &rnd_info, message_str->x,
884 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100885 if( result == 0 )
886 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100887
Ronald Cronac6ae352020-06-26 14:33:03 +0200888 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200889 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200890 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100891 }
892
Paul Bakkerbd51b262014-07-10 15:26:12 +0200893exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100894 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
895 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000898}
Paul Bakker33b43f12013-08-20 11:48:36 +0200899/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000900
Paul Bakker33b43f12013-08-20 11:48:36 +0200901/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100902void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_rsa_context ctx;
905 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000908}
Paul Bakker33b43f12013-08-20 11:48:36 +0200909/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000910
Paul Bakker33b43f12013-08-20 11:48:36 +0200911/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100912void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
913 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000914{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100916 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000917
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100918 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000920
Paul Bakker33b43f12013-08-20 11:48:36 +0200921 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000922 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100923 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000924 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200925 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000926 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100927 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000928 }
929
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100930 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100932
Paul Bakkerbd51b262014-07-10 15:26:12 +0200933exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100934 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000936}
Paul Bakker33b43f12013-08-20 11:48:36 +0200937/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000938
Paul Bakker33b43f12013-08-20 11:48:36 +0200939/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100940void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
941 int radix_Q, char * input_Q, int radix_N,
942 char * input_N, int radix_E, char * input_E,
943 int radix_D, char * input_D, int radix_DP,
944 char * input_DP, int radix_DQ,
945 char * input_DQ, int radix_QP,
946 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000947{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000951
Paul Bakker33b43f12013-08-20 11:48:36 +0200952 ctx.len = mod / 8;
953 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000956 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200957 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000960 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200961 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000964 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200965 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000968 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200969 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000972 }
Hanno Becker131134f2017-08-23 08:31:07 +0100973#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200974 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000977 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200978 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000981 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200982 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000985 }
Hanno Becker131134f2017-08-23 08:31:07 +0100986#else
987 ((void) radix_DP); ((void) input_DP);
988 ((void) radix_DQ); ((void) input_DQ);
989 ((void) radix_QP); ((void) input_QP);
990#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100993
Paul Bakkerbd51b262014-07-10 15:26:12 +0200994exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000996}
Paul Bakker33b43f12013-08-20 11:48:36 +0200997/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000998
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100999/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +01001000void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
1001 int radix_Epub, char * input_Epub, int radix_P,
1002 char * input_P, int radix_Q, char * input_Q,
1003 int radix_N, char * input_N, int radix_E,
1004 char * input_E, int radix_D, char * input_D,
1005 int radix_DP, char * input_DP, int radix_DQ,
1006 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001007 int result )
1008{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1012 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001013
1014 pub.len = mod / 8;
1015 prv.len = mod / 8;
1016
1017 if( strlen( input_Npub ) )
1018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001020 }
1021 if( strlen( input_Epub ) )
1022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001024 }
1025
1026 if( strlen( input_P ) )
1027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001029 }
1030 if( strlen( input_Q ) )
1031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001033 }
1034 if( strlen( input_N ) )
1035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001037 }
1038 if( strlen( input_E ) )
1039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001041 }
1042 if( strlen( input_D ) )
1043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001045 }
Hanno Becker131134f2017-08-23 08:31:07 +01001046#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001047 if( strlen( input_DP ) )
1048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001050 }
1051 if( strlen( input_DQ ) )
1052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001054 }
1055 if( strlen( input_QP ) )
1056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001058 }
Hanno Becker131134f2017-08-23 08:31:07 +01001059#else
1060 ((void) radix_DP); ((void) input_DP);
1061 ((void) radix_DQ); ((void) input_DQ);
1062 ((void) radix_QP); ((void) input_QP);
1063#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001066
1067exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_rsa_free( &pub );
1069 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001070}
1071/* END_CASE */
1072
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001073/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_rsa_context ctx;
1077 mbedtls_entropy_context entropy;
1078 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001079 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001080
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001081 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001083 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001084
Hanno Beckera47023e2017-12-22 17:08:03 +00001085 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1086 &entropy, (const unsigned char *) pers,
1087 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001090 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001093 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001094 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001095
Paul Bakkerbd51b262014-07-10 15:26:12 +02001096exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 mbedtls_rsa_free( &ctx );
1098 mbedtls_ctr_drbg_free( &ctr_drbg );
1099 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001100}
Paul Bakker33b43f12013-08-20 11:48:36 +02001101/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001102
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001103/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001104void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001105 int radix_D, char *input_D,
1106 int radix_E, char *input_E,
1107 int radix_P, char *output_P,
1108 int radix_Q, char *output_Q,
1109 int corrupt, int result )
1110{
1111 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1112
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001113 mbedtls_mpi_init( &N );
1114 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1115 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1116 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1117
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001118 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1119 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1120 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1121 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1122 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1123
1124 if( corrupt )
1125 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1126
1127 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001128 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001129
1130 if( !corrupt )
1131 {
1132 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1133 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1134 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1135 }
1136
1137exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001138 mbedtls_mpi_free( &N );
1139 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1140 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1141 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001142}
1143/* END_CASE */
1144
Hanno Becker6b4ce492017-08-23 11:00:21 +01001145/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001146void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1147 int radix_Q, char *input_Q,
1148 int radix_E, char *input_E,
1149 int radix_D, char *output_D,
1150 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001151{
1152 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1153
1154 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1155 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1156 mbedtls_mpi_init( &E );
1157 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1158
1159 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1160 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1161 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1162 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1163
1164 if( corrupt )
1165 {
1166 /* Make E even */
1167 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1168 }
1169
1170 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001171 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1172 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001173
1174 if( !corrupt )
1175 {
1176 /*
1177 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1178 */
1179
1180 /* Replace P,Q by P-1, Q-1 */
1181 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1182 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1183
1184 /* Check D == Dp modulo P-1 */
1185 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1186 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1187 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1188
1189 /* Check D == Dp modulo Q-1 */
1190 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1191 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1192 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1193 }
1194
1195exit:
1196
1197 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1198 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1199 mbedtls_mpi_free( &E );
1200 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1201}
1202/* END_CASE */
1203
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001204/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001205void mbedtls_rsa_import( int radix_N, char *input_N,
1206 int radix_P, char *input_P,
1207 int radix_Q, char *input_Q,
1208 int radix_D, char *input_D,
1209 int radix_E, char *input_E,
1210 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001211 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001212 int res_check,
1213 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001214{
1215 mbedtls_mpi N, P, Q, D, E;
1216 mbedtls_rsa_context ctx;
1217
Hanno Beckere1582a82017-09-29 11:51:05 +01001218 /* Buffers used for encryption-decryption test */
1219 unsigned char *buf_orig = NULL;
1220 unsigned char *buf_enc = NULL;
1221 unsigned char *buf_dec = NULL;
1222
Hanno Beckerc77ab892017-08-23 11:01:06 +01001223 mbedtls_entropy_context entropy;
1224 mbedtls_ctr_drbg_context ctr_drbg;
1225 const char *pers = "test_suite_rsa";
1226
Hanno Becker4d6e8342017-09-29 11:50:18 +01001227 const int have_N = ( strlen( input_N ) > 0 );
1228 const int have_P = ( strlen( input_P ) > 0 );
1229 const int have_Q = ( strlen( input_Q ) > 0 );
1230 const int have_D = ( strlen( input_D ) > 0 );
1231 const int have_E = ( strlen( input_E ) > 0 );
1232
Hanno Beckerc77ab892017-08-23 11:01:06 +01001233 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001234 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001235 mbedtls_rsa_init( &ctx, 0, 0 );
1236
1237 mbedtls_mpi_init( &N );
1238 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1239 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1240
Hanno Beckerd4d60572018-01-10 07:12:01 +00001241 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1242 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1243
Hanno Becker4d6e8342017-09-29 11:50:18 +01001244 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001245 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1246
Hanno Becker4d6e8342017-09-29 11:50:18 +01001247 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001248 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1249
Hanno Becker4d6e8342017-09-29 11:50:18 +01001250 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001251 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1252
Hanno Becker4d6e8342017-09-29 11:50:18 +01001253 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001254 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1255
Hanno Becker4d6e8342017-09-29 11:50:18 +01001256 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001257 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1258
1259 if( !successive )
1260 {
1261 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001262 have_N ? &N : NULL,
1263 have_P ? &P : NULL,
1264 have_Q ? &Q : NULL,
1265 have_D ? &D : NULL,
1266 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001267 }
1268 else
1269 {
1270 /* Import N, P, Q, D, E separately.
1271 * This should make no functional difference. */
1272
1273 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001274 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001275 NULL, NULL, NULL, NULL ) == 0 );
1276
1277 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1278 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001279 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001280 NULL, NULL, NULL ) == 0 );
1281
1282 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1283 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001284 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001285 NULL, NULL ) == 0 );
1286
1287 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1288 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001289 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001290 NULL ) == 0 );
1291
1292 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1293 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001294 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001295 }
1296
Hanno Becker04877a42017-10-11 10:01:33 +01001297 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001298
Hanno Beckere1582a82017-09-29 11:51:05 +01001299 /* On expected success, perform some public and private
1300 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001301 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001302 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001303 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001304 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1305 else
1306 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1307
1308 if( res_check != 0 )
1309 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001310
1311 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1312 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1313 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1314 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1315 goto exit;
1316
1317 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1318 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1319
1320 /* Make sure the number we're generating is smaller than the modulus */
1321 buf_orig[0] = 0x00;
1322
1323 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1324
1325 if( is_priv )
1326 {
1327 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1328 &ctr_drbg, buf_enc,
1329 buf_dec ) == 0 );
1330
1331 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1332 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1333 }
1334 }
1335
Hanno Beckerc77ab892017-08-23 11:01:06 +01001336exit:
1337
Hanno Beckere1582a82017-09-29 11:51:05 +01001338 mbedtls_free( buf_orig );
1339 mbedtls_free( buf_enc );
1340 mbedtls_free( buf_dec );
1341
Hanno Beckerc77ab892017-08-23 11:01:06 +01001342 mbedtls_rsa_free( &ctx );
1343
1344 mbedtls_ctr_drbg_free( &ctr_drbg );
1345 mbedtls_entropy_free( &entropy );
1346
1347 mbedtls_mpi_free( &N );
1348 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1349 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1350}
1351/* END_CASE */
1352
Hanno Becker417f2d62017-08-23 11:44:51 +01001353/* BEGIN_CASE */
1354void mbedtls_rsa_export( int radix_N, char *input_N,
1355 int radix_P, char *input_P,
1356 int radix_Q, char *input_Q,
1357 int radix_D, char *input_D,
1358 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001359 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001360 int successive )
1361{
1362 /* Original MPI's with which we set up the RSA context */
1363 mbedtls_mpi N, P, Q, D, E;
1364
1365 /* Exported MPI's */
1366 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1367
1368 const int have_N = ( strlen( input_N ) > 0 );
1369 const int have_P = ( strlen( input_P ) > 0 );
1370 const int have_Q = ( strlen( input_Q ) > 0 );
1371 const int have_D = ( strlen( input_D ) > 0 );
1372 const int have_E = ( strlen( input_E ) > 0 );
1373
Hanno Becker417f2d62017-08-23 11:44:51 +01001374 mbedtls_rsa_context ctx;
1375
1376 mbedtls_rsa_init( &ctx, 0, 0 );
1377
1378 mbedtls_mpi_init( &N );
1379 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1380 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1381
1382 mbedtls_mpi_init( &Ne );
1383 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1384 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1385
1386 /* Setup RSA context */
1387
1388 if( have_N )
1389 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1390
1391 if( have_P )
1392 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1393
1394 if( have_Q )
1395 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1396
1397 if( have_D )
1398 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1399
1400 if( have_E )
1401 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1402
1403 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1404 strlen( input_N ) ? &N : NULL,
1405 strlen( input_P ) ? &P : NULL,
1406 strlen( input_Q ) ? &Q : NULL,
1407 strlen( input_D ) ? &D : NULL,
1408 strlen( input_E ) ? &E : NULL ) == 0 );
1409
Hanno Becker7f25f852017-10-10 16:56:22 +01001410 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001411
1412 /*
1413 * Export parameters and compare to original ones.
1414 */
1415
1416 /* N and E must always be present. */
1417 if( !successive )
1418 {
1419 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1420 }
1421 else
1422 {
1423 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1424 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1425 }
1426 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1427 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1428
1429 /* If we were providing enough information to setup a complete private context,
1430 * we expect to be able to export all core parameters. */
1431
1432 if( is_priv )
1433 {
1434 if( !successive )
1435 {
1436 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1437 &De, NULL ) == 0 );
1438 }
1439 else
1440 {
1441 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1442 NULL, NULL ) == 0 );
1443 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1444 NULL, NULL ) == 0 );
1445 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1446 &De, NULL ) == 0 );
1447 }
1448
1449 if( have_P )
1450 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1451
1452 if( have_Q )
1453 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1454
1455 if( have_D )
1456 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1457
1458 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001459 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1460 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001461 }
1462
1463exit:
1464
1465 mbedtls_rsa_free( &ctx );
1466
1467 mbedtls_mpi_free( &N );
1468 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1469 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1470
1471 mbedtls_mpi_free( &Ne );
1472 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1473 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1474}
1475/* END_CASE */
1476
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001477/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001478void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1479 int radix_P, char *input_P,
1480 int radix_Q, char *input_Q,
1481 int radix_D, char *input_D,
1482 int radix_E, char *input_E,
1483 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001484{
1485 /* Original MPI's with which we set up the RSA context */
1486 mbedtls_mpi N, P, Q, D, E;
1487
1488 const int have_N = ( strlen( input_N ) > 0 );
1489 const int have_P = ( strlen( input_P ) > 0 );
1490 const int have_Q = ( strlen( input_Q ) > 0 );
1491 const int have_D = ( strlen( input_D ) > 0 );
1492 const int have_E = ( strlen( input_E ) > 0 );
1493
1494 mbedtls_entropy_context entropy;
1495 mbedtls_ctr_drbg_context ctr_drbg;
1496 const char *pers = "test_suite_rsa";
1497
1498 mbedtls_mpi_init( &N );
1499 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1500 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1501
1502 mbedtls_ctr_drbg_init( &ctr_drbg );
1503 mbedtls_entropy_init( &entropy );
1504 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1505 &entropy, (const unsigned char *) pers,
1506 strlen( pers ) ) == 0 );
1507
1508 if( have_N )
1509 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1510
1511 if( have_P )
1512 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1513
1514 if( have_Q )
1515 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1516
1517 if( have_D )
1518 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1519
1520 if( have_E )
1521 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1522
Hanno Becker750e8b42017-08-25 07:54:27 +01001523 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1524 have_P ? &P : NULL,
1525 have_Q ? &Q : NULL,
1526 have_D ? &D : NULL,
1527 have_E ? &E : NULL,
1528 prng ? mbedtls_ctr_drbg_random : NULL,
1529 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001530exit:
1531
1532 mbedtls_ctr_drbg_free( &ctr_drbg );
1533 mbedtls_entropy_free( &entropy );
1534
1535 mbedtls_mpi_free( &N );
1536 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1537 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1538}
1539/* END_CASE */
1540
Hanno Beckerc77ab892017-08-23 11:01:06 +01001541/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001542void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1543 data_t *input_Q, data_t *input_D,
1544 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001545 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001546{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001547 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001548 unsigned char bufNe[256];
1549 unsigned char bufPe[128];
1550 unsigned char bufQe[128];
1551 unsigned char bufDe[256];
1552 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001553
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001554 mbedtls_rsa_context ctx;
1555
1556 mbedtls_rsa_init( &ctx, 0, 0 );
1557
1558 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001559 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001560 input_N->len ? input_N->x : NULL, input_N->len,
1561 input_P->len ? input_P->x : NULL, input_P->len,
1562 input_Q->len ? input_Q->x : NULL, input_Q->len,
1563 input_D->len ? input_D->x : NULL, input_D->len,
1564 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001565
Hanno Becker7f25f852017-10-10 16:56:22 +01001566 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001567
1568 /*
1569 * Export parameters and compare to original ones.
1570 */
1571
1572 /* N and E must always be present. */
1573 if( !successive )
1574 {
Azim Khand30ca132017-06-09 04:32:58 +01001575 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001576 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001577 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001578 }
1579 else
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,
1583 NULL, 0 ) == 0 );
1584 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1585 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001586 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001587 }
Azim Khand30ca132017-06-09 04:32:58 +01001588 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1589 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001590
1591 /* If we were providing enough information to setup a complete private context,
1592 * we expect to be able to export all core parameters. */
1593
1594 if( is_priv )
1595 {
1596 if( !successive )
1597 {
1598 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001599 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1600 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1601 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001602 NULL, 0 ) == 0 );
1603 }
1604 else
1605 {
1606 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001607 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001608 NULL, 0, NULL, 0,
1609 NULL, 0 ) == 0 );
1610
1611 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001612 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001613 NULL, 0, NULL, 0 ) == 0 );
1614
Azim Khand30ca132017-06-09 04:32:58 +01001615 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1616 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001617 NULL, 0 ) == 0 );
1618 }
1619
Azim Khand30ca132017-06-09 04:32:58 +01001620 if( input_P->len )
1621 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001622
Azim Khand30ca132017-06-09 04:32:58 +01001623 if( input_Q->len )
1624 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001625
Azim Khand30ca132017-06-09 04:32:58 +01001626 if( input_D->len )
1627 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001628
1629 }
1630
1631exit:
1632 mbedtls_rsa_free( &ctx );
1633}
1634/* END_CASE */
1635
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001636/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001637void mbedtls_rsa_import_raw( data_t *input_N,
1638 data_t *input_P, data_t *input_Q,
1639 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001640 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001641 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001642 int res_check,
1643 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001644{
Hanno Beckere1582a82017-09-29 11:51:05 +01001645 /* Buffers used for encryption-decryption test */
1646 unsigned char *buf_orig = NULL;
1647 unsigned char *buf_enc = NULL;
1648 unsigned char *buf_dec = NULL;
1649
Hanno Beckerc77ab892017-08-23 11:01:06 +01001650 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001651 mbedtls_entropy_context entropy;
1652 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001653
Hanno Beckerc77ab892017-08-23 11:01:06 +01001654 const char *pers = "test_suite_rsa";
1655
1656 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001657 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001658 mbedtls_rsa_init( &ctx, 0, 0 );
1659
Hanno Beckerc77ab892017-08-23 11:01:06 +01001660 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1661 &entropy, (const unsigned char *) pers,
1662 strlen( pers ) ) == 0 );
1663
Hanno Beckerc77ab892017-08-23 11:01:06 +01001664 if( !successive )
1665 {
1666 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001667 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1668 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1669 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1670 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1671 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001672 }
1673 else
1674 {
1675 /* Import N, P, Q, D, E separately.
1676 * This should make no functional difference. */
1677
1678 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001679 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001680 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1681
1682 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1683 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001684 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001685 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1686
1687 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1688 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001689 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001690 NULL, 0, NULL, 0 ) == 0 );
1691
1692 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1693 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001694 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001695 NULL, 0 ) == 0 );
1696
1697 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1698 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001699 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001700 }
1701
Hanno Becker04877a42017-10-11 10:01:33 +01001702 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001703
Hanno Beckere1582a82017-09-29 11:51:05 +01001704 /* On expected success, perform some public and private
1705 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001706 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001707 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001708 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001709 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1710 else
1711 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1712
1713 if( res_check != 0 )
1714 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001715
1716 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1717 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1718 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1719 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1720 goto exit;
1721
1722 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1723 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1724
1725 /* Make sure the number we're generating is smaller than the modulus */
1726 buf_orig[0] = 0x00;
1727
1728 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1729
1730 if( is_priv )
1731 {
1732 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1733 &ctr_drbg, buf_enc,
1734 buf_dec ) == 0 );
1735
1736 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1737 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1738 }
1739 }
1740
Hanno Beckerc77ab892017-08-23 11:01:06 +01001741exit:
1742
Hanno Becker3f3ae852017-10-02 10:08:39 +01001743 mbedtls_free( buf_orig );
1744 mbedtls_free( buf_enc );
1745 mbedtls_free( buf_dec );
1746
Hanno Beckerc77ab892017-08-23 11:01:06 +01001747 mbedtls_rsa_free( &ctx );
1748
1749 mbedtls_ctr_drbg_free( &ctr_drbg );
1750 mbedtls_entropy_free( &entropy );
1751
1752}
1753/* END_CASE */
1754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001756void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001757{
Andres AG93012e82016-09-09 09:10:28 +01001758 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001759}
Paul Bakker33b43f12013-08-20 11:48:36 +02001760/* END_CASE */