blob: 1bf1850027786194aa01ccfa58182fec9bc7a038 [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,
208 valid_mode,
209 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,
213 invalid_mode,
214 0, sizeof( buf ), buf,
215 buf ) );
216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
217 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
218 valid_mode,
219 0, sizeof( buf ), NULL,
220 buf ) );
221 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
222 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
223 valid_mode,
224 0, sizeof( buf ), buf,
225 NULL ) );
226 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
227 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
228 valid_mode,
229 MBEDTLS_MD_SHA1,
230 0, NULL,
231 buf ) );
232
233 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
234 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
235 valid_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 invalid_mode,
241 0, sizeof( buf ), buf,
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 ), NULL,
247 buf ) );
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 0, sizeof( buf ), buf,
252 NULL ) );
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 MBEDTLS_MD_SHA1,
257 0, NULL,
258 buf ) );
259
260 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
261 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
262 valid_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 invalid_mode,
268 0, sizeof( buf ), buf,
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 ), NULL,
274 buf ) );
275 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
276 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
277 valid_mode,
278 0, sizeof( buf ), buf,
279 NULL ) );
280 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
281 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
282 valid_mode,
283 MBEDTLS_MD_SHA1,
284 0, NULL,
285 buf ) );
286
287 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Cédric Meutera05cbec2020-04-25 15:02:34 +0200288 mbedtls_rsa_rsassa_pss_sign_ext( NULL, NULL, NULL,
289 0, sizeof( buf ), buf,
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 ), NULL,
295 MBEDTLS_RSA_SALT_LEN_ANY,
296 buf ) );
297 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
298 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
299 0, sizeof( buf ), buf,
300 MBEDTLS_RSA_SALT_LEN_ANY,
301 NULL ) );
302 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
303 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
304 MBEDTLS_MD_SHA1,
305 0, NULL,
306 MBEDTLS_RSA_SALT_LEN_ANY,
307 buf ) );
308
309 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500310 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
311 valid_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 invalid_mode,
317 0, sizeof( buf ), buf,
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 ), NULL,
323 buf ) );
324 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
325 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
326 valid_mode,
327 0, sizeof( buf ), buf,
328 NULL ) );
329 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
330 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
331 valid_mode,
332 MBEDTLS_MD_SHA1, 0, NULL,
333 buf ) );
334
335 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
336 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
337 NULL,
338 valid_mode,
339 0, sizeof( buf ), buf,
340 buf ) );
341 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
342 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
343 NULL,
344 invalid_mode,
345 0, sizeof( buf ), buf,
346 buf ) );
347 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
348 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
349 NULL,
350 valid_mode,
351 0, sizeof( buf ),
352 NULL, buf ) );
353 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
354 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
355 NULL,
356 valid_mode,
357 0, sizeof( buf ), buf,
358 NULL ) );
359 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
360 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
361 NULL,
362 valid_mode,
363 MBEDTLS_MD_SHA1,
364 0, NULL,
365 buf ) );
366
367 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
368 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
369 valid_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 invalid_mode,
375 0, sizeof( buf ),
376 buf, 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 NULL, buf ) );
382 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
383 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
384 valid_mode,
385 0, sizeof( buf ),
386 buf, NULL ) );
387 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
388 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
389 valid_mode,
390 MBEDTLS_MD_SHA1,
391 0, NULL,
392 buf ) );
393
394 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
395 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
396 valid_mode,
397 0, sizeof( buf ),
398 buf,
399 0, 0,
400 buf ) );
401 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
402 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
403 invalid_mode,
404 0, sizeof( buf ),
405 buf,
406 0, 0,
407 buf ) );
408 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
409 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
410 valid_mode,
411 0, sizeof( buf ),
412 NULL, 0, 0,
413 buf ) );
414 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
415 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
416 valid_mode,
417 0, sizeof( buf ),
418 buf, 0, 0,
419 NULL ) );
420 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
421 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
422 valid_mode,
423 MBEDTLS_MD_SHA1,
424 0, NULL,
425 0, 0,
426 buf ) );
427
428 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
429 mbedtls_rsa_copy( NULL, &ctx ) );
430 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
431 mbedtls_rsa_copy( &ctx, NULL ) );
432
433exit:
434 return;
435}
436/* END_CASE */
437
Paul Bakker33b43f12013-08-20 11:48:36 +0200438/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100439void rsa_init_free( int reinit )
440{
441 mbedtls_rsa_context ctx;
442
443 /* Double free is not explicitly documented to work, but we rely on it
444 * even inside the library so that you can call mbedtls_rsa_free()
445 * unconditionally on an error path without checking whether it has
446 * already been called in the success path. */
447
448 mbedtls_rsa_init( &ctx, 0, 0 );
449 mbedtls_rsa_free( &ctx );
450
451 if( reinit )
452 mbedtls_rsa_init( &ctx, 0, 0 );
453 mbedtls_rsa_free( &ctx );
454
455 /* This test case always succeeds, functionally speaking. A plausible
456 * bug might trigger an invalid pointer dereference or a memory leak. */
457 goto exit;
458}
459/* END_CASE */
460
461/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100462void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100463 int digest, int mod, int radix_P, char * input_P,
464 int radix_Q, char * input_Q, int radix_N,
465 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200466 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000467{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200468 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
469 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100471 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200472 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000473
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100474 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
475 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000477
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200478 memset( hash_result, 0x00, sizeof( hash_result ) );
479 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200480 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000481
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100482 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
483 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
484 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
485 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000486
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100487 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
488 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100489 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000491
Paul Bakker42a29bf2009-07-07 20:18:41 +0000492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100494 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 +0000495
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200496 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
497 &rnd_info, MBEDTLS_RSA_PRIVATE, digest,
498 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200499 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000500 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000501
Ronald Cronac6ae352020-06-26 14:33:03 +0200502 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
503 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000504 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000505
Paul Bakkerbd51b262014-07-10 15:26:12 +0200506exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100507 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
508 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000510}
Paul Bakker33b43f12013-08-20 11:48:36 +0200511/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000512
Paul Bakker33b43f12013-08-20 11:48:36 +0200513/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100514void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100515 int digest, int mod, int radix_N,
516 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100517 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000518{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200519 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000521
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100522 mbedtls_mpi N, E;
523
524 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200526 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000527
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100528 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
529 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
530 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
531 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000533
Paul Bakker42a29bf2009-07-07 20:18:41 +0000534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100536 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 +0000537
Azim Khand30ca132017-06-09 04:32:58 +0100538 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 +0100539
Paul Bakkerbd51b262014-07-10 15:26:12 +0200540exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100541 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000543}
Paul Bakker33b43f12013-08-20 11:48:36 +0200544/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000545
Paul Bakker821fb082009-07-12 13:26:42 +0000546
Paul Bakker33b43f12013-08-20 11:48:36 +0200547/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100548void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100549 int padding_mode, int mod, int radix_P,
550 char * input_P, int radix_Q, char * input_Q,
551 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200552 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000553{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200554 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100556 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200557 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100560 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
561 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000562
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200563 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200564 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000565
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100566 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
567 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
568 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
569 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000570
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100571 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
572 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100573 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000575
Paul Bakker821fb082009-07-12 13:26:42 +0000576
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200577 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
578 &rnd_info, MBEDTLS_RSA_PRIVATE,
579 MBEDTLS_MD_NONE, hash_result->len,
580 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000581
Paul Bakker821fb082009-07-12 13:26:42 +0000582
Ronald Cronac6ae352020-06-26 14:33:03 +0200583 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
584 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000585
Paul Bakkerbd51b262014-07-10 15:26:12 +0200586exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100587 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
588 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000591}
Paul Bakker33b43f12013-08-20 11:48:36 +0200592/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000593
Paul Bakker33b43f12013-08-20 11:48:36 +0200594/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100595void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200596 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100597 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100598 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000599{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200600 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000602
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100603 mbedtls_mpi N, E;
604 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100607 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000608
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100609 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
610 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000611
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100612 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
613 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000615
Paul Bakker821fb082009-07-12 13:26:42 +0000616
Azim Khand30ca132017-06-09 04:32:58 +0100617 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 +0100618
Paul Bakkerbd51b262014-07-10 15:26:12 +0200619exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100620 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000622}
Paul Bakker33b43f12013-08-20 11:48:36 +0200623/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000624
Paul Bakker33b43f12013-08-20 11:48:36 +0200625/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100626void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100627 int mod, int radix_N, char * input_N,
628 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200629 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000630{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200631 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200633 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000634
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100635 mbedtls_mpi N, E;
636 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
637
Ronald Cron351f0ee2020-06-10 12:12:18 +0200638 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200641 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000642
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100643 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
644 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000645
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100646 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
647 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000649
Paul Bakker42a29bf2009-07-07 20:18:41 +0000650
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200651 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
652 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100653 &rnd_info, message_str->len,
654 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200655 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200656 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000657 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000658
Ronald Cronac6ae352020-06-26 14:33:03 +0200659 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
660 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000661 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100662
Paul Bakkerbd51b262014-07-10 15:26:12 +0200663exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100664 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000666}
Paul Bakker33b43f12013-08-20 11:48:36 +0200667/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000668
Paul Bakker33b43f12013-08-20 11:48:36 +0200669/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100670void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100671 int mod, int radix_N, char * input_N,
672 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200673 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000674{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200675 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000677
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100678 mbedtls_mpi N, E;
679
680 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200682 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000683
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100684 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
685 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000686
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100687 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
688 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000690
Paul Bakkera6656852010-07-18 19:47:14 +0000691
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200692 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100693 NULL, message_str->len,
694 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200695 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200696 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000697 {
Paul Bakkera6656852010-07-18 19:47:14 +0000698
Ronald Cronac6ae352020-06-26 14:33:03 +0200699 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
700 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000701 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100702
Paul Bakkerbd51b262014-07-10 15:26:12 +0200703exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100704 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000706}
Paul Bakker33b43f12013-08-20 11:48:36 +0200707/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000708
Paul Bakker33b43f12013-08-20 11:48:36 +0200709/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100710void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100711 int mod, int radix_P, char * input_P,
712 int radix_Q, char * input_Q, int radix_N,
713 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200714 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100715 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000716{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200717 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000719 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200720 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100721 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000722
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100723 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
724 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000727
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200728 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200729 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000730
Paul Bakker42a29bf2009-07-07 20:18:41 +0000731
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100732 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
733 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
734 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
735 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000736
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100737 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
738 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100739 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000741
Paul Bakker69998dd2009-07-11 19:15:20 +0000742 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000743
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200744 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100745 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200746 &output_len, message_str->x, output,
747 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200748 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000749 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000750
Ronald Cronac6ae352020-06-26 14:33:03 +0200751 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200752 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200753 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000754 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000755
Paul Bakkerbd51b262014-07-10 15:26:12 +0200756exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100757 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
758 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000760}
Paul Bakker33b43f12013-08-20 11:48:36 +0200761/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000762
Paul Bakker33b43f12013-08-20 11:48:36 +0200763/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100764void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100765 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200766 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000767{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200768 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000770
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100771 mbedtls_mpi N, E;
772
773 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
775 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200776 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000777
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100778 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
779 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000780
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100781 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
782 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000784
Paul Bakker821fb082009-07-12 13:26:42 +0000785
Azim Khand30ca132017-06-09 04:32:58 +0100786 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200787 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000788 {
Paul Bakker821fb082009-07-12 13:26:42 +0000789
Ronald Cronac6ae352020-06-26 14:33:03 +0200790 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
791 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000792 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100793
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100794 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200796 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100800
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200801 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100802 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100803 if( result == 0 )
804 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100805
Ronald Cronac6ae352020-06-26 14:33:03 +0200806 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
807 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100808 }
809
Paul Bakkerbd51b262014-07-10 15:26:12 +0200810exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100811 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_rsa_free( &ctx );
813 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000814}
Paul Bakker33b43f12013-08-20 11:48:36 +0200815/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000816
Paul Bakker33b43f12013-08-20 11:48:36 +0200817/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100818void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100819 char * input_P, int radix_Q, char * input_Q,
820 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200821 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100822 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000823{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200824 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100826 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200827 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200828 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000829
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100830 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
831 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
833 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000834
Ronald Cron351f0ee2020-06-10 12:12:18 +0200835 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000836
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100837 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
838 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
839 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
840 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000841
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100842 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
843 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100844 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000846
Paul Bakker821fb082009-07-12 13:26:42 +0000847
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200848 /* repeat three times to test updating of blinding values */
849 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000850 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200851 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200852 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
853 &rnd_info, message_str->x,
854 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200855 if( result == 0 )
856 {
Paul Bakker821fb082009-07-12 13:26:42 +0000857
Ronald Cronac6ae352020-06-26 14:33:03 +0200858 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200859 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200860 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200861 }
Paul Bakker821fb082009-07-12 13:26:42 +0000862 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000863
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100864 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200866 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100870
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200871 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200872 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
873 &rnd_info, message_str->x,
874 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100875 if( result == 0 )
876 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100877
Ronald Cronac6ae352020-06-26 14:33:03 +0200878 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200879 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200880 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100881 }
882
Paul Bakkerbd51b262014-07-10 15:26:12 +0200883exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100884 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
885 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000888}
Paul Bakker33b43f12013-08-20 11:48:36 +0200889/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000890
Paul Bakker33b43f12013-08-20 11:48:36 +0200891/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100892void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000893{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_rsa_context ctx;
895 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000898}
Paul Bakker33b43f12013-08-20 11:48:36 +0200899/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000900
Paul Bakker33b43f12013-08-20 11:48:36 +0200901/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100902void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
903 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000904{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100906 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000907
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100908 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000910
Paul Bakker33b43f12013-08-20 11:48:36 +0200911 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000912 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100913 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000914 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200915 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000916 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100917 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000918 }
919
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100920 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100922
Paul Bakkerbd51b262014-07-10 15:26:12 +0200923exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100924 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000926}
Paul Bakker33b43f12013-08-20 11:48:36 +0200927/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000928
Paul Bakker33b43f12013-08-20 11:48:36 +0200929/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100930void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
931 int radix_Q, char * input_Q, int radix_N,
932 char * input_N, int radix_E, char * input_E,
933 int radix_D, char * input_D, int radix_DP,
934 char * input_DP, int radix_DQ,
935 char * input_DQ, int radix_QP,
936 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000937{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000941
Paul Bakker33b43f12013-08-20 11:48:36 +0200942 ctx.len = mod / 8;
943 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000946 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200947 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000950 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200951 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000954 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200955 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000958 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200959 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000962 }
Hanno Becker131134f2017-08-23 08:31:07 +0100963#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200964 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000967 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200968 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000971 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200972 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000975 }
Hanno Becker131134f2017-08-23 08:31:07 +0100976#else
977 ((void) radix_DP); ((void) input_DP);
978 ((void) radix_DQ); ((void) input_DQ);
979 ((void) radix_QP); ((void) input_QP);
980#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100983
Paul Bakkerbd51b262014-07-10 15:26:12 +0200984exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000986}
Paul Bakker33b43f12013-08-20 11:48:36 +0200987/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000988
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100989/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100990void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
991 int radix_Epub, char * input_Epub, int radix_P,
992 char * input_P, int radix_Q, char * input_Q,
993 int radix_N, char * input_N, int radix_E,
994 char * input_E, int radix_D, char * input_D,
995 int radix_DP, char * input_DP, int radix_DQ,
996 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100997 int result )
998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1002 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001003
1004 pub.len = mod / 8;
1005 prv.len = mod / 8;
1006
1007 if( strlen( input_Npub ) )
1008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001010 }
1011 if( strlen( input_Epub ) )
1012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001014 }
1015
1016 if( strlen( input_P ) )
1017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001019 }
1020 if( strlen( input_Q ) )
1021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001023 }
1024 if( strlen( input_N ) )
1025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001027 }
1028 if( strlen( input_E ) )
1029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001031 }
1032 if( strlen( input_D ) )
1033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001035 }
Hanno Becker131134f2017-08-23 08:31:07 +01001036#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001037 if( strlen( input_DP ) )
1038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001040 }
1041 if( strlen( input_DQ ) )
1042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001044 }
1045 if( strlen( input_QP ) )
1046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001048 }
Hanno Becker131134f2017-08-23 08:31:07 +01001049#else
1050 ((void) radix_DP); ((void) input_DP);
1051 ((void) radix_DQ); ((void) input_DQ);
1052 ((void) radix_QP); ((void) input_QP);
1053#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001056
1057exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_rsa_free( &pub );
1059 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001060}
1061/* END_CASE */
1062
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001063/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 mbedtls_rsa_context ctx;
1067 mbedtls_entropy_context entropy;
1068 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001069 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001070
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001071 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001073 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001074
Hanno Beckera47023e2017-12-22 17:08:03 +00001075 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1076 &entropy, (const unsigned char *) pers,
1077 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001080 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001083 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001084 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001085
Paul Bakkerbd51b262014-07-10 15:26:12 +02001086exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_rsa_free( &ctx );
1088 mbedtls_ctr_drbg_free( &ctr_drbg );
1089 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001090}
Paul Bakker33b43f12013-08-20 11:48:36 +02001091/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001092
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001093/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001094void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001095 int radix_D, char *input_D,
1096 int radix_E, char *input_E,
1097 int radix_P, char *output_P,
1098 int radix_Q, char *output_Q,
1099 int corrupt, int result )
1100{
1101 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1102
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001103 mbedtls_mpi_init( &N );
1104 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1105 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1106 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1107
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001108 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1109 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1110 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1111 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1112 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1113
1114 if( corrupt )
1115 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1116
1117 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001118 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001119
1120 if( !corrupt )
1121 {
1122 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1123 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1124 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1125 }
1126
1127exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001128 mbedtls_mpi_free( &N );
1129 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1130 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1131 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001132}
1133/* END_CASE */
1134
Hanno Becker6b4ce492017-08-23 11:00:21 +01001135/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001136void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1137 int radix_Q, char *input_Q,
1138 int radix_E, char *input_E,
1139 int radix_D, char *output_D,
1140 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001141{
1142 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1143
1144 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1145 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1146 mbedtls_mpi_init( &E );
1147 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1148
1149 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1150 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1151 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1152 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1153
1154 if( corrupt )
1155 {
1156 /* Make E even */
1157 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1158 }
1159
1160 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001161 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1162 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001163
1164 if( !corrupt )
1165 {
1166 /*
1167 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1168 */
1169
1170 /* Replace P,Q by P-1, Q-1 */
1171 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1172 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1173
1174 /* Check D == Dp modulo P-1 */
1175 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1176 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1177 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1178
1179 /* Check D == Dp modulo Q-1 */
1180 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1181 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1182 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1183 }
1184
1185exit:
1186
1187 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1188 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1189 mbedtls_mpi_free( &E );
1190 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1191}
1192/* END_CASE */
1193
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001194/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001195void mbedtls_rsa_import( int radix_N, char *input_N,
1196 int radix_P, char *input_P,
1197 int radix_Q, char *input_Q,
1198 int radix_D, char *input_D,
1199 int radix_E, char *input_E,
1200 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001201 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001202 int res_check,
1203 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001204{
1205 mbedtls_mpi N, P, Q, D, E;
1206 mbedtls_rsa_context ctx;
1207
Hanno Beckere1582a82017-09-29 11:51:05 +01001208 /* Buffers used for encryption-decryption test */
1209 unsigned char *buf_orig = NULL;
1210 unsigned char *buf_enc = NULL;
1211 unsigned char *buf_dec = NULL;
1212
Hanno Beckerc77ab892017-08-23 11:01:06 +01001213 mbedtls_entropy_context entropy;
1214 mbedtls_ctr_drbg_context ctr_drbg;
1215 const char *pers = "test_suite_rsa";
1216
Hanno Becker4d6e8342017-09-29 11:50:18 +01001217 const int have_N = ( strlen( input_N ) > 0 );
1218 const int have_P = ( strlen( input_P ) > 0 );
1219 const int have_Q = ( strlen( input_Q ) > 0 );
1220 const int have_D = ( strlen( input_D ) > 0 );
1221 const int have_E = ( strlen( input_E ) > 0 );
1222
Hanno Beckerc77ab892017-08-23 11:01:06 +01001223 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001224 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001225 mbedtls_rsa_init( &ctx, 0, 0 );
1226
1227 mbedtls_mpi_init( &N );
1228 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1229 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1230
Hanno Beckerd4d60572018-01-10 07:12:01 +00001231 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1232 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1233
Hanno Becker4d6e8342017-09-29 11:50:18 +01001234 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001235 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1236
Hanno Becker4d6e8342017-09-29 11:50:18 +01001237 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001238 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1239
Hanno Becker4d6e8342017-09-29 11:50:18 +01001240 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001241 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1242
Hanno Becker4d6e8342017-09-29 11:50:18 +01001243 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001244 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1245
Hanno Becker4d6e8342017-09-29 11:50:18 +01001246 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001247 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1248
1249 if( !successive )
1250 {
1251 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001252 have_N ? &N : NULL,
1253 have_P ? &P : NULL,
1254 have_Q ? &Q : NULL,
1255 have_D ? &D : NULL,
1256 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001257 }
1258 else
1259 {
1260 /* Import N, P, Q, D, E separately.
1261 * This should make no functional difference. */
1262
1263 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001264 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001265 NULL, NULL, NULL, NULL ) == 0 );
1266
1267 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1268 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001269 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001270 NULL, NULL, NULL ) == 0 );
1271
1272 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1273 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001274 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001275 NULL, NULL ) == 0 );
1276
1277 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1278 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001279 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001280 NULL ) == 0 );
1281
1282 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1283 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001284 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001285 }
1286
Hanno Becker04877a42017-10-11 10:01:33 +01001287 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001288
Hanno Beckere1582a82017-09-29 11:51:05 +01001289 /* On expected success, perform some public and private
1290 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001291 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001292 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001293 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001294 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1295 else
1296 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1297
1298 if( res_check != 0 )
1299 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001300
1301 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1302 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1303 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1304 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1305 goto exit;
1306
1307 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1308 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1309
1310 /* Make sure the number we're generating is smaller than the modulus */
1311 buf_orig[0] = 0x00;
1312
1313 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1314
1315 if( is_priv )
1316 {
1317 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1318 &ctr_drbg, buf_enc,
1319 buf_dec ) == 0 );
1320
1321 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1322 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1323 }
1324 }
1325
Hanno Beckerc77ab892017-08-23 11:01:06 +01001326exit:
1327
Hanno Beckere1582a82017-09-29 11:51:05 +01001328 mbedtls_free( buf_orig );
1329 mbedtls_free( buf_enc );
1330 mbedtls_free( buf_dec );
1331
Hanno Beckerc77ab892017-08-23 11:01:06 +01001332 mbedtls_rsa_free( &ctx );
1333
1334 mbedtls_ctr_drbg_free( &ctr_drbg );
1335 mbedtls_entropy_free( &entropy );
1336
1337 mbedtls_mpi_free( &N );
1338 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1339 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1340}
1341/* END_CASE */
1342
Hanno Becker417f2d62017-08-23 11:44:51 +01001343/* BEGIN_CASE */
1344void mbedtls_rsa_export( int radix_N, char *input_N,
1345 int radix_P, char *input_P,
1346 int radix_Q, char *input_Q,
1347 int radix_D, char *input_D,
1348 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001349 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001350 int successive )
1351{
1352 /* Original MPI's with which we set up the RSA context */
1353 mbedtls_mpi N, P, Q, D, E;
1354
1355 /* Exported MPI's */
1356 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1357
1358 const int have_N = ( strlen( input_N ) > 0 );
1359 const int have_P = ( strlen( input_P ) > 0 );
1360 const int have_Q = ( strlen( input_Q ) > 0 );
1361 const int have_D = ( strlen( input_D ) > 0 );
1362 const int have_E = ( strlen( input_E ) > 0 );
1363
Hanno Becker417f2d62017-08-23 11:44:51 +01001364 mbedtls_rsa_context ctx;
1365
1366 mbedtls_rsa_init( &ctx, 0, 0 );
1367
1368 mbedtls_mpi_init( &N );
1369 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1370 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1371
1372 mbedtls_mpi_init( &Ne );
1373 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1374 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1375
1376 /* Setup RSA context */
1377
1378 if( have_N )
1379 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1380
1381 if( have_P )
1382 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1383
1384 if( have_Q )
1385 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1386
1387 if( have_D )
1388 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1389
1390 if( have_E )
1391 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1392
1393 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1394 strlen( input_N ) ? &N : NULL,
1395 strlen( input_P ) ? &P : NULL,
1396 strlen( input_Q ) ? &Q : NULL,
1397 strlen( input_D ) ? &D : NULL,
1398 strlen( input_E ) ? &E : NULL ) == 0 );
1399
Hanno Becker7f25f852017-10-10 16:56:22 +01001400 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001401
1402 /*
1403 * Export parameters and compare to original ones.
1404 */
1405
1406 /* N and E must always be present. */
1407 if( !successive )
1408 {
1409 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1410 }
1411 else
1412 {
1413 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1414 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1415 }
1416 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1417 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1418
1419 /* If we were providing enough information to setup a complete private context,
1420 * we expect to be able to export all core parameters. */
1421
1422 if( is_priv )
1423 {
1424 if( !successive )
1425 {
1426 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1427 &De, NULL ) == 0 );
1428 }
1429 else
1430 {
1431 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1432 NULL, NULL ) == 0 );
1433 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1434 NULL, NULL ) == 0 );
1435 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1436 &De, NULL ) == 0 );
1437 }
1438
1439 if( have_P )
1440 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1441
1442 if( have_Q )
1443 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1444
1445 if( have_D )
1446 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1447
1448 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001449 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1450 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001451 }
1452
1453exit:
1454
1455 mbedtls_rsa_free( &ctx );
1456
1457 mbedtls_mpi_free( &N );
1458 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1459 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1460
1461 mbedtls_mpi_free( &Ne );
1462 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1463 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1464}
1465/* END_CASE */
1466
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001467/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001468void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1469 int radix_P, char *input_P,
1470 int radix_Q, char *input_Q,
1471 int radix_D, char *input_D,
1472 int radix_E, char *input_E,
1473 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001474{
1475 /* Original MPI's with which we set up the RSA context */
1476 mbedtls_mpi N, P, Q, D, E;
1477
1478 const int have_N = ( strlen( input_N ) > 0 );
1479 const int have_P = ( strlen( input_P ) > 0 );
1480 const int have_Q = ( strlen( input_Q ) > 0 );
1481 const int have_D = ( strlen( input_D ) > 0 );
1482 const int have_E = ( strlen( input_E ) > 0 );
1483
1484 mbedtls_entropy_context entropy;
1485 mbedtls_ctr_drbg_context ctr_drbg;
1486 const char *pers = "test_suite_rsa";
1487
1488 mbedtls_mpi_init( &N );
1489 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1490 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1491
1492 mbedtls_ctr_drbg_init( &ctr_drbg );
1493 mbedtls_entropy_init( &entropy );
1494 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1495 &entropy, (const unsigned char *) pers,
1496 strlen( pers ) ) == 0 );
1497
1498 if( have_N )
1499 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1500
1501 if( have_P )
1502 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1503
1504 if( have_Q )
1505 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1506
1507 if( have_D )
1508 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1509
1510 if( have_E )
1511 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1512
Hanno Becker750e8b42017-08-25 07:54:27 +01001513 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1514 have_P ? &P : NULL,
1515 have_Q ? &Q : NULL,
1516 have_D ? &D : NULL,
1517 have_E ? &E : NULL,
1518 prng ? mbedtls_ctr_drbg_random : NULL,
1519 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001520exit:
1521
1522 mbedtls_ctr_drbg_free( &ctr_drbg );
1523 mbedtls_entropy_free( &entropy );
1524
1525 mbedtls_mpi_free( &N );
1526 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1527 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1528}
1529/* END_CASE */
1530
Hanno Beckerc77ab892017-08-23 11:01:06 +01001531/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001532void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1533 data_t *input_Q, data_t *input_D,
1534 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001535 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001536{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001537 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001538 unsigned char bufNe[256];
1539 unsigned char bufPe[128];
1540 unsigned char bufQe[128];
1541 unsigned char bufDe[256];
1542 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001543
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001544 mbedtls_rsa_context ctx;
1545
1546 mbedtls_rsa_init( &ctx, 0, 0 );
1547
1548 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001549 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001550 input_N->len ? input_N->x : NULL, input_N->len,
1551 input_P->len ? input_P->x : NULL, input_P->len,
1552 input_Q->len ? input_Q->x : NULL, input_Q->len,
1553 input_D->len ? input_D->x : NULL, input_D->len,
1554 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001555
Hanno Becker7f25f852017-10-10 16:56:22 +01001556 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001557
1558 /*
1559 * Export parameters and compare to original ones.
1560 */
1561
1562 /* N and E must always be present. */
1563 if( !successive )
1564 {
Azim Khand30ca132017-06-09 04:32:58 +01001565 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001566 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001567 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001568 }
1569 else
1570 {
Azim Khand30ca132017-06-09 04:32:58 +01001571 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001572 NULL, 0, NULL, 0, NULL, 0,
1573 NULL, 0 ) == 0 );
1574 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1575 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001576 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001577 }
Azim Khand30ca132017-06-09 04:32:58 +01001578 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1579 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001580
1581 /* If we were providing enough information to setup a complete private context,
1582 * we expect to be able to export all core parameters. */
1583
1584 if( is_priv )
1585 {
1586 if( !successive )
1587 {
1588 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001589 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1590 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1591 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001592 NULL, 0 ) == 0 );
1593 }
1594 else
1595 {
1596 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001597 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001598 NULL, 0, NULL, 0,
1599 NULL, 0 ) == 0 );
1600
1601 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001602 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001603 NULL, 0, NULL, 0 ) == 0 );
1604
Azim Khand30ca132017-06-09 04:32:58 +01001605 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1606 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001607 NULL, 0 ) == 0 );
1608 }
1609
Azim Khand30ca132017-06-09 04:32:58 +01001610 if( input_P->len )
1611 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001612
Azim Khand30ca132017-06-09 04:32:58 +01001613 if( input_Q->len )
1614 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001615
Azim Khand30ca132017-06-09 04:32:58 +01001616 if( input_D->len )
1617 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001618
1619 }
1620
1621exit:
1622 mbedtls_rsa_free( &ctx );
1623}
1624/* END_CASE */
1625
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001626/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001627void mbedtls_rsa_import_raw( data_t *input_N,
1628 data_t *input_P, data_t *input_Q,
1629 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001630 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001631 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001632 int res_check,
1633 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001634{
Hanno Beckere1582a82017-09-29 11:51:05 +01001635 /* Buffers used for encryption-decryption test */
1636 unsigned char *buf_orig = NULL;
1637 unsigned char *buf_enc = NULL;
1638 unsigned char *buf_dec = NULL;
1639
Hanno Beckerc77ab892017-08-23 11:01:06 +01001640 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001641 mbedtls_entropy_context entropy;
1642 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001643
Hanno Beckerc77ab892017-08-23 11:01:06 +01001644 const char *pers = "test_suite_rsa";
1645
1646 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001647 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001648 mbedtls_rsa_init( &ctx, 0, 0 );
1649
Hanno Beckerc77ab892017-08-23 11:01:06 +01001650 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1651 &entropy, (const unsigned char *) pers,
1652 strlen( pers ) ) == 0 );
1653
Hanno Beckerc77ab892017-08-23 11:01:06 +01001654 if( !successive )
1655 {
1656 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001657 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1658 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1659 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1660 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1661 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001662 }
1663 else
1664 {
1665 /* Import N, P, Q, D, E separately.
1666 * This should make no functional difference. */
1667
1668 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001669 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001670 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1671
1672 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1673 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001674 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001675 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1676
1677 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1678 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001679 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001680 NULL, 0, NULL, 0 ) == 0 );
1681
1682 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1683 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001684 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001685 NULL, 0 ) == 0 );
1686
1687 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1688 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001689 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001690 }
1691
Hanno Becker04877a42017-10-11 10:01:33 +01001692 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001693
Hanno Beckere1582a82017-09-29 11:51:05 +01001694 /* On expected success, perform some public and private
1695 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001696 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001697 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001698 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001699 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1700 else
1701 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1702
1703 if( res_check != 0 )
1704 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001705
1706 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1707 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1708 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1709 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1710 goto exit;
1711
1712 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1713 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1714
1715 /* Make sure the number we're generating is smaller than the modulus */
1716 buf_orig[0] = 0x00;
1717
1718 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1719
1720 if( is_priv )
1721 {
1722 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1723 &ctr_drbg, buf_enc,
1724 buf_dec ) == 0 );
1725
1726 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1727 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1728 }
1729 }
1730
Hanno Beckerc77ab892017-08-23 11:01:06 +01001731exit:
1732
Hanno Becker3f3ae852017-10-02 10:08:39 +01001733 mbedtls_free( buf_orig );
1734 mbedtls_free( buf_enc );
1735 mbedtls_free( buf_dec );
1736
Hanno Beckerc77ab892017-08-23 11:01:06 +01001737 mbedtls_rsa_free( &ctx );
1738
1739 mbedtls_ctr_drbg_free( &ctr_drbg );
1740 mbedtls_entropy_free( &entropy );
1741
1742}
1743/* END_CASE */
1744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001746void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001747{
Andres AG93012e82016-09-09 09:10:28 +01001748 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001749}
Paul Bakker33b43f12013-08-20 11:48:36 +02001750/* END_CASE */