blob: b004c95736807289c816a1d414b9887bf5ed9663 [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"
Hanno Beckera565f542017-10-11 11:00:19 +01003#include "mbedtls/rsa_internal.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
Hanno Becker046d2022018-12-13 18:07:09 +000020/* 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
Hanno Beckera7ee0022018-12-18 13:30:20 +000031 TEST_INVALID_PARAM( mbedtls_rsa_init( NULL, valid_padding, 0 ) );
32 TEST_INVALID_PARAM( mbedtls_rsa_init( &ctx, invalid_padding, 0 ) );
Hanno Becker046d2022018-12-13 18:07:09 +000033 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,
Hanno Becker046d2022018-12-13 18:07:09 +000055 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,
Hanno Beckerf04d9232018-12-18 13:30:42 +000070 mbedtls_rsa_gen_key( NULL, rnd_std_rand,
71 NULL, 0, 0 ) );
72 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
73 mbedtls_rsa_gen_key( &ctx, NULL,
74 NULL, 0, 0 ) );
Hanno Becker046d2022018-12-13 18:07:09 +000075
76 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
77 mbedtls_rsa_check_pubkey( NULL ) );
78 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
79 mbedtls_rsa_check_privkey( NULL ) );
80
81 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
82 mbedtls_rsa_check_pub_priv( NULL, &ctx ) );
83 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
84 mbedtls_rsa_check_pub_priv( &ctx, NULL ) );
85
86 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
87 mbedtls_rsa_public( NULL, buf, buf ) );
88 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
89 mbedtls_rsa_public( &ctx, NULL, buf ) );
90 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
91 mbedtls_rsa_public( &ctx, buf, NULL ) );
92
93 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
94 mbedtls_rsa_private( NULL, NULL, NULL,
95 buf, buf ) );
96 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
97 mbedtls_rsa_private( &ctx, NULL, NULL,
98 NULL, buf ) );
99 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
100 mbedtls_rsa_private( &ctx, NULL, NULL,
101 buf, NULL ) );
102
103 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
104 mbedtls_rsa_pkcs1_encrypt( NULL, NULL, NULL,
105 valid_mode,
106 sizeof( buf ), buf,
107 buf ) );
108 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
109 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
110 invalid_mode,
111 sizeof( buf ), buf,
112 buf ) );
113 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
114 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
115 valid_mode,
116 sizeof( buf ), NULL,
117 buf ) );
118 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
119 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
120 valid_mode,
121 sizeof( buf ), buf,
122 NULL ) );
123
124 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
125 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
126 NULL,
127 valid_mode,
128 sizeof( buf ), buf,
129 buf ) );
130 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
131 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
132 NULL,
133 invalid_mode,
134 sizeof( buf ), buf,
135 buf ) );
136 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
137 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
138 NULL,
139 valid_mode,
140 sizeof( buf ), NULL,
141 buf ) );
142 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
143 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
144 NULL,
145 valid_mode,
146 sizeof( buf ), buf,
147 NULL ) );
148
149 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
150 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
151 valid_mode,
152 buf, sizeof( buf ),
153 sizeof( buf ), buf,
154 buf ) );
155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
156 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
157 invalid_mode,
158 buf, sizeof( buf ),
159 sizeof( buf ), buf,
160 buf ) );
161 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
162 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
163 valid_mode,
164 NULL, sizeof( buf ),
165 sizeof( buf ), buf,
166 buf ) );
167 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
168 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
169 valid_mode,
170 buf, sizeof( buf ),
171 sizeof( buf ), NULL,
172 buf ) );
173 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
174 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
175 valid_mode,
176 buf, sizeof( buf ),
177 sizeof( buf ), buf,
178 NULL ) );
179
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
181 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
182 valid_mode, &olen,
183 buf, buf, 42 ) );
184 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
185 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
186 invalid_mode, &olen,
187 buf, buf, 42 ) );
188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
189 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
190 valid_mode, NULL,
191 buf, buf, 42 ) );
192 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
193 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
194 valid_mode, &olen,
195 NULL, buf, 42 ) );
196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
197 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
198 valid_mode, &olen,
199 buf, NULL, 42 ) );
200
201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
202 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
203 NULL,
204 valid_mode, &olen,
205 buf, buf, 42 ) );
206 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
207 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
208 NULL,
209 invalid_mode, &olen,
210 buf, buf, 42 ) );
211 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
212 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
213 NULL,
214 valid_mode, NULL,
215 buf, buf, 42 ) );
216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
217 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
218 NULL,
219 valid_mode, &olen,
220 NULL, buf, 42 ) );
221 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
222 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
223 NULL,
224 valid_mode, &olen,
225 buf, NULL, 42 ) );
226
227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
228 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
229 valid_mode,
230 buf, sizeof( buf ),
231 &olen,
232 buf, buf, 42 ) );
233 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
234 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
235 invalid_mode,
236 buf, sizeof( buf ),
237 &olen,
238 buf, buf, 42 ) );
239 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
240 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
241 valid_mode,
242 NULL, sizeof( buf ),
243 NULL,
244 buf, buf, 42 ) );
245 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
246 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
247 valid_mode,
248 buf, sizeof( buf ),
249 &olen,
250 NULL, buf, 42 ) );
251 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
252 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
253 valid_mode,
254 buf, sizeof( buf ),
255 &olen,
256 buf, NULL, 42 ) );
257
258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
259 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
260 valid_mode,
261 0, sizeof( buf ), buf,
262 buf ) );
263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
264 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
265 invalid_mode,
266 0, sizeof( buf ), buf,
267 buf ) );
268 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
269 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
270 valid_mode,
271 0, sizeof( buf ), NULL,
272 buf ) );
273 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
274 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
275 valid_mode,
276 0, sizeof( buf ), buf,
277 NULL ) );
Hanno Becker05cf6da2018-12-18 13:33:37 +0000278 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
279 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
280 valid_mode,
281 MBEDTLS_MD_SHA1,
282 0, NULL,
283 buf ) );
Hanno Becker046d2022018-12-13 18:07:09 +0000284
285 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
286 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
287 valid_mode,
288 0, sizeof( buf ), buf,
289 buf ) );
290 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
291 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
292 invalid_mode,
293 0, sizeof( buf ), buf,
294 buf ) );
295 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
296 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
297 valid_mode,
298 0, sizeof( buf ), NULL,
299 buf ) );
300 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
301 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
302 valid_mode,
303 0, sizeof( buf ), buf,
304 NULL ) );
305
306 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
307 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
308 valid_mode,
309 0, sizeof( buf ), buf,
310 buf ) );
311 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
312 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
313 invalid_mode,
314 0, sizeof( buf ), buf,
315 buf ) );
316 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
317 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
318 valid_mode,
319 0, sizeof( buf ), NULL,
320 buf ) );
321 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
322 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
323 valid_mode,
324 0, sizeof( buf ), buf,
325 NULL ) );
326
327 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
328 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
329 valid_mode,
330 0, sizeof( buf ), buf,
331 buf ) );
332 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
333 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
334 invalid_mode,
335 0, sizeof( buf ), buf,
336 buf ) );
337 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
338 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
339 valid_mode,
340 0, sizeof( buf ), NULL,
341 buf ) );
342 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
343 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
344 valid_mode,
345 0, sizeof( buf ), buf,
346 NULL ) );
347
348 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
349 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
350 NULL,
351 valid_mode,
352 0, sizeof( buf ), buf,
353 buf ) );
354 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
355 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
356 NULL,
357 invalid_mode,
358 0, sizeof( buf ), buf,
359 buf ) );
360 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
361 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
362 NULL,
363 valid_mode,
364 0, sizeof( buf ),
365 NULL, buf ) );
366 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
367 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
368 NULL,
369 valid_mode,
370 0, sizeof( buf ), buf,
371 NULL ) );
372
373 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
374 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
375 valid_mode,
376 0, sizeof( buf ),
377 buf, buf ) );
378 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
379 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
380 invalid_mode,
381 0, sizeof( buf ),
382 buf, buf ) );
383 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
384 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
385 valid_mode,
386 0, sizeof( buf ),
387 NULL, buf ) );
388 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
389 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
390 valid_mode,
391 0, sizeof( buf ),
392 buf, NULL ) );
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
421 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
422 mbedtls_rsa_copy( NULL, &ctx ) );
423 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
424 mbedtls_rsa_copy( &ctx, NULL ) );
425
426exit:
427 return;
428}
429/* END_CASE */
430
Paul Bakker33b43f12013-08-20 11:48:36 +0200431/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100432void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100433 int digest, int mod, int radix_P, char * input_P,
434 int radix_Q, char * input_Q, int radix_N,
435 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100436 data_t * result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000437{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000438 unsigned char hash_result[1000];
439 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100441 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200442 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000443
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100444 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
445 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000447
Paul Bakker42a29bf2009-07-07 20:18:41 +0000448 memset( hash_result, 0x00, 1000 );
449 memset( output, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200450 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000451
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100452 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
453 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
454 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
455 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000456
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100457 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
458 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100459 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000461
Paul Bakker42a29bf2009-07-07 20:18:41 +0000462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100464 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 +0000465
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100466 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
467 MBEDTLS_RSA_PRIVATE, digest, 0,
468 hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200469 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000470 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000471
Azim Khand30ca132017-06-09 04:32:58 +0100472 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000473 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000474
Paul Bakkerbd51b262014-07-10 15:26:12 +0200475exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100476 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
477 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000479}
Paul Bakker33b43f12013-08-20 11:48:36 +0200480/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000481
Paul Bakker33b43f12013-08-20 11:48:36 +0200482/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100483void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100484 int digest, int mod, int radix_N,
485 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100486 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000487{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000488 unsigned char hash_result[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000490
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100491 mbedtls_mpi N, E;
492
493 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000495 memset( hash_result, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000496
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100497 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
498 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
499 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
500 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000502
Paul Bakker42a29bf2009-07-07 20:18:41 +0000503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100505 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 +0000506
Azim Khand30ca132017-06-09 04:32:58 +0100507 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 +0100508
Paul Bakkerbd51b262014-07-10 15:26:12 +0200509exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100510 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000512}
Paul Bakker33b43f12013-08-20 11:48:36 +0200513/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000514
Paul Bakker821fb082009-07-12 13:26:42 +0000515
Paul Bakker33b43f12013-08-20 11:48:36 +0200516/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100517void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100518 int padding_mode, int mod, int radix_P,
519 char * input_P, int radix_Q, char * input_Q,
520 int radix_N, char * input_N, int radix_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100521 char * input_E, data_t * result_hex_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000522{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000523 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100525 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200526 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100529 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
530 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000531
Paul Bakker42a29bf2009-07-07 20:18:41 +0000532 memset( output, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200533 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000534
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100535 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
536 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
537 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
538 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000539
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100540 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
541 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100542 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000544
Paul Bakker821fb082009-07-12 13:26:42 +0000545
Hanno Becker8fd55482017-08-23 14:07:48 +0100546 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
547 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
Azim Khand30ca132017-06-09 04:32:58 +0100548 hash_result->len, hash_result->x,
549 output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000550
Paul Bakker821fb082009-07-12 13:26:42 +0000551
Azim Khand30ca132017-06-09 04:32:58 +0100552 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000553
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200554#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100555 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100557 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100558 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100559 memset( output, 0x00, 1000 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100560
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100561 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Azim Khand30ca132017-06-09 04:32:58 +0100563 hash_result->len, hash_result->x, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100564
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100565#if !defined(MBEDTLS_RSA_ALT)
566 TEST_ASSERT( res == 0 );
567#else
568 TEST_ASSERT( ( res == 0 ) ||
569 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
570#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100571
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100572 if( res == 0 )
573 {
Azim Khand30ca132017-06-09 04:32:58 +0100574 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100575 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100576 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200577#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100578
Paul Bakkerbd51b262014-07-10 15:26:12 +0200579exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100580 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
581 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000584}
Paul Bakker33b43f12013-08-20 11:48:36 +0200585/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000586
Paul Bakker33b43f12013-08-20 11:48:36 +0200587/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100588void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200589 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100590 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100591 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000592{
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100593 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000595
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100596 mbedtls_mpi N, E;
597 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100600 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000601
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100602 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
603 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000604
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100605 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
606 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000608
Paul Bakker821fb082009-07-12 13:26:42 +0000609
Azim Khand30ca132017-06-09 04:32:58 +0100610 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 +0100611
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200612#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100613 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100615 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100616 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100617 int ok;
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200618 size_t olen;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100619
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100620 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Azim Khand30ca132017-06-09 04:32:58 +0100622 &olen, result_str->x, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100623
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100624#if !defined(MBEDTLS_RSA_ALT)
625 TEST_ASSERT( res == 0 );
626#else
627 TEST_ASSERT( ( res == 0 ) ||
628 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
629#endif
630
631 if( res == 0 )
632 {
Azim Khand30ca132017-06-09 04:32:58 +0100633 ok = olen == hash_result->len && memcmp( output, hash_result->x, olen ) == 0;
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100634 if( correct == 0 )
635 TEST_ASSERT( ok == 1 );
636 else
637 TEST_ASSERT( ok == 0 );
638 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100639 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200640#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100641
Paul Bakkerbd51b262014-07-10 15:26:12 +0200642exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100643 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000645}
Paul Bakker33b43f12013-08-20 11:48:36 +0200646/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000647
Paul Bakker33b43f12013-08-20 11:48:36 +0200648/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100649void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100650 int mod, int radix_N, char * input_N,
651 int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100652 data_t * result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000653{
Paul Bakker821fb082009-07-12 13:26:42 +0000654 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_rsa_context ctx;
Paul Bakker997bbd12011-03-13 15:45:42 +0000656 rnd_pseudo_info rnd_info;
657
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100658 mbedtls_mpi N, E;
659 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
660
Paul Bakker997bbd12011-03-13 15:45:42 +0000661 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000664 memset( output, 0x00, 1000 );
Paul Bakker821fb082009-07-12 13:26:42 +0000665
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100666 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
667 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000668
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100669 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
670 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000672
Paul Bakker42a29bf2009-07-07 20:18:41 +0000673
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100674 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100675 MBEDTLS_RSA_PUBLIC, message_str->len,
676 message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200677 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000678 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000679
Azim Khand30ca132017-06-09 04:32:58 +0100680 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000681 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100682
Paul Bakkerbd51b262014-07-10 15:26:12 +0200683exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100684 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000686}
Paul Bakker33b43f12013-08-20 11:48:36 +0200687/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000688
Paul Bakker33b43f12013-08-20 11:48:36 +0200689/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100690void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100691 int mod, int radix_N, char * input_N,
692 int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100693 data_t * result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000694{
Paul Bakkera6656852010-07-18 19:47:14 +0000695 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000697
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100698 mbedtls_mpi N, E;
699
700 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000702 memset( output, 0x00, 1000 );
Paul Bakkera6656852010-07-18 19:47:14 +0000703
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100704 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
705 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000706
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100707 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
708 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000710
Paul Bakkera6656852010-07-18 19:47:14 +0000711
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100712 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL,
Azim Khand30ca132017-06-09 04:32:58 +0100713 MBEDTLS_RSA_PUBLIC, message_str->len,
714 message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200715 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000716 {
Paul Bakkera6656852010-07-18 19:47:14 +0000717
Azim Khand30ca132017-06-09 04:32:58 +0100718 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000719 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100720
Paul Bakkerbd51b262014-07-10 15:26:12 +0200721exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100722 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000724}
Paul Bakker33b43f12013-08-20 11:48:36 +0200725/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000726
Paul Bakker33b43f12013-08-20 11:48:36 +0200727/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100728void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100729 int mod, int radix_P, char * input_P,
730 int radix_Q, char * input_Q, int radix_N,
731 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100732 int max_output, data_t * result_hex_str,
Azim Khand30ca132017-06-09 04:32:58 +0100733 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000734{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000735 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000737 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200738 rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100739 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000740
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100741 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
742 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000745
Paul Bakker42a29bf2009-07-07 20:18:41 +0000746 memset( output, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200747 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000748
Paul Bakker42a29bf2009-07-07 20:18:41 +0000749
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100750 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
751 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
752 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
753 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000754
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100755 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
756 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100757 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000759
Paul Bakker69998dd2009-07-11 19:15:20 +0000760 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000761
Azim Khand30ca132017-06-09 04:32:58 +0100762 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str->x, output, max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200763 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000764 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000765
Azim Khand30ca132017-06-09 04:32:58 +0100766 TEST_ASSERT( hexcmp( output, result_hex_str->x, output_len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000767 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000768
Paul Bakkerbd51b262014-07-10 15:26:12 +0200769exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100770 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
771 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000773}
Paul Bakker33b43f12013-08-20 11:48:36 +0200774/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000775
Paul Bakker33b43f12013-08-20 11:48:36 +0200776/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100777void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100778 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100779 data_t * result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000780{
Paul Bakker821fb082009-07-12 13:26:42 +0000781 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000783
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100784 mbedtls_mpi N, E;
785
786 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
788 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000789 memset( output, 0x00, 1000 );
Paul Bakker821fb082009-07-12 13:26:42 +0000790
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100791 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
792 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000793
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100794 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
795 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000797
Paul Bakker821fb082009-07-12 13:26:42 +0000798
Azim Khand30ca132017-06-09 04:32:58 +0100799 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200800 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000801 {
Paul Bakker821fb082009-07-12 13:26:42 +0000802
Azim Khand30ca132017-06-09 04:32:58 +0100803 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000804 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100805
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100806 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200808 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100812
813 memset( output, 0x00, 1000 );
Azim Khand30ca132017-06-09 04:32:58 +0100814 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100815 if( result == 0 )
816 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100817
Azim Khand30ca132017-06-09 04:32:58 +0100818 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100819 }
820
Paul Bakkerbd51b262014-07-10 15:26:12 +0200821exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100822 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_rsa_free( &ctx );
824 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000825}
Paul Bakker33b43f12013-08-20 11:48:36 +0200826/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000827
Paul Bakker33b43f12013-08-20 11:48:36 +0200828/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100829void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100830 char * input_P, int radix_Q, char * input_Q,
831 int radix_N, char * input_N, int radix_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100832 char * input_E, data_t * result_hex_str,
Azim Khand30ca132017-06-09 04:32:58 +0100833 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000834{
Paul Bakker821fb082009-07-12 13:26:42 +0000835 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100837 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200838 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200839 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000840
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100841 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
842 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
844 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000845
Paul Bakker548957d2013-08-30 10:30:02 +0200846 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000847
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100848 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
849 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
850 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
851 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000852
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100853 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
854 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100855 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000857
Paul Bakker821fb082009-07-12 13:26:42 +0000858
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200859 /* repeat three times to test updating of blinding values */
860 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000861 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200862 memset( output, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100864 message_str->x, output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200865 if( result == 0 )
866 {
Paul Bakker821fb082009-07-12 13:26:42 +0000867
Azim Khand30ca132017-06-09 04:32:58 +0100868 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200869 }
Paul Bakker821fb082009-07-12 13:26:42 +0000870 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000871
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100872 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200874 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100878
879 memset( output, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100881 message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100882 if( result == 0 )
883 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100884
Azim Khand30ca132017-06-09 04:32:58 +0100885 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx2.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100886 }
887
Paul Bakkerbd51b262014-07-10 15:26:12 +0200888exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100889 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
890 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000893}
Paul Bakker33b43f12013-08-20 11:48:36 +0200894/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000895
Paul Bakker33b43f12013-08-20 11:48:36 +0200896/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100897void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000898{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 mbedtls_rsa_context ctx;
900 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000903}
Paul Bakker33b43f12013-08-20 11:48:36 +0200904/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000905
Paul Bakker33b43f12013-08-20 11:48:36 +0200906/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100907void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
908 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100911 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000912
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100913 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000915
Paul Bakker33b43f12013-08-20 11:48:36 +0200916 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000917 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100918 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000919 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200920 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000921 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100922 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000923 }
924
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100925 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100927
Paul Bakkerbd51b262014-07-10 15:26:12 +0200928exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100929 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000931}
Paul Bakker33b43f12013-08-20 11:48:36 +0200932/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000933
Paul Bakker33b43f12013-08-20 11:48:36 +0200934/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100935void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
936 int radix_Q, char * input_Q, int radix_N,
937 char * input_N, int radix_E, char * input_E,
938 int radix_D, char * input_D, int radix_DP,
939 char * input_DP, int radix_DQ,
940 char * input_DQ, int radix_QP,
941 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000946
Paul Bakker33b43f12013-08-20 11:48:36 +0200947 ctx.len = mod / 8;
948 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000951 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200952 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000955 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200956 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000959 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200960 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000963 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200964 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000967 }
Hanno Becker131134f2017-08-23 08:31:07 +0100968#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200969 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000972 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200973 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000976 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200977 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000980 }
Hanno Becker131134f2017-08-23 08:31:07 +0100981#else
982 ((void) radix_DP); ((void) input_DP);
983 ((void) radix_DQ); ((void) input_DQ);
984 ((void) radix_QP); ((void) input_QP);
985#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100988
Paul Bakkerbd51b262014-07-10 15:26:12 +0200989exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000991}
Paul Bakker33b43f12013-08-20 11:48:36 +0200992/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000993
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100994/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100995void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
996 int radix_Epub, char * input_Epub, int radix_P,
997 char * input_P, int radix_Q, char * input_Q,
998 int radix_N, char * input_N, int radix_E,
999 char * input_E, int radix_D, char * input_D,
1000 int radix_DP, char * input_DP, int radix_DQ,
1001 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001002 int result )
1003{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1007 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001008
1009 pub.len = mod / 8;
1010 prv.len = mod / 8;
1011
1012 if( strlen( input_Npub ) )
1013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001015 }
1016 if( strlen( input_Epub ) )
1017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001019 }
1020
1021 if( strlen( input_P ) )
1022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001024 }
1025 if( strlen( input_Q ) )
1026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001028 }
1029 if( strlen( input_N ) )
1030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001032 }
1033 if( strlen( input_E ) )
1034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001036 }
1037 if( strlen( input_D ) )
1038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001040 }
Hanno Becker131134f2017-08-23 08:31:07 +01001041#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001042 if( strlen( input_DP ) )
1043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001045 }
1046 if( strlen( input_DQ ) )
1047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001049 }
1050 if( strlen( input_QP ) )
1051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001053 }
Hanno Becker131134f2017-08-23 08:31:07 +01001054#else
1055 ((void) radix_DP); ((void) input_DP);
1056 ((void) radix_DQ); ((void) input_DQ);
1057 ((void) radix_QP); ((void) input_QP);
1058#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001061
1062exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 mbedtls_rsa_free( &pub );
1064 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001065}
1066/* END_CASE */
1067
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001068/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_rsa_context ctx;
1072 mbedtls_entropy_context entropy;
1073 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001074 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001075
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001076 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001078 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001079
Hanno Beckera47023e2017-12-22 17:08:03 +00001080 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1081 &entropy, (const unsigned char *) pers,
1082 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001085 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001088 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001089 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001090
Paul Bakkerbd51b262014-07-10 15:26:12 +02001091exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_rsa_free( &ctx );
1093 mbedtls_ctr_drbg_free( &ctr_drbg );
1094 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001095}
Paul Bakker33b43f12013-08-20 11:48:36 +02001096/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001097
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001098/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001099void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001100 int radix_D, char *input_D,
1101 int radix_E, char *input_E,
1102 int radix_P, char *output_P,
1103 int radix_Q, char *output_Q,
1104 int corrupt, int result )
1105{
1106 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1107
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001108 mbedtls_mpi_init( &N );
1109 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1110 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1111 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1112
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001113 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1114 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1115 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1116 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1117 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1118
1119 if( corrupt )
1120 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1121
1122 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001123 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001124
1125 if( !corrupt )
1126 {
1127 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1128 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1129 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1130 }
1131
1132exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001133 mbedtls_mpi_free( &N );
1134 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1135 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1136 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001137}
1138/* END_CASE */
1139
Hanno Becker6b4ce492017-08-23 11:00:21 +01001140/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001141void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1142 int radix_Q, char *input_Q,
1143 int radix_E, char *input_E,
1144 int radix_D, char *output_D,
1145 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001146{
1147 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1148
1149 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1150 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1151 mbedtls_mpi_init( &E );
1152 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1153
1154 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1155 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1156 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1157 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1158
1159 if( corrupt )
1160 {
1161 /* Make E even */
1162 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1163 }
1164
1165 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001166 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1167 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001168
1169 if( !corrupt )
1170 {
1171 /*
1172 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1173 */
1174
1175 /* Replace P,Q by P-1, Q-1 */
1176 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1177 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1178
1179 /* Check D == Dp modulo P-1 */
1180 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1181 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1182 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1183
1184 /* Check D == Dp modulo Q-1 */
1185 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1186 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1187 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1188 }
1189
1190exit:
1191
1192 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1193 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1194 mbedtls_mpi_free( &E );
1195 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1196}
1197/* END_CASE */
1198
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001199/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001200void mbedtls_rsa_import( int radix_N, char *input_N,
1201 int radix_P, char *input_P,
1202 int radix_Q, char *input_Q,
1203 int radix_D, char *input_D,
1204 int radix_E, char *input_E,
1205 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001206 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001207 int res_check,
1208 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001209{
1210 mbedtls_mpi N, P, Q, D, E;
1211 mbedtls_rsa_context ctx;
1212
Hanno Beckere1582a82017-09-29 11:51:05 +01001213 /* Buffers used for encryption-decryption test */
1214 unsigned char *buf_orig = NULL;
1215 unsigned char *buf_enc = NULL;
1216 unsigned char *buf_dec = NULL;
1217
Hanno Beckerc77ab892017-08-23 11:01:06 +01001218 mbedtls_entropy_context entropy;
1219 mbedtls_ctr_drbg_context ctr_drbg;
1220 const char *pers = "test_suite_rsa";
1221
Hanno Becker4d6e8342017-09-29 11:50:18 +01001222 const int have_N = ( strlen( input_N ) > 0 );
1223 const int have_P = ( strlen( input_P ) > 0 );
1224 const int have_Q = ( strlen( input_Q ) > 0 );
1225 const int have_D = ( strlen( input_D ) > 0 );
1226 const int have_E = ( strlen( input_E ) > 0 );
1227
Hanno Beckerc77ab892017-08-23 11:01:06 +01001228 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001229 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001230 mbedtls_rsa_init( &ctx, 0, 0 );
1231
1232 mbedtls_mpi_init( &N );
1233 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1234 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1235
Hanno Beckerd4d60572018-01-10 07:12:01 +00001236 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1237 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1238
Hanno Becker4d6e8342017-09-29 11:50:18 +01001239 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001240 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1241
Hanno Becker4d6e8342017-09-29 11:50:18 +01001242 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001243 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1244
Hanno Becker4d6e8342017-09-29 11:50:18 +01001245 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001246 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1247
Hanno Becker4d6e8342017-09-29 11:50:18 +01001248 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001249 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1250
Hanno Becker4d6e8342017-09-29 11:50:18 +01001251 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001252 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1253
1254 if( !successive )
1255 {
1256 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001257 have_N ? &N : NULL,
1258 have_P ? &P : NULL,
1259 have_Q ? &Q : NULL,
1260 have_D ? &D : NULL,
1261 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001262 }
1263 else
1264 {
1265 /* Import N, P, Q, D, E separately.
1266 * This should make no functional difference. */
1267
1268 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001269 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001270 NULL, NULL, NULL, NULL ) == 0 );
1271
1272 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1273 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001274 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001275 NULL, NULL, NULL ) == 0 );
1276
1277 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1278 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001279 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001280 NULL, NULL ) == 0 );
1281
1282 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1283 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001284 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001285 NULL ) == 0 );
1286
1287 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1288 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001289 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001290 }
1291
Hanno Becker04877a42017-10-11 10:01:33 +01001292 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001293
Hanno Beckere1582a82017-09-29 11:51:05 +01001294 /* On expected success, perform some public and private
1295 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001296 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001297 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001298 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001299 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1300 else
1301 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1302
1303 if( res_check != 0 )
1304 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001305
1306 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1307 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1308 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1309 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1310 goto exit;
1311
1312 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1313 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1314
1315 /* Make sure the number we're generating is smaller than the modulus */
1316 buf_orig[0] = 0x00;
1317
1318 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1319
1320 if( is_priv )
1321 {
1322 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1323 &ctr_drbg, buf_enc,
1324 buf_dec ) == 0 );
1325
1326 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1327 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1328 }
1329 }
1330
Hanno Beckerc77ab892017-08-23 11:01:06 +01001331exit:
1332
Hanno Beckere1582a82017-09-29 11:51:05 +01001333 mbedtls_free( buf_orig );
1334 mbedtls_free( buf_enc );
1335 mbedtls_free( buf_dec );
1336
Hanno Beckerc77ab892017-08-23 11:01:06 +01001337 mbedtls_rsa_free( &ctx );
1338
1339 mbedtls_ctr_drbg_free( &ctr_drbg );
1340 mbedtls_entropy_free( &entropy );
1341
1342 mbedtls_mpi_free( &N );
1343 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1344 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1345}
1346/* END_CASE */
1347
Hanno Becker417f2d62017-08-23 11:44:51 +01001348/* BEGIN_CASE */
1349void mbedtls_rsa_export( int radix_N, char *input_N,
1350 int radix_P, char *input_P,
1351 int radix_Q, char *input_Q,
1352 int radix_D, char *input_D,
1353 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001354 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001355 int successive )
1356{
1357 /* Original MPI's with which we set up the RSA context */
1358 mbedtls_mpi N, P, Q, D, E;
1359
1360 /* Exported MPI's */
1361 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1362
1363 const int have_N = ( strlen( input_N ) > 0 );
1364 const int have_P = ( strlen( input_P ) > 0 );
1365 const int have_Q = ( strlen( input_Q ) > 0 );
1366 const int have_D = ( strlen( input_D ) > 0 );
1367 const int have_E = ( strlen( input_E ) > 0 );
1368
Hanno Becker417f2d62017-08-23 11:44:51 +01001369 mbedtls_rsa_context ctx;
1370
1371 mbedtls_rsa_init( &ctx, 0, 0 );
1372
1373 mbedtls_mpi_init( &N );
1374 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1375 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1376
1377 mbedtls_mpi_init( &Ne );
1378 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1379 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1380
1381 /* Setup RSA context */
1382
1383 if( have_N )
1384 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1385
1386 if( have_P )
1387 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1388
1389 if( have_Q )
1390 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1391
1392 if( have_D )
1393 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1394
1395 if( have_E )
1396 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1397
1398 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1399 strlen( input_N ) ? &N : NULL,
1400 strlen( input_P ) ? &P : NULL,
1401 strlen( input_Q ) ? &Q : NULL,
1402 strlen( input_D ) ? &D : NULL,
1403 strlen( input_E ) ? &E : NULL ) == 0 );
1404
Hanno Becker7f25f852017-10-10 16:56:22 +01001405 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001406
1407 /*
1408 * Export parameters and compare to original ones.
1409 */
1410
1411 /* N and E must always be present. */
1412 if( !successive )
1413 {
1414 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1415 }
1416 else
1417 {
1418 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1419 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1420 }
1421 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1422 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1423
1424 /* If we were providing enough information to setup a complete private context,
1425 * we expect to be able to export all core parameters. */
1426
1427 if( is_priv )
1428 {
1429 if( !successive )
1430 {
1431 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1432 &De, NULL ) == 0 );
1433 }
1434 else
1435 {
1436 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1437 NULL, NULL ) == 0 );
1438 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1439 NULL, NULL ) == 0 );
1440 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1441 &De, NULL ) == 0 );
1442 }
1443
1444 if( have_P )
1445 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1446
1447 if( have_Q )
1448 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1449
1450 if( have_D )
1451 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1452
1453 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001454 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1455 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001456 }
1457
1458exit:
1459
1460 mbedtls_rsa_free( &ctx );
1461
1462 mbedtls_mpi_free( &N );
1463 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1464 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1465
1466 mbedtls_mpi_free( &Ne );
1467 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1468 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1469}
1470/* END_CASE */
1471
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001472/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Becker750e8b42017-08-25 07:54:27 +01001473void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1474 int radix_P, char *input_P,
1475 int radix_Q, char *input_Q,
1476 int radix_D, char *input_D,
1477 int radix_E, char *input_E,
1478 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001479{
1480 /* Original MPI's with which we set up the RSA context */
1481 mbedtls_mpi N, P, Q, D, E;
1482
1483 const int have_N = ( strlen( input_N ) > 0 );
1484 const int have_P = ( strlen( input_P ) > 0 );
1485 const int have_Q = ( strlen( input_Q ) > 0 );
1486 const int have_D = ( strlen( input_D ) > 0 );
1487 const int have_E = ( strlen( input_E ) > 0 );
1488
1489 mbedtls_entropy_context entropy;
1490 mbedtls_ctr_drbg_context ctr_drbg;
1491 const char *pers = "test_suite_rsa";
1492
1493 mbedtls_mpi_init( &N );
1494 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1495 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1496
1497 mbedtls_ctr_drbg_init( &ctr_drbg );
1498 mbedtls_entropy_init( &entropy );
1499 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1500 &entropy, (const unsigned char *) pers,
1501 strlen( pers ) ) == 0 );
1502
1503 if( have_N )
1504 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1505
1506 if( have_P )
1507 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1508
1509 if( have_Q )
1510 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1511
1512 if( have_D )
1513 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1514
1515 if( have_E )
1516 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1517
Hanno Becker750e8b42017-08-25 07:54:27 +01001518 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1519 have_P ? &P : NULL,
1520 have_Q ? &Q : NULL,
1521 have_D ? &D : NULL,
1522 have_E ? &E : NULL,
1523 prng ? mbedtls_ctr_drbg_random : NULL,
1524 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001525exit:
1526
1527 mbedtls_ctr_drbg_free( &ctr_drbg );
1528 mbedtls_entropy_free( &entropy );
1529
1530 mbedtls_mpi_free( &N );
1531 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1532 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1533}
1534/* END_CASE */
1535
Hanno Beckerc77ab892017-08-23 11:01:06 +01001536/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001537void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1538 data_t *input_Q, data_t *input_D,
1539 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001540 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001541{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001542 /* Exported buffers */
Azim Khand30ca132017-06-09 04:32:58 +01001543 unsigned char bufNe[1000];
1544 unsigned char bufPe[1000];
1545 unsigned char bufQe[1000];
1546 unsigned char bufDe[1000];
1547 unsigned char bufEe[1000];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001548
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001549 mbedtls_rsa_context ctx;
1550
1551 mbedtls_rsa_init( &ctx, 0, 0 );
1552
1553 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001554 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001555 input_N->len ? input_N->x : NULL, input_N->len,
1556 input_P->len ? input_P->x : NULL, input_P->len,
1557 input_Q->len ? input_Q->x : NULL, input_Q->len,
1558 input_D->len ? input_D->x : NULL, input_D->len,
1559 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001560
Hanno Becker7f25f852017-10-10 16:56:22 +01001561 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001562
1563 /*
1564 * Export parameters and compare to original ones.
1565 */
1566
1567 /* N and E must always be present. */
1568 if( !successive )
1569 {
Azim Khand30ca132017-06-09 04:32:58 +01001570 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001571 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001572 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001573 }
1574 else
1575 {
Azim Khand30ca132017-06-09 04:32:58 +01001576 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001577 NULL, 0, NULL, 0, NULL, 0,
1578 NULL, 0 ) == 0 );
1579 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1580 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001581 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001582 }
Azim Khand30ca132017-06-09 04:32:58 +01001583 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1584 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001585
1586 /* If we were providing enough information to setup a complete private context,
1587 * we expect to be able to export all core parameters. */
1588
1589 if( is_priv )
1590 {
1591 if( !successive )
1592 {
1593 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001594 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1595 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1596 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001597 NULL, 0 ) == 0 );
1598 }
1599 else
1600 {
1601 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001602 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001603 NULL, 0, NULL, 0,
1604 NULL, 0 ) == 0 );
1605
1606 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001607 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001608 NULL, 0, NULL, 0 ) == 0 );
1609
Azim Khand30ca132017-06-09 04:32:58 +01001610 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1611 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001612 NULL, 0 ) == 0 );
1613 }
1614
Azim Khand30ca132017-06-09 04:32:58 +01001615 if( input_P->len )
1616 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001617
Azim Khand30ca132017-06-09 04:32:58 +01001618 if( input_Q->len )
1619 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001620
Azim Khand30ca132017-06-09 04:32:58 +01001621 if( input_D->len )
1622 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001623
1624 }
1625
1626exit:
1627 mbedtls_rsa_free( &ctx );
1628}
1629/* END_CASE */
1630
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001631/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001632void mbedtls_rsa_import_raw( data_t *input_N,
1633 data_t *input_P, data_t *input_Q,
1634 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001635 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001636 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001637 int res_check,
1638 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001639{
Hanno Beckere1582a82017-09-29 11:51:05 +01001640 /* Buffers used for encryption-decryption test */
1641 unsigned char *buf_orig = NULL;
1642 unsigned char *buf_enc = NULL;
1643 unsigned char *buf_dec = NULL;
1644
Hanno Beckerc77ab892017-08-23 11:01:06 +01001645 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001646 mbedtls_entropy_context entropy;
1647 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001648
Hanno Beckerc77ab892017-08-23 11:01:06 +01001649 const char *pers = "test_suite_rsa";
1650
1651 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001652 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001653 mbedtls_rsa_init( &ctx, 0, 0 );
1654
Hanno Beckerc77ab892017-08-23 11:01:06 +01001655 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1656 &entropy, (const unsigned char *) pers,
1657 strlen( pers ) ) == 0 );
1658
Hanno Beckerc77ab892017-08-23 11:01:06 +01001659 if( !successive )
1660 {
1661 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001662 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1663 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1664 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1665 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1666 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001667 }
1668 else
1669 {
1670 /* Import N, P, Q, D, E separately.
1671 * This should make no functional difference. */
1672
1673 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001674 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001675 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1676
1677 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1678 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001679 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001680 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1681
1682 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1683 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001684 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001685 NULL, 0, NULL, 0 ) == 0 );
1686
1687 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1688 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001689 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001690 NULL, 0 ) == 0 );
1691
1692 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1693 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001694 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001695 }
1696
Hanno Becker04877a42017-10-11 10:01:33 +01001697 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001698
Hanno Beckere1582a82017-09-29 11:51:05 +01001699 /* On expected success, perform some public and private
1700 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001701 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001702 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001703 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001704 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1705 else
1706 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1707
1708 if( res_check != 0 )
1709 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001710
1711 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1712 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1713 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1714 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1715 goto exit;
1716
1717 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1718 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1719
1720 /* Make sure the number we're generating is smaller than the modulus */
1721 buf_orig[0] = 0x00;
1722
1723 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1724
1725 if( is_priv )
1726 {
1727 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1728 &ctr_drbg, buf_enc,
1729 buf_dec ) == 0 );
1730
1731 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1732 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1733 }
1734 }
1735
Hanno Beckerc77ab892017-08-23 11:01:06 +01001736exit:
1737
Hanno Becker3f3ae852017-10-02 10:08:39 +01001738 mbedtls_free( buf_orig );
1739 mbedtls_free( buf_enc );
1740 mbedtls_free( buf_dec );
1741
Hanno Beckerc77ab892017-08-23 11:01:06 +01001742 mbedtls_rsa_free( &ctx );
1743
1744 mbedtls_ctr_drbg_free( &ctr_drbg );
1745 mbedtls_entropy_free( &entropy );
1746
1747}
1748/* END_CASE */
1749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001751void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001752{
Andres AG93012e82016-09-09 09:10:28 +01001753 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001754}
Paul Bakker33b43f12013-08-20 11:48:36 +02001755/* END_CASE */