Update mbedTLS sources
[platform/upstream/iotivity.git] / extlibs / mbedtls / mbedtls / tests / suites / test_suite_rsa.function
1 /* BEGIN_HEADER */
2 #include "mbedtls/rsa.h"
3 #include "mbedtls/rsa_internal.h"
4 #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"
12
13 /* END_HEADER */
14
15 /* BEGIN_DEPENDENCIES
16  * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
17  * END_DEPENDENCIES
18  */
19
20 /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
21 void 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,
70                             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 ) );
75
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 ) );
278     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 ) );
284
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     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
306                             mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
307                                                         valid_mode,
308                                                         MBEDTLS_MD_SHA1,
309                                                         0, NULL,
310                                                         buf ) );
311
312     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
313                             mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
314                                                          valid_mode,
315                                                          0, sizeof( buf ), buf,
316                                                          buf ) );
317     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
318                             mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
319                                                          invalid_mode,
320                                                          0, sizeof( buf ), buf,
321                                                          buf ) );
322     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
323                             mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
324                                                          valid_mode,
325                                                          0, sizeof( buf ), NULL,
326                                                          buf ) );
327     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
328                             mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
329                                                          valid_mode,
330                                                          0, sizeof( buf ), buf,
331                                                          NULL ) );
332     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
333                             mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
334                                                          valid_mode,
335                                                          MBEDTLS_MD_SHA1,
336                                                          0, NULL,
337                                                          buf ) );
338
339     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
340                             mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
341                                                       valid_mode,
342                                                       0, sizeof( buf ), buf,
343                                                       buf ) );
344     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
345                             mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
346                                                       invalid_mode,
347                                                       0, sizeof( buf ), buf,
348                                                       buf ) );
349     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
350                             mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
351                                                       valid_mode,
352                                                       0, sizeof( buf ), NULL,
353                                                       buf ) );
354     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
355                             mbedtls_rsa_pkcs1_verify( &ctx, NULL, 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_pkcs1_verify( &ctx, NULL, NULL,
361                                                       valid_mode,
362                                                       MBEDTLS_MD_SHA1, 0, NULL,
363                                                       buf ) );
364
365     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
366                             mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
367                                                           NULL,
368                                                           valid_mode,
369                                                           0, sizeof( buf ), buf,
370                                                           buf ) );
371     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
372                             mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
373                                                           NULL,
374                                                           invalid_mode,
375                                                           0, sizeof( buf ), buf,
376                                                           buf ) );
377     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
378                             mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
379                                                           NULL,
380                                                           valid_mode,
381                                                           0, sizeof( buf ),
382                                                           NULL, buf ) );
383     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
384                             mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
385                                                           NULL,
386                                                           valid_mode,
387                                                           0, sizeof( buf ), buf,
388                                                           NULL ) );
389     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
390                             mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
391                                                           NULL,
392                                                           valid_mode,
393                                                           MBEDTLS_MD_SHA1,
394                                                           0, NULL,
395                                                           buf ) );
396
397     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
398                             mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
399                                                            valid_mode,
400                                                            0, sizeof( buf ),
401                                                            buf, buf ) );
402     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
403                             mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
404                                                            invalid_mode,
405                                                            0, sizeof( buf ),
406                                                            buf, buf ) );
407     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
408                             mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
409                                                            valid_mode,
410                                                            0, sizeof( buf ),
411                                                            NULL, buf ) );
412     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
413                             mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
414                                                            valid_mode,
415                                                            0, sizeof( buf ),
416                                                            buf, NULL ) );
417     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
418                             mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
419                                                            valid_mode,
420                                                            MBEDTLS_MD_SHA1,
421                                                            0, NULL,
422                                                            buf ) );
423
424     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
425                             mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
426                                                                valid_mode,
427                                                                0, sizeof( buf ),
428                                                                buf,
429                                                                0, 0,
430                                                                buf ) );
431     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
432                             mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
433                                                                invalid_mode,
434                                                                0, sizeof( buf ),
435                                                                buf,
436                                                                0, 0,
437                                                                buf ) );
438     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
439                             mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
440                                                                valid_mode,
441                                                                0, sizeof( buf ),
442                                                                NULL, 0, 0,
443                                                                buf ) );
444     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
445                             mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
446                                                                valid_mode,
447                                                                0, sizeof( buf ),
448                                                                buf, 0, 0,
449                                                                NULL ) );
450     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
451                             mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
452                                                                valid_mode,
453                                                                MBEDTLS_MD_SHA1,
454                                                                0, NULL,
455                                                                0, 0,
456                                                                buf ) );
457
458     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
459                             mbedtls_rsa_copy( NULL, &ctx ) );
460     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
461                             mbedtls_rsa_copy( &ctx, NULL ) );
462
463 exit:
464     return;
465 }
466 /* END_CASE */
467
468 /* BEGIN_CASE */
469 void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
470                              int digest, int mod, int radix_P, char * input_P,
471                              int radix_Q, char * input_Q, int radix_N,
472                              char * input_N, int radix_E, char * input_E,
473                              data_t * result_hex_str, int result )
474 {
475     unsigned char hash_result[1000];
476     unsigned char output[1000];
477     mbedtls_rsa_context ctx;
478     mbedtls_mpi N, P, Q, E;
479     rnd_pseudo_info rnd_info;
480
481     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
482     mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
483     mbedtls_rsa_init( &ctx, padding_mode, 0 );
484
485     memset( hash_result, 0x00, 1000 );
486     memset( output, 0x00, 1000 );
487     memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
488
489     TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
490     TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
491     TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
492     TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
493
494     TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
495     TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
496     TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
497     TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
498
499
500     if( mbedtls_md_info_from_type( digest ) != NULL )
501         TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
502
503     TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
504                                          MBEDTLS_RSA_PRIVATE, digest, 0,
505                                          hash_result, output ) == result );
506     if( result == 0 )
507     {
508
509         TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
510     }
511
512 exit:
513     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
514     mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
515     mbedtls_rsa_free( &ctx );
516 }
517 /* END_CASE */
518
519 /* BEGIN_CASE */
520 void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
521                                int digest, int mod, int radix_N,
522                                char * input_N, int radix_E, char * input_E,
523                                data_t * result_str, int result )
524 {
525     unsigned char hash_result[1000];
526     mbedtls_rsa_context ctx;
527
528     mbedtls_mpi N, E;
529
530     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
531     mbedtls_rsa_init( &ctx, padding_mode, 0 );
532     memset( hash_result, 0x00, 1000 );
533
534     TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
535     TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
536     TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
537     TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
538     TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
539
540
541     if( mbedtls_md_info_from_type( digest ) != NULL )
542         TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
543
544     TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str->x ) == result );
545
546 exit:
547     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
548     mbedtls_rsa_free( &ctx );
549 }
550 /* END_CASE */
551
552
553 /* BEGIN_CASE */
554 void rsa_pkcs1_sign_raw( data_t * hash_result,
555                          int padding_mode, int mod, int radix_P,
556                          char * input_P, int radix_Q, char * input_Q,
557                          int radix_N, char * input_N, int radix_E,
558                          char * input_E, data_t * result_hex_str )
559 {
560     unsigned char output[1000];
561     mbedtls_rsa_context ctx;
562     mbedtls_mpi N, P, Q, E;
563     rnd_pseudo_info rnd_info;
564
565     mbedtls_rsa_init( &ctx, padding_mode, 0 );
566     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
567     mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
568
569     memset( output, 0x00, 1000 );
570     memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
571
572     TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
573     TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
574     TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
575     TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
576
577     TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
578     TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
579     TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
580     TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
581
582
583     TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
584                                          MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
585                                          hash_result->len, hash_result->x,
586                                          output ) == 0 );
587
588
589     TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
590
591 #if defined(MBEDTLS_PKCS1_V15)
592     /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
593     if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
594     {
595         int res;
596         memset( output, 0x00, 1000 );
597
598         res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
599                     &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
600                     hash_result->len, hash_result->x, output );
601
602 #if !defined(MBEDTLS_RSA_ALT)
603         TEST_ASSERT( res == 0 );
604 #else
605         TEST_ASSERT( ( res == 0 ) ||
606                      ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
607 #endif
608
609         if( res == 0 )
610         {
611             TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
612         }
613     }
614 #endif /* MBEDTLS_PKCS1_V15 */
615
616 exit:
617     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
618     mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
619
620     mbedtls_rsa_free( &ctx );
621 }
622 /* END_CASE */
623
624 /* BEGIN_CASE */
625 void rsa_pkcs1_verify_raw( data_t * hash_result,
626                            int padding_mode, int mod, int radix_N,
627                            char * input_N, int radix_E, char * input_E,
628                            data_t * result_str, int correct )
629 {
630     unsigned char output[1000];
631     mbedtls_rsa_context ctx;
632
633     mbedtls_mpi N, E;
634     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
635
636     mbedtls_rsa_init( &ctx, padding_mode, 0 );
637     memset( output, 0x00, sizeof( output ) );
638
639     TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
640     TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
641
642     TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
643     TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
644     TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
645
646
647     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 );
648
649 #if defined(MBEDTLS_PKCS1_V15)
650     /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
651     if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
652     {
653         int res;
654         int ok;
655         size_t olen;
656
657         res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
658                     NULL, NULL, MBEDTLS_RSA_PUBLIC,
659                     &olen, result_str->x, output, sizeof( output ) );
660
661 #if !defined(MBEDTLS_RSA_ALT)
662         TEST_ASSERT( res == 0 );
663 #else
664         TEST_ASSERT( ( res == 0 ) ||
665                      ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
666 #endif
667
668         if( res == 0 )
669         {
670             ok = olen == hash_result->len && memcmp( output, hash_result->x, olen ) == 0;
671             if( correct == 0 )
672                 TEST_ASSERT( ok == 1 );
673             else
674                 TEST_ASSERT( ok == 0 );
675         }
676     }
677 #endif /* MBEDTLS_PKCS1_V15 */
678
679 exit:
680     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
681     mbedtls_rsa_free( &ctx );
682 }
683 /* END_CASE */
684
685 /* BEGIN_CASE */
686 void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
687                                 int mod, int radix_N, char * input_N,
688                                 int radix_E, char * input_E,
689                                 data_t * result_hex_str, int result )
690 {
691     unsigned char output[1000];
692     mbedtls_rsa_context ctx;
693     rnd_pseudo_info rnd_info;
694
695     mbedtls_mpi N, E;
696     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
697
698     memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
699
700     mbedtls_rsa_init( &ctx, padding_mode, 0 );
701     memset( output, 0x00, 1000 );
702
703     TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
704     TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
705
706     TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
707     TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
708     TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
709
710
711     TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
712                                             MBEDTLS_RSA_PUBLIC, message_str->len,
713                                             message_str->x, output ) == result );
714     if( result == 0 )
715     {
716
717         TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
718     }
719
720 exit:
721     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
722     mbedtls_rsa_free( &ctx );
723 }
724 /* END_CASE */
725
726 /* BEGIN_CASE */
727 void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
728                                 int mod, int radix_N, char * input_N,
729                                 int radix_E, char * input_E,
730                                 data_t * result_hex_str, int result )
731 {
732     unsigned char output[1000];
733     mbedtls_rsa_context ctx;
734
735     mbedtls_mpi N, E;
736
737     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
738     mbedtls_rsa_init( &ctx, padding_mode, 0 );
739     memset( output, 0x00, 1000 );
740
741     TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
742     TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
743
744     TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
745     TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
746     TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
747
748
749     TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL,
750                                             MBEDTLS_RSA_PUBLIC, message_str->len,
751                                             message_str->x, output ) == result );
752     if( result == 0 )
753     {
754
755         TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
756     }
757
758 exit:
759     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
760     mbedtls_rsa_free( &ctx );
761 }
762 /* END_CASE */
763
764 /* BEGIN_CASE */
765 void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
766                                 int mod, int radix_P, char * input_P,
767                                 int radix_Q, char * input_Q, int radix_N,
768                                 char * input_N, int radix_E, char * input_E,
769                                 int max_output, data_t * result_hex_str,
770                                 int result )
771 {
772     unsigned char output[1000];
773     mbedtls_rsa_context ctx;
774     size_t output_len;
775     rnd_pseudo_info rnd_info;
776     mbedtls_mpi N, P, Q, E;
777
778     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
779     mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
780
781     mbedtls_rsa_init( &ctx, padding_mode, 0 );
782
783     memset( output, 0x00, 1000 );
784     memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
785
786
787     TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
788     TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
789     TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
790     TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
791
792     TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
793     TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
794     TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
795     TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
796
797     output_len = 0;
798
799     TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str->x, output, max_output ) == result );
800     if( result == 0 )
801     {
802
803         TEST_ASSERT( hexcmp( output, result_hex_str->x, output_len, result_hex_str->len ) == 0 );
804     }
805
806 exit:
807     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
808     mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
809     mbedtls_rsa_free( &ctx );
810 }
811 /* END_CASE */
812
813 /* BEGIN_CASE */
814 void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
815                          char * input_N, int radix_E, char * input_E,
816                          data_t * result_hex_str, int result )
817 {
818     unsigned char output[1000];
819     mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
820
821     mbedtls_mpi N, E;
822
823     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
824     mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
825     mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
826     memset( output, 0x00, 1000 );
827
828     TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
829     TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
830
831     TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
832     TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
833     TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
834
835
836     TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
837     if( result == 0 )
838     {
839
840         TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
841     }
842
843     /* And now with the copy */
844     TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
845     /* clear the original to be sure */
846     mbedtls_rsa_free( &ctx );
847
848     TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
849
850     memset( output, 0x00, 1000 );
851     TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
852     if( result == 0 )
853     {
854
855         TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
856     }
857
858 exit:
859     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
860     mbedtls_rsa_free( &ctx );
861     mbedtls_rsa_free( &ctx2 );
862 }
863 /* END_CASE */
864
865 /* BEGIN_CASE */
866 void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
867                           char * input_P, int radix_Q, char * input_Q,
868                           int radix_N, char * input_N, int radix_E,
869                           char * input_E, data_t * result_hex_str,
870                           int result )
871 {
872     unsigned char output[1000];
873     mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
874     mbedtls_mpi N, P, Q, E;
875     rnd_pseudo_info rnd_info;
876     int i;
877
878     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
879     mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
880     mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
881     mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
882
883     memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
884
885     TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
886     TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
887     TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
888     TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
889
890     TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
891     TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
892     TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
893     TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
894
895
896     /* repeat three times to test updating of blinding values */
897     for( i = 0; i < 3; i++ )
898     {
899         memset( output, 0x00, 1000 );
900         TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
901                                   message_str->x, output ) == result );
902         if( result == 0 )
903         {
904
905             TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
906         }
907     }
908
909     /* And now one more time with the copy */
910     TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
911     /* clear the original to be sure */
912     mbedtls_rsa_free( &ctx );
913
914     TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
915
916     memset( output, 0x00, 1000 );
917     TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
918                               message_str->x, output ) == result );
919     if( result == 0 )
920     {
921
922         TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx2.len, result_hex_str->len ) == 0 );
923     }
924
925 exit:
926     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
927     mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
928
929     mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
930 }
931 /* END_CASE */
932
933 /* BEGIN_CASE */
934 void rsa_check_privkey_null(  )
935 {
936     mbedtls_rsa_context ctx;
937     memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
938
939     TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
940 }
941 /* END_CASE */
942
943 /* BEGIN_CASE */
944 void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
945                                char * input_E, int result )
946 {
947     mbedtls_rsa_context ctx;
948     mbedtls_mpi N, E;
949
950     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
951     mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
952
953     if( strlen( input_N ) )
954     {
955         TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
956     }
957     if( strlen( input_E ) )
958     {
959         TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
960     }
961
962     TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
963     TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
964
965 exit:
966     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
967     mbedtls_rsa_free( &ctx );
968 }
969 /* END_CASE */
970
971 /* BEGIN_CASE */
972 void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
973                                 int radix_Q, char * input_Q, int radix_N,
974                                 char * input_N, int radix_E, char * input_E,
975                                 int radix_D, char * input_D, int radix_DP,
976                                 char * input_DP, int radix_DQ,
977                                 char * input_DQ, int radix_QP,
978                                 char * input_QP, int result )
979 {
980     mbedtls_rsa_context ctx;
981
982     mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
983
984     ctx.len = mod / 8;
985     if( strlen( input_P ) )
986     {
987         TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
988     }
989     if( strlen( input_Q ) )
990     {
991         TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
992     }
993     if( strlen( input_N ) )
994     {
995         TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
996     }
997     if( strlen( input_E ) )
998     {
999         TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
1000     }
1001     if( strlen( input_D ) )
1002     {
1003         TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
1004     }
1005 #if !defined(MBEDTLS_RSA_NO_CRT)
1006     if( strlen( input_DP ) )
1007     {
1008         TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
1009     }
1010     if( strlen( input_DQ ) )
1011     {
1012         TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
1013     }
1014     if( strlen( input_QP ) )
1015     {
1016         TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
1017     }
1018 #else
1019     ((void) radix_DP); ((void) input_DP);
1020     ((void) radix_DQ); ((void) input_DQ);
1021     ((void) radix_QP); ((void) input_QP);
1022 #endif
1023
1024     TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
1025
1026 exit:
1027     mbedtls_rsa_free( &ctx );
1028 }
1029 /* END_CASE */
1030
1031 /* BEGIN_CASE */
1032 void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
1033                         int radix_Epub, char * input_Epub, int radix_P,
1034                         char * input_P, int radix_Q, char * input_Q,
1035                         int radix_N, char * input_N, int radix_E,
1036                         char * input_E, int radix_D, char * input_D,
1037                         int radix_DP, char * input_DP, int radix_DQ,
1038                         char * input_DQ, int radix_QP, char * input_QP,
1039                         int result )
1040 {
1041     mbedtls_rsa_context pub, prv;
1042
1043     mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1044     mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
1045
1046     pub.len = mod / 8;
1047     prv.len = mod / 8;
1048
1049     if( strlen( input_Npub ) )
1050     {
1051         TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
1052     }
1053     if( strlen( input_Epub ) )
1054     {
1055         TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
1056     }
1057
1058     if( strlen( input_P ) )
1059     {
1060         TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
1061     }
1062     if( strlen( input_Q ) )
1063     {
1064         TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
1065     }
1066     if( strlen( input_N ) )
1067     {
1068         TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
1069     }
1070     if( strlen( input_E ) )
1071     {
1072         TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
1073     }
1074     if( strlen( input_D ) )
1075     {
1076         TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
1077     }
1078 #if !defined(MBEDTLS_RSA_NO_CRT)
1079     if( strlen( input_DP ) )
1080     {
1081         TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
1082     }
1083     if( strlen( input_DQ ) )
1084     {
1085         TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
1086     }
1087     if( strlen( input_QP ) )
1088     {
1089         TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
1090     }
1091 #else
1092     ((void) radix_DP); ((void) input_DP);
1093     ((void) radix_DQ); ((void) input_DQ);
1094     ((void) radix_QP); ((void) input_QP);
1095 #endif
1096
1097     TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
1098
1099 exit:
1100     mbedtls_rsa_free( &pub );
1101     mbedtls_rsa_free( &prv );
1102 }
1103 /* END_CASE */
1104
1105 /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
1106 void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
1107 {
1108     mbedtls_rsa_context ctx;
1109     mbedtls_entropy_context entropy;
1110     mbedtls_ctr_drbg_context ctr_drbg;
1111     const char *pers = "test_suite_rsa";
1112
1113     mbedtls_ctr_drbg_init( &ctr_drbg );
1114     mbedtls_entropy_init( &entropy );
1115     mbedtls_rsa_init ( &ctx, 0, 0 );
1116
1117     TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1118                                         &entropy, (const unsigned char *) pers,
1119                                         strlen( pers ) ) == 0 );
1120
1121     TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
1122     if( result == 0 )
1123     {
1124         TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
1125         TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
1126     }
1127
1128 exit:
1129     mbedtls_rsa_free( &ctx );
1130     mbedtls_ctr_drbg_free( &ctr_drbg );
1131     mbedtls_entropy_free( &entropy );
1132 }
1133 /* END_CASE */
1134
1135 /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
1136 void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
1137                                 int radix_D, char *input_D,
1138                                 int radix_E, char *input_E,
1139                                 int radix_P, char *output_P,
1140                                 int radix_Q, char *output_Q,
1141                                 int corrupt, int result )
1142 {
1143     mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1144
1145     mbedtls_mpi_init( &N );
1146     mbedtls_mpi_init( &P );  mbedtls_mpi_init( &Q  );
1147     mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1148     mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1149
1150     TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1151     TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1152     TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1153     TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1154     TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1155
1156     if( corrupt )
1157         TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1158
1159     /* Try to deduce P, Q from N, D, E only. */
1160     TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
1161
1162     if( !corrupt )
1163     {
1164         /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1165         TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1166                      ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1167     }
1168
1169 exit:
1170     mbedtls_mpi_free( &N );
1171     mbedtls_mpi_free( &P  ); mbedtls_mpi_free( &Q  );
1172     mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1173     mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1174 }
1175 /* END_CASE */
1176
1177 /* BEGIN_CASE */
1178 void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1179                                           int radix_Q, char *input_Q,
1180                                           int radix_E, char *input_E,
1181                                           int radix_D, char *output_D,
1182                                           int corrupt, int result )
1183 {
1184     mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1185
1186     mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1187     mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1188     mbedtls_mpi_init( &E );
1189     mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1190
1191     TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1192     TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1193     TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1194     TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1195
1196     if( corrupt )
1197     {
1198         /* Make E even */
1199         TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1200     }
1201
1202     /* Try to deduce D from N, P, Q, E. */
1203     TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1204                                                       &E, &D ) == result );
1205
1206     if( !corrupt )
1207     {
1208         /*
1209          * Check that D and Dp agree modulo LCM(P-1, Q-1).
1210          */
1211
1212         /* Replace P,Q by P-1, Q-1 */
1213         TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1214         TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1215
1216         /* Check D == Dp modulo P-1 */
1217         TEST_ASSERT( mbedtls_mpi_mod_mpi( &R,  &D,  &P ) == 0 );
1218         TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1219         TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R,  &Rp )     == 0 );
1220
1221         /* Check D == Dp modulo Q-1 */
1222         TEST_ASSERT( mbedtls_mpi_mod_mpi( &R,  &D,  &Q ) == 0 );
1223         TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1224         TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R,  &Rp )     == 0 );
1225     }
1226
1227 exit:
1228
1229     mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q  );
1230     mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1231     mbedtls_mpi_free( &E );
1232     mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1233 }
1234 /* END_CASE */
1235
1236 /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
1237 void mbedtls_rsa_import( int radix_N, char *input_N,
1238                          int radix_P, char *input_P,
1239                          int radix_Q, char *input_Q,
1240                          int radix_D, char *input_D,
1241                          int radix_E, char *input_E,
1242                          int successive,
1243                          int is_priv,
1244                          int res_check,
1245                          int res_complete )
1246 {
1247     mbedtls_mpi N, P, Q, D, E;
1248     mbedtls_rsa_context ctx;
1249
1250     /* Buffers used for encryption-decryption test */
1251     unsigned char *buf_orig = NULL;
1252     unsigned char *buf_enc  = NULL;
1253     unsigned char *buf_dec  = NULL;
1254
1255     mbedtls_entropy_context entropy;
1256     mbedtls_ctr_drbg_context ctr_drbg;
1257     const char *pers = "test_suite_rsa";
1258
1259     const int have_N = ( strlen( input_N ) > 0 );
1260     const int have_P = ( strlen( input_P ) > 0 );
1261     const int have_Q = ( strlen( input_Q ) > 0 );
1262     const int have_D = ( strlen( input_D ) > 0 );
1263     const int have_E = ( strlen( input_E ) > 0 );
1264
1265     mbedtls_ctr_drbg_init( &ctr_drbg );
1266     mbedtls_entropy_init( &entropy );
1267     mbedtls_rsa_init( &ctx, 0, 0 );
1268
1269     mbedtls_mpi_init( &N );
1270     mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1271     mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1272
1273     TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1274                                 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1275
1276     if( have_N )
1277         TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1278
1279     if( have_P )
1280         TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1281
1282     if( have_Q )
1283         TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1284
1285     if( have_D )
1286         TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1287
1288     if( have_E )
1289         TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1290
1291     if( !successive )
1292     {
1293         TEST_ASSERT( mbedtls_rsa_import( &ctx,
1294                              have_N ? &N : NULL,
1295                              have_P ? &P : NULL,
1296                              have_Q ? &Q : NULL,
1297                              have_D ? &D : NULL,
1298                              have_E ? &E : NULL ) == 0 );
1299     }
1300     else
1301     {
1302         /* Import N, P, Q, D, E separately.
1303          * This should make no functional difference. */
1304
1305         TEST_ASSERT( mbedtls_rsa_import( &ctx,
1306                                have_N ? &N : NULL,
1307                                NULL, NULL, NULL, NULL ) == 0 );
1308
1309         TEST_ASSERT( mbedtls_rsa_import( &ctx,
1310                                NULL,
1311                                have_P ? &P : NULL,
1312                                NULL, NULL, NULL ) == 0 );
1313
1314         TEST_ASSERT( mbedtls_rsa_import( &ctx,
1315                                NULL, NULL,
1316                                have_Q ? &Q : NULL,
1317                                NULL, NULL ) == 0 );
1318
1319         TEST_ASSERT( mbedtls_rsa_import( &ctx,
1320                                NULL, NULL, NULL,
1321                                have_D ? &D : NULL,
1322                                NULL ) == 0 );
1323
1324         TEST_ASSERT( mbedtls_rsa_import( &ctx,
1325                                NULL, NULL, NULL, NULL,
1326                                have_E ? &E : NULL ) == 0 );
1327     }
1328
1329     TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
1330
1331     /* On expected success, perform some public and private
1332      * key operations to check if the key is working properly. */
1333     if( res_complete == 0 )
1334     {
1335         if( is_priv )
1336             TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1337         else
1338             TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1339
1340         if( res_check != 0 )
1341             goto exit;
1342
1343         buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1344         buf_enc  = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1345         buf_dec  = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1346         if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1347             goto exit;
1348
1349         TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1350                               buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1351
1352         /* Make sure the number we're generating is smaller than the modulus */
1353         buf_orig[0] = 0x00;
1354
1355         TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1356
1357         if( is_priv )
1358         {
1359             TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1360                                               &ctr_drbg, buf_enc,
1361                                               buf_dec ) == 0 );
1362
1363             TEST_ASSERT( memcmp( buf_orig, buf_dec,
1364                                  mbedtls_rsa_get_len( &ctx ) ) == 0 );
1365         }
1366     }
1367
1368 exit:
1369
1370     mbedtls_free( buf_orig );
1371     mbedtls_free( buf_enc  );
1372     mbedtls_free( buf_dec  );
1373
1374     mbedtls_rsa_free( &ctx );
1375
1376     mbedtls_ctr_drbg_free( &ctr_drbg );
1377     mbedtls_entropy_free( &entropy );
1378
1379     mbedtls_mpi_free( &N );
1380     mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1381     mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1382 }
1383 /* END_CASE */
1384
1385 /* BEGIN_CASE */
1386 void mbedtls_rsa_export( int radix_N, char *input_N,
1387                          int radix_P, char *input_P,
1388                          int radix_Q, char *input_Q,
1389                          int radix_D, char *input_D,
1390                          int radix_E, char *input_E,
1391                          int is_priv,
1392                          int successive )
1393 {
1394     /* Original MPI's with which we set up the RSA context */
1395     mbedtls_mpi N, P, Q, D, E;
1396
1397     /* Exported MPI's */
1398     mbedtls_mpi Ne, Pe, Qe, De, Ee;
1399
1400     const int have_N = ( strlen( input_N ) > 0 );
1401     const int have_P = ( strlen( input_P ) > 0 );
1402     const int have_Q = ( strlen( input_Q ) > 0 );
1403     const int have_D = ( strlen( input_D ) > 0 );
1404     const int have_E = ( strlen( input_E ) > 0 );
1405
1406     mbedtls_rsa_context ctx;
1407
1408     mbedtls_rsa_init( &ctx, 0, 0 );
1409
1410     mbedtls_mpi_init( &N );
1411     mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1412     mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1413
1414     mbedtls_mpi_init( &Ne );
1415     mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1416     mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1417
1418     /* Setup RSA context */
1419
1420     if( have_N )
1421         TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1422
1423     if( have_P )
1424         TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1425
1426     if( have_Q )
1427         TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1428
1429     if( have_D )
1430         TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1431
1432     if( have_E )
1433         TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1434
1435     TEST_ASSERT( mbedtls_rsa_import( &ctx,
1436                                      strlen( input_N ) ? &N : NULL,
1437                                      strlen( input_P ) ? &P : NULL,
1438                                      strlen( input_Q ) ? &Q : NULL,
1439                                      strlen( input_D ) ? &D : NULL,
1440                                      strlen( input_E ) ? &E : NULL ) == 0 );
1441
1442     TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
1443
1444     /*
1445      * Export parameters and compare to original ones.
1446      */
1447
1448     /* N and E must always be present. */
1449     if( !successive )
1450     {
1451         TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1452     }
1453     else
1454     {
1455         TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1456         TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1457     }
1458     TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1459     TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1460
1461     /* If we were providing enough information to setup a complete private context,
1462      * we expect to be able to export all core parameters. */
1463
1464     if( is_priv )
1465     {
1466         if( !successive )
1467         {
1468             TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1469                                              &De, NULL ) == 0 );
1470         }
1471         else
1472         {
1473             TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1474                                              NULL, NULL ) == 0 );
1475             TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1476                                              NULL, NULL ) == 0 );
1477             TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1478                                              &De, NULL ) == 0 );
1479         }
1480
1481         if( have_P )
1482             TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1483
1484         if( have_Q )
1485             TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1486
1487         if( have_D )
1488             TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1489
1490         /* While at it, perform a sanity check */
1491         TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1492                                                        NULL, NULL ) == 0 );
1493     }
1494
1495 exit:
1496
1497     mbedtls_rsa_free( &ctx );
1498
1499     mbedtls_mpi_free( &N );
1500     mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1501     mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1502
1503     mbedtls_mpi_free( &Ne );
1504     mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1505     mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1506 }
1507 /* END_CASE */
1508
1509 /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
1510 void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1511                                   int radix_P, char *input_P,
1512                                   int radix_Q, char *input_Q,
1513                                   int radix_D, char *input_D,
1514                                   int radix_E, char *input_E,
1515                                   int prng, int result )
1516 {
1517     /* Original MPI's with which we set up the RSA context */
1518     mbedtls_mpi N, P, Q, D, E;
1519
1520     const int have_N = ( strlen( input_N ) > 0 );
1521     const int have_P = ( strlen( input_P ) > 0 );
1522     const int have_Q = ( strlen( input_Q ) > 0 );
1523     const int have_D = ( strlen( input_D ) > 0 );
1524     const int have_E = ( strlen( input_E ) > 0 );
1525
1526     mbedtls_entropy_context entropy;
1527     mbedtls_ctr_drbg_context ctr_drbg;
1528     const char *pers = "test_suite_rsa";
1529
1530     mbedtls_mpi_init( &N );
1531     mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1532     mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1533
1534     mbedtls_ctr_drbg_init( &ctr_drbg );
1535     mbedtls_entropy_init( &entropy );
1536     TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1537                                         &entropy, (const unsigned char *) pers,
1538                                         strlen( pers ) ) == 0 );
1539
1540     if( have_N )
1541         TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1542
1543     if( have_P )
1544         TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1545
1546     if( have_Q )
1547         TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1548
1549     if( have_D )
1550         TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1551
1552     if( have_E )
1553         TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1554
1555     TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1556                                         have_P ? &P : NULL,
1557                                         have_Q ? &Q : NULL,
1558                                         have_D ? &D : NULL,
1559                                         have_E ? &E : NULL,
1560                                         prng ? mbedtls_ctr_drbg_random : NULL,
1561                                         prng ? &ctr_drbg : NULL ) == result );
1562 exit:
1563
1564     mbedtls_ctr_drbg_free( &ctr_drbg );
1565     mbedtls_entropy_free( &entropy );
1566
1567     mbedtls_mpi_free( &N );
1568     mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1569     mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1570 }
1571 /* END_CASE */
1572
1573 /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
1574 void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1575                              data_t *input_Q, data_t *input_D,
1576                              data_t *input_E, int is_priv,
1577                              int successive )
1578 {
1579     /* Exported buffers */
1580     unsigned char bufNe[1000];
1581     unsigned char bufPe[1000];
1582     unsigned char bufQe[1000];
1583     unsigned char bufDe[1000];
1584     unsigned char bufEe[1000];
1585
1586     mbedtls_rsa_context ctx;
1587
1588     mbedtls_rsa_init( &ctx, 0, 0 );
1589
1590     /* Setup RSA context */
1591     TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1592                                input_N->len ? input_N->x : NULL, input_N->len,
1593                                input_P->len ? input_P->x : NULL, input_P->len,
1594                                input_Q->len ? input_Q->x : NULL, input_Q->len,
1595                                input_D->len ? input_D->x : NULL, input_D->len,
1596                                input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
1597
1598     TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
1599
1600     /*
1601      * Export parameters and compare to original ones.
1602      */
1603
1604     /* N and E must always be present. */
1605     if( !successive )
1606     {
1607         TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
1608                                              NULL, 0, NULL, 0, NULL, 0,
1609                                              bufEe, input_E->len ) == 0 );
1610     }
1611     else
1612     {
1613         TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
1614                                              NULL, 0, NULL, 0, NULL, 0,
1615                                              NULL, 0 ) == 0 );
1616         TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1617                                              NULL, 0, NULL, 0, NULL, 0,
1618                                              bufEe, input_E->len ) == 0 );
1619     }
1620     TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1621     TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
1622
1623     /* If we were providing enough information to setup a complete private context,
1624      * we expect to be able to export all core parameters. */
1625
1626     if( is_priv )
1627     {
1628         if( !successive )
1629         {
1630             TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1631                                          bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1632                                          bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1633                                          bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
1634                                          NULL, 0 ) == 0 );
1635         }
1636         else
1637         {
1638             TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1639                                          bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1640                                          NULL, 0, NULL, 0,
1641                                          NULL, 0 ) == 0 );
1642
1643             TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
1644                                          bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1645                                          NULL, 0, NULL, 0 ) == 0 );
1646
1647             TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1648                                          bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
1649                                          NULL, 0 ) == 0 );
1650         }
1651
1652         if( input_P->len )
1653             TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
1654
1655         if( input_Q->len )
1656             TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
1657
1658         if( input_D->len )
1659             TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
1660
1661     }
1662
1663 exit:
1664     mbedtls_rsa_free( &ctx );
1665 }
1666 /* END_CASE */
1667
1668 /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
1669 void mbedtls_rsa_import_raw( data_t *input_N,
1670                              data_t *input_P, data_t *input_Q,
1671                              data_t *input_D, data_t *input_E,
1672                              int successive,
1673                              int is_priv,
1674                              int res_check,
1675                              int res_complete )
1676 {
1677     /* Buffers used for encryption-decryption test */
1678     unsigned char *buf_orig = NULL;
1679     unsigned char *buf_enc  = NULL;
1680     unsigned char *buf_dec  = NULL;
1681
1682     mbedtls_rsa_context ctx;
1683     mbedtls_entropy_context entropy;
1684     mbedtls_ctr_drbg_context ctr_drbg;
1685
1686     const char *pers = "test_suite_rsa";
1687
1688     mbedtls_ctr_drbg_init( &ctr_drbg );
1689     mbedtls_entropy_init( &entropy );
1690     mbedtls_rsa_init( &ctx, 0, 0 );
1691
1692     TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1693                                         &entropy, (const unsigned char *) pers,
1694                                         strlen( pers ) ) == 0 );
1695
1696     if( !successive )
1697     {
1698         TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1699                                ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1700                                ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1701                                ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1702                                ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1703                                ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
1704     }
1705     else
1706     {
1707         /* Import N, P, Q, D, E separately.
1708          * This should make no functional difference. */
1709
1710         TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1711                                ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1712                                NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1713
1714         TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1715                                NULL, 0,
1716                                ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1717                                NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1718
1719         TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1720                                NULL, 0, NULL, 0,
1721                                ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1722                                NULL, 0, NULL, 0 ) == 0 );
1723
1724         TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1725                                NULL, 0, NULL, 0, NULL, 0,
1726                                ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1727                                NULL, 0 ) == 0 );
1728
1729         TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1730                                NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1731                                ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
1732     }
1733
1734     TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
1735
1736     /* On expected success, perform some public and private
1737      * key operations to check if the key is working properly. */
1738     if( res_complete == 0 )
1739     {
1740         if( is_priv )
1741             TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1742         else
1743             TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1744
1745         if( res_check != 0 )
1746             goto exit;
1747
1748         buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1749         buf_enc  = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1750         buf_dec  = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1751         if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1752             goto exit;
1753
1754         TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1755                               buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1756
1757         /* Make sure the number we're generating is smaller than the modulus */
1758         buf_orig[0] = 0x00;
1759
1760         TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1761
1762         if( is_priv )
1763         {
1764             TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1765                                               &ctr_drbg, buf_enc,
1766                                               buf_dec ) == 0 );
1767
1768             TEST_ASSERT( memcmp( buf_orig, buf_dec,
1769                                  mbedtls_rsa_get_len( &ctx ) ) == 0 );
1770         }
1771     }
1772
1773 exit:
1774
1775     mbedtls_free( buf_orig );
1776     mbedtls_free( buf_enc  );
1777     mbedtls_free( buf_dec  );
1778
1779     mbedtls_rsa_free( &ctx );
1780
1781     mbedtls_ctr_drbg_free( &ctr_drbg );
1782     mbedtls_entropy_free( &entropy );
1783
1784 }
1785 /* END_CASE */
1786
1787 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
1788 void rsa_selftest(  )
1789 {
1790     TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
1791 }
1792 /* END_CASE */