blob: f8bf859bf60171ad24845e610c18274197be0071 [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,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132 buf, sizeof( buf ),
133 sizeof( buf ), buf,
134 buf ) );
135 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
136 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 NULL, sizeof( buf ),
138 sizeof( buf ), buf,
139 buf ) );
140 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
141 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 buf, sizeof( buf ),
143 sizeof( buf ), NULL,
144 buf ) );
145 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
146 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500147 buf, sizeof( buf ),
148 sizeof( buf ), buf,
149 NULL ) );
150
151 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
152 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100153 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500154 buf, buf, 42 ) );
155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
156 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100157 NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500158 buf, buf, 42 ) );
159 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
160 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100161 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500162 NULL, buf, 42 ) );
163 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
164 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100165 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500166 buf, NULL, 42 ) );
167
168 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
169 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100170 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500171 buf, buf, 42 ) );
172 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
173 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100174 NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500175 buf, buf, 42 ) );
176 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
177 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100178 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500179 NULL, buf, 42 ) );
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
181 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100182 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 buf, NULL, 42 ) );
184
185 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
186 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187 buf, sizeof( buf ),
188 &olen,
189 buf, buf, 42 ) );
190 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
191 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500192 NULL, sizeof( buf ),
193 NULL,
194 buf, buf, 42 ) );
195 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
196 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 buf, sizeof( buf ),
198 &olen,
199 NULL, 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 buf, sizeof( buf ),
203 &olen,
204 buf, NULL, 42 ) );
205
206 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
207 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
Thomas Daubney11425342021-05-18 11:57:05 +0100208 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500209 0, sizeof( buf ), buf,
210 buf ) );
211 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
212 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Thomas Daubney11425342021-05-18 11:57:05 +0100213 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214 0, sizeof( buf ), NULL,
215 buf ) );
216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
217 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Thomas Daubney11425342021-05-18 11:57:05 +0100218 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500219 0, sizeof( buf ), buf,
220 NULL ) );
221 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
222 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Thomas Daubney11425342021-05-18 11:57:05 +0100223 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500224 MBEDTLS_MD_SHA1,
225 0, NULL,
226 buf ) );
227
228 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
229 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
230 valid_mode,
231 0, sizeof( buf ), buf,
232 buf ) );
233 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
234 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
235 invalid_mode,
236 0, sizeof( buf ), buf,
237 buf ) );
238 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
239 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
240 valid_mode,
241 0, sizeof( buf ), NULL,
242 buf ) );
243 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
244 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
245 valid_mode,
246 0, sizeof( buf ), buf,
247 NULL ) );
248 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
249 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
250 valid_mode,
251 MBEDTLS_MD_SHA1,
252 0, NULL,
253 buf ) );
254
255 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
256 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
257 valid_mode,
258 0, sizeof( buf ), buf,
259 buf ) );
260 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
261 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
262 invalid_mode,
263 0, sizeof( buf ), buf,
264 buf ) );
265 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
266 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
267 valid_mode,
268 0, sizeof( buf ), NULL,
269 buf ) );
270 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
271 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
272 valid_mode,
273 0, sizeof( buf ), buf,
274 NULL ) );
275 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
276 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
277 valid_mode,
278 MBEDTLS_MD_SHA1,
279 0, NULL,
280 buf ) );
281
282 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Cédric Meutera05cbec2020-04-25 15:02:34 +0200283 mbedtls_rsa_rsassa_pss_sign_ext( NULL, NULL, NULL,
284 0, sizeof( buf ), buf,
285 MBEDTLS_RSA_SALT_LEN_ANY,
286 buf ) );
287 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
288 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
289 0, sizeof( buf ), NULL,
290 MBEDTLS_RSA_SALT_LEN_ANY,
291 buf ) );
292 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
293 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
294 0, sizeof( buf ), buf,
295 MBEDTLS_RSA_SALT_LEN_ANY,
296 NULL ) );
297 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
298 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
299 MBEDTLS_MD_SHA1,
300 0, NULL,
301 MBEDTLS_RSA_SALT_LEN_ANY,
302 buf ) );
303
304 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500305 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
306 valid_mode,
307 0, sizeof( buf ), buf,
308 buf ) );
309 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
310 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
311 invalid_mode,
312 0, sizeof( buf ), buf,
313 buf ) );
314 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
315 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
316 valid_mode,
317 0, sizeof( buf ), NULL,
318 buf ) );
319 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
320 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
321 valid_mode,
322 0, sizeof( buf ), buf,
323 NULL ) );
324 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
325 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
326 valid_mode,
327 MBEDTLS_MD_SHA1, 0, NULL,
328 buf ) );
329
330 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
331 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
332 NULL,
333 valid_mode,
334 0, sizeof( buf ), buf,
335 buf ) );
336 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
337 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
338 NULL,
339 invalid_mode,
340 0, sizeof( buf ), buf,
341 buf ) );
342 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
343 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
344 NULL,
345 valid_mode,
346 0, sizeof( buf ),
347 NULL, buf ) );
348 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
349 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
350 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_rsassa_pkcs1_v15_verify( &ctx, NULL,
356 NULL,
357 valid_mode,
358 MBEDTLS_MD_SHA1,
359 0, NULL,
360 buf ) );
361
362 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
363 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
364 valid_mode,
365 0, sizeof( buf ),
366 buf, buf ) );
367 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
368 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
369 invalid_mode,
370 0, sizeof( buf ),
371 buf, buf ) );
372 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
373 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
374 valid_mode,
375 0, sizeof( buf ),
376 NULL, buf ) );
377 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
378 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
379 valid_mode,
380 0, sizeof( buf ),
381 buf, NULL ) );
382 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
383 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
384 valid_mode,
385 MBEDTLS_MD_SHA1,
386 0, NULL,
387 buf ) );
388
389 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
390 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
391 valid_mode,
392 0, sizeof( buf ),
393 buf,
394 0, 0,
395 buf ) );
396 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
397 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
398 invalid_mode,
399 0, sizeof( buf ),
400 buf,
401 0, 0,
402 buf ) );
403 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
404 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
405 valid_mode,
406 0, sizeof( buf ),
407 NULL, 0, 0,
408 buf ) );
409 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
410 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
411 valid_mode,
412 0, sizeof( buf ),
413 buf, 0, 0,
414 NULL ) );
415 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
416 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
417 valid_mode,
418 MBEDTLS_MD_SHA1,
419 0, NULL,
420 0, 0,
421 buf ) );
422
423 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
424 mbedtls_rsa_copy( NULL, &ctx ) );
425 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
426 mbedtls_rsa_copy( &ctx, NULL ) );
427
428exit:
429 return;
430}
431/* END_CASE */
432
Paul Bakker33b43f12013-08-20 11:48:36 +0200433/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100434void rsa_init_free( int reinit )
435{
436 mbedtls_rsa_context ctx;
437
438 /* Double free is not explicitly documented to work, but we rely on it
439 * even inside the library so that you can call mbedtls_rsa_free()
440 * unconditionally on an error path without checking whether it has
441 * already been called in the success path. */
442
443 mbedtls_rsa_init( &ctx, 0, 0 );
444 mbedtls_rsa_free( &ctx );
445
446 if( reinit )
447 mbedtls_rsa_init( &ctx, 0, 0 );
448 mbedtls_rsa_free( &ctx );
449
450 /* This test case always succeeds, functionally speaking. A plausible
451 * bug might trigger an invalid pointer dereference or a memory leak. */
452 goto exit;
453}
454/* END_CASE */
455
456/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100457void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100458 int digest, int mod, int radix_P, char * input_P,
459 int radix_Q, char * input_Q, int radix_N,
460 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200461 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000462{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200463 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
464 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100466 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200467 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000468
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100469 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
470 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000472
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200473 memset( hash_result, 0x00, sizeof( hash_result ) );
474 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200475 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000476
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100477 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
478 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
479 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
480 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000481
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100482 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
483 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100484 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000486
Paul Bakker42a29bf2009-07-07 20:18:41 +0000487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100489 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 +0000490
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200491 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
492 &rnd_info, MBEDTLS_RSA_PRIVATE, digest,
493 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200494 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000495 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000496
Ronald Cronac6ae352020-06-26 14:33:03 +0200497 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
498 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000499 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000500
Paul Bakkerbd51b262014-07-10 15:26:12 +0200501exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100502 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
503 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000505}
Paul Bakker33b43f12013-08-20 11:48:36 +0200506/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000507
Paul Bakker33b43f12013-08-20 11:48:36 +0200508/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100509void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100510 int digest, int mod, int radix_N,
511 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100512 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000513{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200514 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000516
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100517 mbedtls_mpi N, E;
518
519 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200521 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000522
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100523 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
524 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
525 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
526 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000528
Paul Bakker42a29bf2009-07-07 20:18:41 +0000529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100531 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 +0000532
Azim Khand30ca132017-06-09 04:32:58 +0100533 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 +0100534
Paul Bakkerbd51b262014-07-10 15:26:12 +0200535exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100536 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000538}
Paul Bakker33b43f12013-08-20 11:48:36 +0200539/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000540
Paul Bakker821fb082009-07-12 13:26:42 +0000541
Paul Bakker33b43f12013-08-20 11:48:36 +0200542/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100543void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100544 int padding_mode, int mod, int radix_P,
545 char * input_P, int radix_Q, char * input_Q,
546 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200547 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000548{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200549 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100551 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200552 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100555 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
556 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000557
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200558 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200559 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000560
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100561 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
562 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
563 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
564 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000565
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100566 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
567 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100568 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000570
Paul Bakker821fb082009-07-12 13:26:42 +0000571
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200572 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
573 &rnd_info, MBEDTLS_RSA_PRIVATE,
574 MBEDTLS_MD_NONE, hash_result->len,
575 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000576
Paul Bakker821fb082009-07-12 13:26:42 +0000577
Ronald Cronac6ae352020-06-26 14:33:03 +0200578 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
579 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000580
Paul Bakkerbd51b262014-07-10 15:26:12 +0200581exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100582 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
583 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000586}
Paul Bakker33b43f12013-08-20 11:48:36 +0200587/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000588
Paul Bakker33b43f12013-08-20 11:48:36 +0200589/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100590void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200591 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100592 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100593 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000594{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200595 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000597
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100598 mbedtls_mpi N, E;
599 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100602 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000603
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100604 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
605 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000606
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100607 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
608 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000610
Paul Bakker821fb082009-07-12 13:26:42 +0000611
Azim Khand30ca132017-06-09 04:32:58 +0100612 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 +0100613
Paul Bakkerbd51b262014-07-10 15:26:12 +0200614exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100615 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000617}
Paul Bakker33b43f12013-08-20 11:48:36 +0200618/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000619
Paul Bakker33b43f12013-08-20 11:48:36 +0200620/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100621void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100622 int mod, int radix_N, char * input_N,
623 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200624 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000625{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200626 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200628 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000629
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100630 mbedtls_mpi N, E;
631 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
632
Ronald Cron351f0ee2020-06-10 12:12:18 +0200633 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200636 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000637
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100638 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
639 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000640
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100641 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
642 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000644
Paul Bakker42a29bf2009-07-07 20:18:41 +0000645
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200646 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
647 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100648 &rnd_info, message_str->len,
649 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200650 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200651 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000652 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000653
Ronald Cronac6ae352020-06-26 14:33:03 +0200654 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
655 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000656 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100657
Paul Bakkerbd51b262014-07-10 15:26:12 +0200658exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100659 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000661}
Paul Bakker33b43f12013-08-20 11:48:36 +0200662/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000663
Paul Bakker33b43f12013-08-20 11:48:36 +0200664/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100665void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100666 int mod, int radix_N, char * input_N,
667 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200668 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000669{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200670 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000672
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100673 mbedtls_mpi N, E;
674
675 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200677 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000678
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100679 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
680 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000681
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100682 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
683 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000685
Paul Bakkera6656852010-07-18 19:47:14 +0000686
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200687 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100688 NULL, message_str->len,
689 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200690 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200691 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000692 {
Paul Bakkera6656852010-07-18 19:47:14 +0000693
Ronald Cronac6ae352020-06-26 14:33:03 +0200694 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
695 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000696 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100697
Paul Bakkerbd51b262014-07-10 15:26:12 +0200698exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100699 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000701}
Paul Bakker33b43f12013-08-20 11:48:36 +0200702/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000703
Paul Bakker33b43f12013-08-20 11:48:36 +0200704/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100705void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100706 int mod, int radix_P, char * input_P,
707 int radix_Q, char * input_Q, int radix_N,
708 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200709 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100710 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000711{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200712 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000714 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200715 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100716 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000717
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100718 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
719 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000722
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200723 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200724 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000725
Paul Bakker42a29bf2009-07-07 20:18:41 +0000726
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100727 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
728 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
729 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
730 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000731
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100732 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
733 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100734 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000736
Paul Bakker69998dd2009-07-11 19:15:20 +0000737 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000738
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200739 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100740 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200741 &output_len, message_str->x, output,
742 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200743 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000744 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000745
Ronald Cronac6ae352020-06-26 14:33:03 +0200746 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200747 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200748 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000749 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000750
Paul Bakkerbd51b262014-07-10 15:26:12 +0200751exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100752 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
753 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000755}
Paul Bakker33b43f12013-08-20 11:48:36 +0200756/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000757
Paul Bakker33b43f12013-08-20 11:48:36 +0200758/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100759void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100760 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200761 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000762{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200763 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000765
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100766 mbedtls_mpi N, E;
767
768 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
770 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200771 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000772
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100773 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
774 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000775
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100776 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
777 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000779
Paul Bakker821fb082009-07-12 13:26:42 +0000780
Azim Khand30ca132017-06-09 04:32:58 +0100781 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200782 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000783 {
Paul Bakker821fb082009-07-12 13:26:42 +0000784
Ronald Cronac6ae352020-06-26 14:33:03 +0200785 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
786 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000787 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100788
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100789 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200791 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100795
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200796 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100797 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100798 if( result == 0 )
799 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100800
Ronald Cronac6ae352020-06-26 14:33:03 +0200801 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
802 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100803 }
804
Paul Bakkerbd51b262014-07-10 15:26:12 +0200805exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100806 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 mbedtls_rsa_free( &ctx );
808 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000809}
Paul Bakker33b43f12013-08-20 11:48:36 +0200810/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000811
Paul Bakker33b43f12013-08-20 11:48:36 +0200812/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100813void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100814 char * input_P, int radix_Q, char * input_Q,
815 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200816 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100817 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000818{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200819 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100821 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200822 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200823 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000824
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100825 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
826 mbedtls_mpi_init( &Q ); 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 );
Paul Bakker821fb082009-07-12 13:26:42 +0000829
Ronald Cron351f0ee2020-06-10 12:12:18 +0200830 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000831
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100832 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
833 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
834 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
835 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000836
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100837 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
838 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100839 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000841
Paul Bakker821fb082009-07-12 13:26:42 +0000842
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200843 /* repeat three times to test updating of blinding values */
844 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000845 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200846 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200847 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
848 &rnd_info, message_str->x,
849 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200850 if( result == 0 )
851 {
Paul Bakker821fb082009-07-12 13:26:42 +0000852
Ronald Cronac6ae352020-06-26 14:33:03 +0200853 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200854 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200855 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200856 }
Paul Bakker821fb082009-07-12 13:26:42 +0000857 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000858
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100859 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200861 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100865
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200866 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200867 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
868 &rnd_info, message_str->x,
869 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100870 if( result == 0 )
871 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100872
Ronald Cronac6ae352020-06-26 14:33:03 +0200873 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200874 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200875 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100876 }
877
Paul Bakkerbd51b262014-07-10 15:26:12 +0200878exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100879 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
880 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000883}
Paul Bakker33b43f12013-08-20 11:48:36 +0200884/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000885
Paul Bakker33b43f12013-08-20 11:48:36 +0200886/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100887void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_rsa_context ctx;
890 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000893}
Paul Bakker33b43f12013-08-20 11:48:36 +0200894/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000895
Paul Bakker33b43f12013-08-20 11:48:36 +0200896/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100897void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
898 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100901 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000902
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100903 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000905
Paul Bakker33b43f12013-08-20 11:48:36 +0200906 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000907 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100908 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000909 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200910 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000911 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100912 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000913 }
914
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100915 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100917
Paul Bakkerbd51b262014-07-10 15:26:12 +0200918exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100919 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000921}
Paul Bakker33b43f12013-08-20 11:48:36 +0200922/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000923
Paul Bakker33b43f12013-08-20 11:48:36 +0200924/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100925void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
926 int radix_Q, char * input_Q, int radix_N,
927 char * input_N, int radix_E, char * input_E,
928 int radix_D, char * input_D, int radix_DP,
929 char * input_DP, int radix_DQ,
930 char * input_DQ, int radix_QP,
931 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000936
Paul Bakker33b43f12013-08-20 11:48:36 +0200937 ctx.len = mod / 8;
938 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000941 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200942 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000945 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200946 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000949 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200950 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000953 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200954 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000957 }
Hanno Becker131134f2017-08-23 08:31:07 +0100958#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200959 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000962 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200963 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000966 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200967 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000970 }
Hanno Becker131134f2017-08-23 08:31:07 +0100971#else
972 ((void) radix_DP); ((void) input_DP);
973 ((void) radix_DQ); ((void) input_DQ);
974 ((void) radix_QP); ((void) input_QP);
975#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100978
Paul Bakkerbd51b262014-07-10 15:26:12 +0200979exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000981}
Paul Bakker33b43f12013-08-20 11:48:36 +0200982/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000983
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100984/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100985void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
986 int radix_Epub, char * input_Epub, int radix_P,
987 char * input_P, int radix_Q, char * input_Q,
988 int radix_N, char * input_N, int radix_E,
989 char * input_E, int radix_D, char * input_D,
990 int radix_DP, char * input_DP, int radix_DQ,
991 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100992 int result )
993{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
997 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100998
999 pub.len = mod / 8;
1000 prv.len = mod / 8;
1001
1002 if( strlen( input_Npub ) )
1003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001005 }
1006 if( strlen( input_Epub ) )
1007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001009 }
1010
1011 if( strlen( input_P ) )
1012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001014 }
1015 if( strlen( input_Q ) )
1016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001018 }
1019 if( strlen( input_N ) )
1020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001022 }
1023 if( strlen( input_E ) )
1024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001026 }
1027 if( strlen( input_D ) )
1028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001030 }
Hanno Becker131134f2017-08-23 08:31:07 +01001031#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001032 if( strlen( input_DP ) )
1033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001035 }
1036 if( strlen( input_DQ ) )
1037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001039 }
1040 if( strlen( input_QP ) )
1041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001043 }
Hanno Becker131134f2017-08-23 08:31:07 +01001044#else
1045 ((void) radix_DP); ((void) input_DP);
1046 ((void) radix_DQ); ((void) input_DQ);
1047 ((void) radix_QP); ((void) input_QP);
1048#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001051
1052exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_rsa_free( &pub );
1054 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001055}
1056/* END_CASE */
1057
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001058/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 mbedtls_rsa_context ctx;
1062 mbedtls_entropy_context entropy;
1063 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001064 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001065
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001066 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001068 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001069
Hanno Beckera47023e2017-12-22 17:08:03 +00001070 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1071 &entropy, (const unsigned char *) pers,
1072 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001075 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001078 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001079 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001080
Paul Bakkerbd51b262014-07-10 15:26:12 +02001081exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_rsa_free( &ctx );
1083 mbedtls_ctr_drbg_free( &ctr_drbg );
1084 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001085}
Paul Bakker33b43f12013-08-20 11:48:36 +02001086/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001087
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001088/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001089void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001090 int radix_D, char *input_D,
1091 int radix_E, char *input_E,
1092 int radix_P, char *output_P,
1093 int radix_Q, char *output_Q,
1094 int corrupt, int result )
1095{
1096 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1097
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001098 mbedtls_mpi_init( &N );
1099 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1100 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1101 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1102
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001103 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1104 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1105 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1106 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1107 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1108
1109 if( corrupt )
1110 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1111
1112 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001113 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001114
1115 if( !corrupt )
1116 {
1117 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1118 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1119 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1120 }
1121
1122exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001123 mbedtls_mpi_free( &N );
1124 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1125 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1126 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001127}
1128/* END_CASE */
1129
Hanno Becker6b4ce492017-08-23 11:00:21 +01001130/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001131void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1132 int radix_Q, char *input_Q,
1133 int radix_E, char *input_E,
1134 int radix_D, char *output_D,
1135 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001136{
1137 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1138
1139 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1140 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1141 mbedtls_mpi_init( &E );
1142 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1143
1144 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1145 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1146 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1147 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1148
1149 if( corrupt )
1150 {
1151 /* Make E even */
1152 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1153 }
1154
1155 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001156 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1157 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001158
1159 if( !corrupt )
1160 {
1161 /*
1162 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1163 */
1164
1165 /* Replace P,Q by P-1, Q-1 */
1166 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1167 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1168
1169 /* Check D == Dp modulo P-1 */
1170 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1171 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1172 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1173
1174 /* Check D == Dp modulo Q-1 */
1175 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1176 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1177 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1178 }
1179
1180exit:
1181
1182 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1183 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1184 mbedtls_mpi_free( &E );
1185 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1186}
1187/* END_CASE */
1188
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001189/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001190void mbedtls_rsa_import( int radix_N, char *input_N,
1191 int radix_P, char *input_P,
1192 int radix_Q, char *input_Q,
1193 int radix_D, char *input_D,
1194 int radix_E, char *input_E,
1195 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001196 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001197 int res_check,
1198 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001199{
1200 mbedtls_mpi N, P, Q, D, E;
1201 mbedtls_rsa_context ctx;
1202
Hanno Beckere1582a82017-09-29 11:51:05 +01001203 /* Buffers used for encryption-decryption test */
1204 unsigned char *buf_orig = NULL;
1205 unsigned char *buf_enc = NULL;
1206 unsigned char *buf_dec = NULL;
1207
Hanno Beckerc77ab892017-08-23 11:01:06 +01001208 mbedtls_entropy_context entropy;
1209 mbedtls_ctr_drbg_context ctr_drbg;
1210 const char *pers = "test_suite_rsa";
1211
Hanno Becker4d6e8342017-09-29 11:50:18 +01001212 const int have_N = ( strlen( input_N ) > 0 );
1213 const int have_P = ( strlen( input_P ) > 0 );
1214 const int have_Q = ( strlen( input_Q ) > 0 );
1215 const int have_D = ( strlen( input_D ) > 0 );
1216 const int have_E = ( strlen( input_E ) > 0 );
1217
Hanno Beckerc77ab892017-08-23 11:01:06 +01001218 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001219 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001220 mbedtls_rsa_init( &ctx, 0, 0 );
1221
1222 mbedtls_mpi_init( &N );
1223 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1224 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1225
Hanno Beckerd4d60572018-01-10 07:12:01 +00001226 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1227 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1228
Hanno Becker4d6e8342017-09-29 11:50:18 +01001229 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001230 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1231
Hanno Becker4d6e8342017-09-29 11:50:18 +01001232 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001233 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1234
Hanno Becker4d6e8342017-09-29 11:50:18 +01001235 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001236 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1237
Hanno Becker4d6e8342017-09-29 11:50:18 +01001238 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001239 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1240
Hanno Becker4d6e8342017-09-29 11:50:18 +01001241 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001242 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1243
1244 if( !successive )
1245 {
1246 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001247 have_N ? &N : NULL,
1248 have_P ? &P : NULL,
1249 have_Q ? &Q : NULL,
1250 have_D ? &D : NULL,
1251 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001252 }
1253 else
1254 {
1255 /* Import N, P, Q, D, E separately.
1256 * This should make no functional difference. */
1257
1258 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001259 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001260 NULL, NULL, NULL, NULL ) == 0 );
1261
1262 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1263 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001264 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001265 NULL, NULL, NULL ) == 0 );
1266
1267 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1268 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001269 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001270 NULL, NULL ) == 0 );
1271
1272 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1273 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001274 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001275 NULL ) == 0 );
1276
1277 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1278 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001279 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001280 }
1281
Hanno Becker04877a42017-10-11 10:01:33 +01001282 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001283
Hanno Beckere1582a82017-09-29 11:51:05 +01001284 /* On expected success, perform some public and private
1285 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001286 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001287 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001288 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001289 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1290 else
1291 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1292
1293 if( res_check != 0 )
1294 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001295
1296 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1297 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1298 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1299 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1300 goto exit;
1301
1302 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1303 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1304
1305 /* Make sure the number we're generating is smaller than the modulus */
1306 buf_orig[0] = 0x00;
1307
1308 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1309
1310 if( is_priv )
1311 {
1312 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1313 &ctr_drbg, buf_enc,
1314 buf_dec ) == 0 );
1315
1316 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1317 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1318 }
1319 }
1320
Hanno Beckerc77ab892017-08-23 11:01:06 +01001321exit:
1322
Hanno Beckere1582a82017-09-29 11:51:05 +01001323 mbedtls_free( buf_orig );
1324 mbedtls_free( buf_enc );
1325 mbedtls_free( buf_dec );
1326
Hanno Beckerc77ab892017-08-23 11:01:06 +01001327 mbedtls_rsa_free( &ctx );
1328
1329 mbedtls_ctr_drbg_free( &ctr_drbg );
1330 mbedtls_entropy_free( &entropy );
1331
1332 mbedtls_mpi_free( &N );
1333 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1334 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1335}
1336/* END_CASE */
1337
Hanno Becker417f2d62017-08-23 11:44:51 +01001338/* BEGIN_CASE */
1339void mbedtls_rsa_export( int radix_N, char *input_N,
1340 int radix_P, char *input_P,
1341 int radix_Q, char *input_Q,
1342 int radix_D, char *input_D,
1343 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001344 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001345 int successive )
1346{
1347 /* Original MPI's with which we set up the RSA context */
1348 mbedtls_mpi N, P, Q, D, E;
1349
1350 /* Exported MPI's */
1351 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1352
1353 const int have_N = ( strlen( input_N ) > 0 );
1354 const int have_P = ( strlen( input_P ) > 0 );
1355 const int have_Q = ( strlen( input_Q ) > 0 );
1356 const int have_D = ( strlen( input_D ) > 0 );
1357 const int have_E = ( strlen( input_E ) > 0 );
1358
Hanno Becker417f2d62017-08-23 11:44:51 +01001359 mbedtls_rsa_context ctx;
1360
1361 mbedtls_rsa_init( &ctx, 0, 0 );
1362
1363 mbedtls_mpi_init( &N );
1364 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1365 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1366
1367 mbedtls_mpi_init( &Ne );
1368 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1369 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1370
1371 /* Setup RSA context */
1372
1373 if( have_N )
1374 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1375
1376 if( have_P )
1377 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1378
1379 if( have_Q )
1380 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1381
1382 if( have_D )
1383 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1384
1385 if( have_E )
1386 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1387
1388 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1389 strlen( input_N ) ? &N : NULL,
1390 strlen( input_P ) ? &P : NULL,
1391 strlen( input_Q ) ? &Q : NULL,
1392 strlen( input_D ) ? &D : NULL,
1393 strlen( input_E ) ? &E : NULL ) == 0 );
1394
Hanno Becker7f25f852017-10-10 16:56:22 +01001395 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001396
1397 /*
1398 * Export parameters and compare to original ones.
1399 */
1400
1401 /* N and E must always be present. */
1402 if( !successive )
1403 {
1404 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1405 }
1406 else
1407 {
1408 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1409 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1410 }
1411 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1412 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1413
1414 /* If we were providing enough information to setup a complete private context,
1415 * we expect to be able to export all core parameters. */
1416
1417 if( is_priv )
1418 {
1419 if( !successive )
1420 {
1421 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1422 &De, NULL ) == 0 );
1423 }
1424 else
1425 {
1426 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1427 NULL, NULL ) == 0 );
1428 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1429 NULL, NULL ) == 0 );
1430 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1431 &De, NULL ) == 0 );
1432 }
1433
1434 if( have_P )
1435 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1436
1437 if( have_Q )
1438 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1439
1440 if( have_D )
1441 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1442
1443 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001444 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1445 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001446 }
1447
1448exit:
1449
1450 mbedtls_rsa_free( &ctx );
1451
1452 mbedtls_mpi_free( &N );
1453 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1454 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1455
1456 mbedtls_mpi_free( &Ne );
1457 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1458 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1459}
1460/* END_CASE */
1461
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001462/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001463void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1464 int radix_P, char *input_P,
1465 int radix_Q, char *input_Q,
1466 int radix_D, char *input_D,
1467 int radix_E, char *input_E,
1468 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001469{
1470 /* Original MPI's with which we set up the RSA context */
1471 mbedtls_mpi N, P, Q, D, E;
1472
1473 const int have_N = ( strlen( input_N ) > 0 );
1474 const int have_P = ( strlen( input_P ) > 0 );
1475 const int have_Q = ( strlen( input_Q ) > 0 );
1476 const int have_D = ( strlen( input_D ) > 0 );
1477 const int have_E = ( strlen( input_E ) > 0 );
1478
1479 mbedtls_entropy_context entropy;
1480 mbedtls_ctr_drbg_context ctr_drbg;
1481 const char *pers = "test_suite_rsa";
1482
1483 mbedtls_mpi_init( &N );
1484 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1485 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1486
1487 mbedtls_ctr_drbg_init( &ctr_drbg );
1488 mbedtls_entropy_init( &entropy );
1489 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1490 &entropy, (const unsigned char *) pers,
1491 strlen( pers ) ) == 0 );
1492
1493 if( have_N )
1494 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1495
1496 if( have_P )
1497 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1498
1499 if( have_Q )
1500 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1501
1502 if( have_D )
1503 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1504
1505 if( have_E )
1506 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1507
Hanno Becker750e8b42017-08-25 07:54:27 +01001508 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1509 have_P ? &P : NULL,
1510 have_Q ? &Q : NULL,
1511 have_D ? &D : NULL,
1512 have_E ? &E : NULL,
1513 prng ? mbedtls_ctr_drbg_random : NULL,
1514 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001515exit:
1516
1517 mbedtls_ctr_drbg_free( &ctr_drbg );
1518 mbedtls_entropy_free( &entropy );
1519
1520 mbedtls_mpi_free( &N );
1521 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1522 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1523}
1524/* END_CASE */
1525
Hanno Beckerc77ab892017-08-23 11:01:06 +01001526/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001527void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1528 data_t *input_Q, data_t *input_D,
1529 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001530 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001531{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001532 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001533 unsigned char bufNe[256];
1534 unsigned char bufPe[128];
1535 unsigned char bufQe[128];
1536 unsigned char bufDe[256];
1537 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001538
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001539 mbedtls_rsa_context ctx;
1540
1541 mbedtls_rsa_init( &ctx, 0, 0 );
1542
1543 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001544 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001545 input_N->len ? input_N->x : NULL, input_N->len,
1546 input_P->len ? input_P->x : NULL, input_P->len,
1547 input_Q->len ? input_Q->x : NULL, input_Q->len,
1548 input_D->len ? input_D->x : NULL, input_D->len,
1549 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001550
Hanno Becker7f25f852017-10-10 16:56:22 +01001551 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001552
1553 /*
1554 * Export parameters and compare to original ones.
1555 */
1556
1557 /* N and E must always be present. */
1558 if( !successive )
1559 {
Azim Khand30ca132017-06-09 04:32:58 +01001560 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001561 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001562 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001563 }
1564 else
1565 {
Azim Khand30ca132017-06-09 04:32:58 +01001566 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001567 NULL, 0, NULL, 0, NULL, 0,
1568 NULL, 0 ) == 0 );
1569 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1570 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001571 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001572 }
Azim Khand30ca132017-06-09 04:32:58 +01001573 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1574 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001575
1576 /* If we were providing enough information to setup a complete private context,
1577 * we expect to be able to export all core parameters. */
1578
1579 if( is_priv )
1580 {
1581 if( !successive )
1582 {
1583 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001584 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1585 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1586 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001587 NULL, 0 ) == 0 );
1588 }
1589 else
1590 {
1591 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001592 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001593 NULL, 0, NULL, 0,
1594 NULL, 0 ) == 0 );
1595
1596 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001597 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001598 NULL, 0, NULL, 0 ) == 0 );
1599
Azim Khand30ca132017-06-09 04:32:58 +01001600 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1601 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001602 NULL, 0 ) == 0 );
1603 }
1604
Azim Khand30ca132017-06-09 04:32:58 +01001605 if( input_P->len )
1606 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001607
Azim Khand30ca132017-06-09 04:32:58 +01001608 if( input_Q->len )
1609 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001610
Azim Khand30ca132017-06-09 04:32:58 +01001611 if( input_D->len )
1612 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001613
1614 }
1615
1616exit:
1617 mbedtls_rsa_free( &ctx );
1618}
1619/* END_CASE */
1620
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001621/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001622void mbedtls_rsa_import_raw( data_t *input_N,
1623 data_t *input_P, data_t *input_Q,
1624 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001625 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001626 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001627 int res_check,
1628 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001629{
Hanno Beckere1582a82017-09-29 11:51:05 +01001630 /* Buffers used for encryption-decryption test */
1631 unsigned char *buf_orig = NULL;
1632 unsigned char *buf_enc = NULL;
1633 unsigned char *buf_dec = NULL;
1634
Hanno Beckerc77ab892017-08-23 11:01:06 +01001635 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001636 mbedtls_entropy_context entropy;
1637 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001638
Hanno Beckerc77ab892017-08-23 11:01:06 +01001639 const char *pers = "test_suite_rsa";
1640
1641 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001642 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001643 mbedtls_rsa_init( &ctx, 0, 0 );
1644
Hanno Beckerc77ab892017-08-23 11:01:06 +01001645 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1646 &entropy, (const unsigned char *) pers,
1647 strlen( pers ) ) == 0 );
1648
Hanno Beckerc77ab892017-08-23 11:01:06 +01001649 if( !successive )
1650 {
1651 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001652 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1653 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1654 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1655 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1656 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001657 }
1658 else
1659 {
1660 /* Import N, P, Q, D, E separately.
1661 * This should make no functional difference. */
1662
1663 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001664 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001665 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1666
1667 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1668 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001669 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001670 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1671
1672 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1673 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001674 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001675 NULL, 0, NULL, 0 ) == 0 );
1676
1677 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1678 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001679 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001680 NULL, 0 ) == 0 );
1681
1682 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1683 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001684 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001685 }
1686
Hanno Becker04877a42017-10-11 10:01:33 +01001687 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001688
Hanno Beckere1582a82017-09-29 11:51:05 +01001689 /* On expected success, perform some public and private
1690 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001691 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001692 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001693 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001694 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1695 else
1696 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1697
1698 if( res_check != 0 )
1699 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001700
1701 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1702 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1703 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1704 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1705 goto exit;
1706
1707 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1708 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1709
1710 /* Make sure the number we're generating is smaller than the modulus */
1711 buf_orig[0] = 0x00;
1712
1713 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1714
1715 if( is_priv )
1716 {
1717 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1718 &ctr_drbg, buf_enc,
1719 buf_dec ) == 0 );
1720
1721 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1722 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1723 }
1724 }
1725
Hanno Beckerc77ab892017-08-23 11:01:06 +01001726exit:
1727
Hanno Becker3f3ae852017-10-02 10:08:39 +01001728 mbedtls_free( buf_orig );
1729 mbedtls_free( buf_enc );
1730 mbedtls_free( buf_dec );
1731
Hanno Beckerc77ab892017-08-23 11:01:06 +01001732 mbedtls_rsa_free( &ctx );
1733
1734 mbedtls_ctr_drbg_free( &ctr_drbg );
1735 mbedtls_entropy_free( &entropy );
1736
1737}
1738/* END_CASE */
1739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001741void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001742{
Andres AG93012e82016-09-09 09:10:28 +01001743 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001744}
Paul Bakker33b43f12013-08-20 11:48:36 +02001745/* END_CASE */