1/* BEGIN_HEADER */
2#include "mbedtls/rsa.h"
3#include "mbedtls/md2.h"
4#include "mbedtls/md4.h"
5#include "mbedtls/md5.h"
6#include "mbedtls/sha1.h"
7#include "mbedtls/sha256.h"
8#include "mbedtls/sha512.h"
9#include "mbedtls/entropy.h"
10#include "mbedtls/ctr_drbg.h"
11/* END_HEADER */
12
13/* BEGIN_DEPENDENCIES
14 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
15 * END_DEPENDENCIES
16 */
17
18/* BEGIN_CASE */
19void mbedtls_rsa_pkcs1_sign( char *message_hex_string, int padding_mode, int digest,
20                     int mod, int radix_P, char *input_P, int radix_Q,
21                     char *input_Q, int radix_N, char *input_N, int radix_E,
22                     char *input_E, char *result_hex_str, int result )
23{
24    unsigned char message_str[1000];
25    unsigned char hash_result[1000];
26    unsigned char output[1000];
27    unsigned char output_str[1000];
28    mbedtls_rsa_context ctx;
29    mbedtls_mpi P1, Q1, H, G;
30    int msg_len;
31    rnd_pseudo_info rnd_info;
32
33    mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
34    mbedtls_rsa_init( &ctx, padding_mode, 0 );
35
36    memset( message_str, 0x00, 1000 );
37    memset( hash_result, 0x00, 1000 );
38    memset( output, 0x00, 1000 );
39    memset( output_str, 0x00, 1000 );
40    memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
41
42    ctx.len = mod / 8;
43    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
44    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
45    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
46    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
47
48    TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
49    TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
50    TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
51    TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H  ) == 0 );
52    TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H  ) == 0 );
53    TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
54    TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
55    TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
56
57    TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
58
59    msg_len = unhexify( message_str, message_hex_string );
60
61    if( mbedtls_md_info_from_type( digest ) != NULL )
62        TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
63
64    TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, digest, 0, hash_result, output ) == result );
65    if( result == 0 )
66    {
67        hexify( output_str, output, ctx.len );
68
69        TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
70    }
71
72exit:
73    mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
74    mbedtls_rsa_free( &ctx );
75}
76/* END_CASE */
77
78/* BEGIN_CASE */
79void mbedtls_rsa_pkcs1_verify( char *message_hex_string, int padding_mode, int digest,
80                       int mod, int radix_N, char *input_N, int radix_E,
81                       char *input_E, char *result_hex_str, int result )
82{
83    unsigned char message_str[1000];
84    unsigned char hash_result[1000];
85    unsigned char result_str[1000];
86    mbedtls_rsa_context ctx;
87    int msg_len;
88
89    mbedtls_rsa_init( &ctx, padding_mode, 0 );
90    memset( message_str, 0x00, 1000 );
91    memset( hash_result, 0x00, 1000 );
92    memset( result_str, 0x00, 1000 );
93
94    ctx.len = mod / 8;
95    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
96    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
97
98    TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
99
100    msg_len = unhexify( message_str, message_hex_string );
101    unhexify( result_str, result_hex_str );
102
103    if( mbedtls_md_info_from_type( digest ) != NULL )
104        TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
105
106    TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str ) == result );
107
108exit:
109    mbedtls_rsa_free( &ctx );
110}
111/* END_CASE */
112
113
114/* BEGIN_CASE */
115void rsa_pkcs1_sign_raw( char *message_hex_string, char *hash_result_string,
116                         int padding_mode, int mod, int radix_P, char *input_P,
117                         int radix_Q, char *input_Q, int radix_N,
118                         char *input_N, int radix_E, char *input_E,
119                         char *result_hex_str )
120{
121    unsigned char message_str[1000];
122    unsigned char hash_result[1000];
123    unsigned char output[1000];
124    unsigned char output_str[1000];
125    mbedtls_rsa_context ctx;
126    mbedtls_mpi P1, Q1, H, G;
127    int hash_len;
128    rnd_pseudo_info rnd_info;
129
130    mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
131    mbedtls_rsa_init( &ctx, padding_mode, 0 );
132
133    memset( message_str, 0x00, 1000 );
134    memset( hash_result, 0x00, 1000 );
135    memset( output, 0x00, 1000 );
136    memset( output_str, 0x00, 1000 );
137    memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
138
139    ctx.len = mod / 8;
140    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
141    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
142    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
143    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
144
145    TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
146    TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
147    TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
148    TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H  ) == 0 );
149    TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H  ) == 0 );
150    TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
151    TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
152    TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
153
154    TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
155
156    unhexify( message_str, message_hex_string );
157    hash_len = unhexify( hash_result, hash_result_string );
158
159    TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE, hash_len, hash_result, output ) == 0 );
160
161    hexify( output_str, output, ctx.len );
162
163    TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
164
165    /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
166    if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
167    {
168        memset( output, 0x00, 1000 );
169        memset( output_str, 0x00, 1000 );
170
171        TEST_ASSERT( mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
172                    &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
173                    hash_len, hash_result, output ) == 0 );
174
175        hexify( output_str, output, ctx.len );
176
177        TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
178    }
179
180exit:
181    mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
182    mbedtls_rsa_free( &ctx );
183}
184/* END_CASE */
185
186/* BEGIN_CASE */
187void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string,
188                           int padding_mode, int mod, int radix_N,
189                           char *input_N, int radix_E, char *input_E,
190                           char *result_hex_str, int correct )
191{
192    unsigned char message_str[1000];
193    unsigned char hash_result[1000];
194    unsigned char result_str[1000];
195    unsigned char output[1000];
196    mbedtls_rsa_context ctx;
197    size_t hash_len, olen;
198
199    mbedtls_rsa_init( &ctx, padding_mode, 0 );
200    memset( message_str, 0x00, 1000 );
201    memset( hash_result, 0x00, 1000 );
202    memset( result_str, 0x00, 1000 );
203    memset( output, 0x00, sizeof( output ) );
204
205    ctx.len = mod / 8;
206    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
207    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
208
209    TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
210
211    unhexify( message_str, message_hex_string );
212    hash_len = unhexify( hash_result, hash_result_string );
213    unhexify( result_str, result_hex_str );
214
215    TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE, hash_len, hash_result, result_str ) == correct );
216
217    /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
218    if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
219    {
220        int ok;
221
222        TEST_ASSERT( mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
223                    NULL, NULL, MBEDTLS_RSA_PUBLIC,
224                    &olen, result_str, output, sizeof( output ) ) == 0 );
225
226        ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0;
227        if( correct == 0 )
228            TEST_ASSERT( ok == 1 );
229        else
230            TEST_ASSERT( ok == 0 );
231    }
232
233exit:
234    mbedtls_rsa_free( &ctx );
235}
236/* END_CASE */
237
238/* BEGIN_CASE */
239void mbedtls_rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod,
240                        int radix_N, char *input_N, int radix_E, char *input_E,
241                        char *result_hex_str, int result )
242{
243    unsigned char message_str[1000];
244    unsigned char output[1000];
245    unsigned char output_str[1000];
246    mbedtls_rsa_context ctx;
247    size_t msg_len;
248    rnd_pseudo_info rnd_info;
249
250    memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
251
252    mbedtls_rsa_init( &ctx, padding_mode, 0 );
253    memset( message_str, 0x00, 1000 );
254    memset( output, 0x00, 1000 );
255    memset( output_str, 0x00, 1000 );
256
257    ctx.len = mod / 8;
258    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
259    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
260
261    TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
262
263    msg_len = unhexify( message_str, message_hex_string );
264
265    TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PUBLIC, msg_len, message_str, output ) == result );
266    if( result == 0 )
267    {
268        hexify( output_str, output, ctx.len );
269
270        TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
271    }
272
273exit:
274    mbedtls_rsa_free( &ctx );
275}
276/* END_CASE */
277
278/* BEGIN_CASE */
279void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode,
280                                int mod, int radix_N, char *input_N,
281                                int radix_E, char *input_E,
282                                char *result_hex_str, int result )
283{
284    unsigned char message_str[1000];
285    unsigned char output[1000];
286    unsigned char output_str[1000];
287    mbedtls_rsa_context ctx;
288    size_t msg_len;
289
290    mbedtls_rsa_init( &ctx, padding_mode, 0 );
291    memset( message_str, 0x00, 1000 );
292    memset( output, 0x00, 1000 );
293    memset( output_str, 0x00, 1000 );
294
295    ctx.len = mod / 8;
296    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
297    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
298
299    TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
300
301    msg_len = unhexify( message_str, message_hex_string );
302
303    TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL, MBEDTLS_RSA_PUBLIC, msg_len, message_str, output ) == result );
304    if( result == 0 )
305    {
306        hexify( output_str, output, ctx.len );
307
308        TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
309    }
310
311exit:
312    mbedtls_rsa_free( &ctx );
313}
314/* END_CASE */
315
316/* BEGIN_CASE */
317void mbedtls_rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod,
318                        int radix_P, char *input_P, int radix_Q, char *input_Q,
319                        int radix_N, char *input_N, int radix_E, char *input_E,
320                        int max_output, char *result_hex_str, int result )
321{
322    unsigned char message_str[1000];
323    unsigned char output[1000];
324    unsigned char output_str[1000];
325    mbedtls_rsa_context ctx;
326    mbedtls_mpi P1, Q1, H, G;
327    size_t output_len;
328    rnd_pseudo_info rnd_info;
329
330    mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
331    mbedtls_rsa_init( &ctx, padding_mode, 0 );
332
333    memset( message_str, 0x00, 1000 );
334    memset( output, 0x00, 1000 );
335    memset( output_str, 0x00, 1000 );
336    memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
337
338    ctx.len = mod / 8;
339    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
340    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
341    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
342    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
343
344    TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
345    TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
346    TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
347    TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H  ) == 0 );
348    TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H  ) == 0 );
349    TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
350    TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
351    TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
352
353    TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
354
355    unhexify( message_str, message_hex_string );
356    output_len = 0;
357
358    TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str, output, max_output ) == result );
359    if( result == 0 )
360    {
361        hexify( output_str, output, ctx.len );
362
363        TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
364    }
365
366exit:
367    mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
368    mbedtls_rsa_free( &ctx );
369}
370/* END_CASE */
371
372/* BEGIN_CASE */
373void mbedtls_rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N,
374                 int radix_E, char *input_E, char *result_hex_str, int result )
375{
376    unsigned char message_str[1000];
377    unsigned char output[1000];
378    unsigned char output_str[1000];
379    mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
380
381    mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
382    mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
383    memset( message_str, 0x00, 1000 );
384    memset( output, 0x00, 1000 );
385    memset( output_str, 0x00, 1000 );
386
387    ctx.len = mod / 8;
388    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
389    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
390
391    TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
392
393    unhexify( message_str, message_hex_string );
394
395    TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str, output ) == result );
396    if( result == 0 )
397    {
398        hexify( output_str, output, ctx.len );
399
400        TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
401    }
402
403    /* And now with the copy */
404    TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
405    /* clear the original to be sure */
406    mbedtls_rsa_free( &ctx );
407
408    TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
409
410    memset( output, 0x00, 1000 );
411    memset( output_str, 0x00, 1000 );
412    TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str, output ) == result );
413    if( result == 0 )
414    {
415        hexify( output_str, output, ctx2.len );
416
417        TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
418    }
419
420exit:
421    mbedtls_rsa_free( &ctx );
422    mbedtls_rsa_free( &ctx2 );
423}
424/* END_CASE */
425
426/* BEGIN_CASE */
427void mbedtls_rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
428                  int radix_Q, char *input_Q, int radix_N, char *input_N,
429                  int radix_E, char *input_E, char *result_hex_str, int result )
430{
431    unsigned char message_str[1000];
432    unsigned char output[1000];
433    unsigned char output_str[1000];
434    mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
435    mbedtls_mpi P1, Q1, H, G;
436    rnd_pseudo_info rnd_info;
437    int i;
438
439    mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
440    mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
441    mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
442
443    memset( message_str, 0x00, 1000 );
444    memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
445
446    ctx.len = mod / 8;
447    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
448    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
449    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
450    TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
451
452    TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
453    TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
454    TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
455    TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H  ) == 0 );
456    TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H  ) == 0 );
457    TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
458    TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
459    TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
460
461    TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
462
463    unhexify( message_str, message_hex_string );
464
465    /* repeat three times to test updating of blinding values */
466    for( i = 0; i < 3; i++ )
467    {
468        memset( output, 0x00, 1000 );
469        memset( output_str, 0x00, 1000 );
470        TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
471                                  message_str, output ) == result );
472        if( result == 0 )
473        {
474            hexify( output_str, output, ctx.len );
475
476            TEST_ASSERT( strcasecmp( (char *) output_str,
477                                              result_hex_str ) == 0 );
478        }
479    }
480
481    /* And now one more time with the copy */
482    TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
483    /* clear the original to be sure */
484    mbedtls_rsa_free( &ctx );
485
486    TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
487
488    memset( output, 0x00, 1000 );
489    memset( output_str, 0x00, 1000 );
490    TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
491                              message_str, output ) == result );
492    if( result == 0 )
493    {
494        hexify( output_str, output, ctx2.len );
495
496        TEST_ASSERT( strcasecmp( (char *) output_str,
497                                          result_hex_str ) == 0 );
498    }
499
500exit:
501    mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
502    mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
503}
504/* END_CASE */
505
506/* BEGIN_CASE */
507void rsa_check_privkey_null()
508{
509    mbedtls_rsa_context ctx;
510    memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
511
512    TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
513}
514/* END_CASE */
515
516/* BEGIN_CASE */
517void mbedtls_rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
518                       int result )
519{
520    mbedtls_rsa_context ctx;
521
522    mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
523
524    if( strlen( input_N ) )
525    {
526        TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
527    }
528    if( strlen( input_E ) )
529    {
530        TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
531    }
532
533    TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
534
535exit:
536    mbedtls_rsa_free( &ctx );
537}
538/* END_CASE */
539
540/* BEGIN_CASE */
541void mbedtls_rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
542                        char *input_Q, int radix_N, char *input_N,
543                        int radix_E, char *input_E, int radix_D, char *input_D,
544                        int radix_DP, char *input_DP, int radix_DQ,
545                        char *input_DQ, int radix_QP, char *input_QP,
546                        int result )
547{
548    mbedtls_rsa_context ctx;
549
550    mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
551
552    ctx.len = mod / 8;
553    if( strlen( input_P ) )
554    {
555        TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
556    }
557    if( strlen( input_Q ) )
558    {
559        TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
560    }
561    if( strlen( input_N ) )
562    {
563        TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
564    }
565    if( strlen( input_E ) )
566    {
567        TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
568    }
569    if( strlen( input_D ) )
570    {
571        TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
572    }
573    if( strlen( input_DP ) )
574    {
575        TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
576    }
577    if( strlen( input_DQ ) )
578    {
579        TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
580    }
581    if( strlen( input_QP ) )
582    {
583        TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
584    }
585
586    TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
587
588exit:
589    mbedtls_rsa_free( &ctx );
590}
591/* END_CASE */
592
593/* BEGIN_CASE */
594void rsa_check_pubpriv( int mod, int radix_Npub, char *input_Npub,
595                        int radix_Epub, char *input_Epub,
596                        int radix_P, char *input_P, int radix_Q,
597                        char *input_Q, int radix_N, char *input_N,
598                        int radix_E, char *input_E, int radix_D, char *input_D,
599                        int radix_DP, char *input_DP, int radix_DQ,
600                        char *input_DQ, int radix_QP, char *input_QP,
601                        int result )
602{
603    mbedtls_rsa_context pub, prv;
604
605    mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
606    mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
607
608    pub.len = mod / 8;
609    prv.len = mod / 8;
610
611    if( strlen( input_Npub ) )
612    {
613        TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
614    }
615    if( strlen( input_Epub ) )
616    {
617        TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
618    }
619
620    if( strlen( input_P ) )
621    {
622        TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
623    }
624    if( strlen( input_Q ) )
625    {
626        TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
627    }
628    if( strlen( input_N ) )
629    {
630        TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
631    }
632    if( strlen( input_E ) )
633    {
634        TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
635    }
636    if( strlen( input_D ) )
637    {
638        TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
639    }
640    if( strlen( input_DP ) )
641    {
642        TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
643    }
644    if( strlen( input_DQ ) )
645    {
646        TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
647    }
648    if( strlen( input_QP ) )
649    {
650        TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
651    }
652
653    TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
654
655exit:
656    mbedtls_rsa_free( &pub );
657    mbedtls_rsa_free( &prv );
658}
659/* END_CASE */
660
661/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
662void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
663{
664    mbedtls_rsa_context ctx;
665    mbedtls_entropy_context entropy;
666    mbedtls_ctr_drbg_context ctr_drbg;
667    const char *pers = "test_suite_rsa";
668
669    mbedtls_ctr_drbg_init( &ctr_drbg );
670
671    mbedtls_entropy_init( &entropy );
672    TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
673                                (const unsigned char *) pers, strlen( pers ) ) == 0 );
674
675    mbedtls_rsa_init( &ctx, 0, 0 );
676
677    TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
678    if( result == 0 )
679    {
680        TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
681        TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
682    }
683
684exit:
685    mbedtls_rsa_free( &ctx );
686    mbedtls_ctr_drbg_free( &ctr_drbg );
687    mbedtls_entropy_free( &entropy );
688}
689/* END_CASE */
690
691/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
692void rsa_selftest()
693{
694    TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
695}
696/* END_CASE */
697