blob: e53967b57e0e2128b97b577915376a8258ede3db [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +00002#include <polarssl/rsa.h>
Paul Bakker821fb082009-07-12 13:26:42 +00003#include <polarssl/md2.h>
4#include <polarssl/md4.h>
5#include <polarssl/md5.h>
Paul Bakker42a29bf2009-07-07 20:18:41 +00006#include <polarssl/sha1.h>
Paul Bakkerd2681d82013-06-30 14:49:12 +02007#include <polarssl/sha256.h>
8#include <polarssl/sha512.h>
Paul Bakkerc0a1a312011-12-04 17:12:15 +00009#include <polarssl/entropy.h>
10#include <polarssl/ctr_drbg.h>
Paul Bakker33b43f12013-08-20 11:48:36 +020011/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +000012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* BEGIN_DEPENDENCIES
14 * depends_on:POLARSSL_RSA_C:POLARSSL_BIGNUM_C:POLARSSL_GENPRIME
15 * END_DEPENDENCIES
16 */
Paul Bakker5690efc2011-05-26 13:16:06 +000017
Paul Bakker33b43f12013-08-20 11:48:36 +020018/* BEGIN_CASE */
19void 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 )
Paul Bakker42a29bf2009-07-07 20:18:41 +000023{
24 unsigned char message_str[1000];
25 unsigned char hash_result[1000];
26 unsigned char output[1000];
27 unsigned char output_str[1000];
28 rsa_context ctx;
29 mpi P1, Q1, H, G;
Paul Bakker69998dd2009-07-11 19:15:20 +000030 int msg_len;
Paul Bakker548957d2013-08-30 10:30:02 +020031 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +000032
Paul Bakker6c591fa2011-05-05 11:49:20 +000033 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakker33b43f12013-08-20 11:48:36 +020034 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000035
36 memset( message_str, 0x00, 1000 );
37 memset( hash_result, 0x00, 1000 );
38 memset( output, 0x00, 1000 );
39 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +020040 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +000041
Paul Bakker33b43f12013-08-20 11:48:36 +020042 ctx.len = mod / 8;
43 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
44 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
45 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
46 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000047
48 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
49 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
50 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
51 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
52 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
53 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
54 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
55 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
56
57 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
58
Paul Bakker33b43f12013-08-20 11:48:36 +020059 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +000060
Paul Bakker33b43f12013-08-20 11:48:36 +020061 if( md_info_from_type( digest ) != NULL )
62 TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000063
Paul Bakker548957d2013-08-30 10:30:02 +020064 TEST_ASSERT( rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, digest, 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020065 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +000066 {
67 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +000068
Paul Bakker33b43f12013-08-20 11:48:36 +020069 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +000070 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000071
72 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010073 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +000074}
Paul Bakker33b43f12013-08-20 11:48:36 +020075/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +000076
Paul Bakker33b43f12013-08-20 11:48:36 +020077/* BEGIN_CASE */
78void rsa_pkcs1_verify( char *message_hex_string, int padding_mode, int digest,
79 int mod, int radix_N, char *input_N, int radix_E,
80 char *input_E, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000081{
82 unsigned char message_str[1000];
83 unsigned char hash_result[1000];
84 unsigned char result_str[1000];
85 rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000086 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +000087
Paul Bakker33b43f12013-08-20 11:48:36 +020088 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000089 memset( message_str, 0x00, 1000 );
90 memset( hash_result, 0x00, 1000 );
91 memset( result_str, 0x00, 1000 );
92
Paul Bakker33b43f12013-08-20 11:48:36 +020093 ctx.len = mod / 8;
94 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
95 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000096
97 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
98
Paul Bakker33b43f12013-08-20 11:48:36 +020099 msg_len = unhexify( message_str, message_hex_string );
100 unhexify( result_str, result_hex_str );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000101
Paul Bakker33b43f12013-08-20 11:48:36 +0200102 if( md_info_from_type( digest ) != NULL )
103 TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000104
Paul Bakker548957d2013-08-30 10:30:02 +0200105 TEST_ASSERT( rsa_pkcs1_verify( &ctx, NULL, NULL, RSA_PUBLIC, digest, 0, hash_result, result_str ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100106
107 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000108}
Paul Bakker33b43f12013-08-20 11:48:36 +0200109/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000110
Paul Bakker821fb082009-07-12 13:26:42 +0000111
Paul Bakker33b43f12013-08-20 11:48:36 +0200112/* BEGIN_CASE */
113void rsa_pkcs1_sign_raw( char *message_hex_string, char *hash_result_string,
114 int padding_mode, int mod, int radix_P, char *input_P,
115 int radix_Q, char *input_Q, int radix_N,
116 char *input_N, int radix_E, char *input_E,
117 char *result_hex_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000118{
119 unsigned char message_str[1000];
120 unsigned char hash_result[1000];
121 unsigned char output[1000];
122 unsigned char output_str[1000];
123 rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000124 mpi P1, Q1, H, G;
Paul Bakkereaf90d92011-07-13 14:21:52 +0000125 int hash_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200126 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000127
Paul Bakker6c591fa2011-05-05 11:49:20 +0000128 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakker33b43f12013-08-20 11:48:36 +0200129 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000130
Paul Bakker42a29bf2009-07-07 20:18:41 +0000131 memset( message_str, 0x00, 1000 );
132 memset( hash_result, 0x00, 1000 );
133 memset( output, 0x00, 1000 );
134 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200135 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000136
Paul Bakker33b43f12013-08-20 11:48:36 +0200137 ctx.len = mod / 8;
138 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
139 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
140 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
141 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000142
143 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
144 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
145 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
146 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
147 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
148 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
149 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
150 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
151
152 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
153
Paul Bakker33b43f12013-08-20 11:48:36 +0200154 unhexify( message_str, message_hex_string );
155 hash_len = unhexify( hash_result, hash_result_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000156
Paul Bakker548957d2013-08-30 10:30:02 +0200157 TEST_ASSERT( rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, POLARSSL_MD_NONE, hash_len, hash_result, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000158
159 hexify( output_str, output, ctx.len );
160
Paul Bakker33b43f12013-08-20 11:48:36 +0200161 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000162
163 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100164 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000165}
Paul Bakker33b43f12013-08-20 11:48:36 +0200166/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000167
Paul Bakker33b43f12013-08-20 11:48:36 +0200168/* BEGIN_CASE */
169void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string,
170 int padding_mode, int mod, int radix_N,
171 char *input_N, int radix_E, char *input_E,
172 char *result_hex_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000173{
174 unsigned char message_str[1000];
175 unsigned char hash_result[1000];
176 unsigned char result_str[1000];
177 rsa_context ctx;
Paul Bakkereaf90d92011-07-13 14:21:52 +0000178 size_t hash_len;
Paul Bakker821fb082009-07-12 13:26:42 +0000179
Paul Bakker33b43f12013-08-20 11:48:36 +0200180 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000181 memset( message_str, 0x00, 1000 );
182 memset( hash_result, 0x00, 1000 );
183 memset( result_str, 0x00, 1000 );
184
Paul Bakker33b43f12013-08-20 11:48:36 +0200185 ctx.len = mod / 8;
186 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
187 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000188
189 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
190
Paul Bakker33b43f12013-08-20 11:48:36 +0200191 unhexify( message_str, message_hex_string );
192 hash_len = unhexify( hash_result, hash_result_string );
193 unhexify( result_str, result_hex_str );
Paul Bakker821fb082009-07-12 13:26:42 +0000194
Paul Bakker548957d2013-08-30 10:30:02 +0200195 TEST_ASSERT( rsa_pkcs1_verify( &ctx, NULL, NULL, RSA_PUBLIC, POLARSSL_MD_NONE, hash_len, hash_result, result_str ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100196
197 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000198}
Paul Bakker33b43f12013-08-20 11:48:36 +0200199/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000200
Paul Bakker33b43f12013-08-20 11:48:36 +0200201/* BEGIN_CASE */
202void rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod,
203 int radix_N, char *input_N, int radix_E, char *input_E,
204 char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000205{
206 unsigned char message_str[1000];
207 unsigned char output[1000];
208 unsigned char output_str[1000];
209 rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000210 size_t msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000211 rnd_pseudo_info rnd_info;
212
213 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000214
Paul Bakker33b43f12013-08-20 11:48:36 +0200215 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000216 memset( message_str, 0x00, 1000 );
217 memset( output, 0x00, 1000 );
218 memset( output_str, 0x00, 1000 );
219
Paul Bakker33b43f12013-08-20 11:48:36 +0200220 ctx.len = mod / 8;
221 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
222 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000223
224 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
225
Paul Bakker33b43f12013-08-20 11:48:36 +0200226 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000227
Paul Bakker33b43f12013-08-20 11:48:36 +0200228 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PUBLIC, msg_len, message_str, output ) == result );
229 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000230 {
231 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000232
Paul Bakker33b43f12013-08-20 11:48:36 +0200233 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000234 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100235
236 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000237}
Paul Bakker33b43f12013-08-20 11:48:36 +0200238/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000239
Paul Bakker33b43f12013-08-20 11:48:36 +0200240/* BEGIN_CASE */
241void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode,
242 int mod, int radix_N, char *input_N,
243 int radix_E, char *input_E,
244 char *result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000245{
246 unsigned char message_str[1000];
247 unsigned char output[1000];
248 unsigned char output_str[1000];
249 rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000250 size_t msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000251
Paul Bakker33b43f12013-08-20 11:48:36 +0200252 rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000253 memset( message_str, 0x00, 1000 );
254 memset( output, 0x00, 1000 );
255 memset( output_str, 0x00, 1000 );
256
Paul Bakker33b43f12013-08-20 11:48:36 +0200257 ctx.len = mod / 8;
258 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
259 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000260
261 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
262
Paul Bakker33b43f12013-08-20 11:48:36 +0200263 msg_len = unhexify( message_str, message_hex_string );
Paul Bakkera6656852010-07-18 19:47:14 +0000264
Paul Bakker33b43f12013-08-20 11:48:36 +0200265 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL, RSA_PUBLIC, msg_len, message_str, output ) == result );
266 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000267 {
268 hexify( output_str, output, ctx.len );
269
Paul Bakker33b43f12013-08-20 11:48:36 +0200270 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000271 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100272
273 rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000274}
Paul Bakker33b43f12013-08-20 11:48:36 +0200275/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000276
Paul Bakker33b43f12013-08-20 11:48:36 +0200277/* BEGIN_CASE */
278void rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod,
279 int radix_P, char *input_P, int radix_Q, char *input_Q,
280 int radix_N, char *input_N, int radix_E, char *input_E,
281 int max_output, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000282{
283 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000284 unsigned char output[1000];
285 unsigned char output_str[1000];
286 rsa_context ctx;
287 mpi P1, Q1, H, G;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000288 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200289 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000290
Paul Bakker6c591fa2011-05-05 11:49:20 +0000291 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakker33b43f12013-08-20 11:48:36 +0200292 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000293
294 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000295 memset( output, 0x00, 1000 );
296 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200297 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000298
Paul Bakker33b43f12013-08-20 11:48:36 +0200299 ctx.len = mod / 8;
300 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
301 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
302 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
303 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000304
305 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
306 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
307 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
308 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
309 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
310 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
311 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
312 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
313
314 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
315
Paul Bakker33b43f12013-08-20 11:48:36 +0200316 unhexify( message_str, message_hex_string );
Paul Bakker69998dd2009-07-11 19:15:20 +0000317 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000318
Paul Bakker548957d2013-08-30 10:30:02 +0200319 TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, &output_len, message_str, output, max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200320 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000321 {
322 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000323
Paul Bakker33b43f12013-08-20 11:48:36 +0200324 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000325 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000326
327 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100328 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000329}
Paul Bakker33b43f12013-08-20 11:48:36 +0200330/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000331
Paul Bakker33b43f12013-08-20 11:48:36 +0200332/* BEGIN_CASE */
333void rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N,
334 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000335{
336 unsigned char message_str[1000];
337 unsigned char output[1000];
338 unsigned char output_str[1000];
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100339 rsa_context ctx, ctx2; /* Also test rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000340
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000341 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100342 rsa_init( &ctx2, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000343 memset( message_str, 0x00, 1000 );
344 memset( output, 0x00, 1000 );
345 memset( output_str, 0x00, 1000 );
346
Paul Bakker33b43f12013-08-20 11:48:36 +0200347 ctx.len = mod / 8;
348 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
349 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000350
351 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
352
Paul Bakker33b43f12013-08-20 11:48:36 +0200353 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000354
Paul Bakker33b43f12013-08-20 11:48:36 +0200355 TEST_ASSERT( rsa_public( &ctx, message_str, output ) == result );
356 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000357 {
358 hexify( output_str, output, ctx.len );
359
Paul Bakker33b43f12013-08-20 11:48:36 +0200360 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000361 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100362
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100363 /* And now with the copy */
364 TEST_ASSERT( rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100365 rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100366
367 TEST_ASSERT( rsa_check_pubkey( &ctx2 ) == 0 );
368
369 memset( output, 0x00, 1000 );
370 memset( output_str, 0x00, 1000 );
371 TEST_ASSERT( rsa_public( &ctx2, message_str, output ) == result );
372 if( result == 0 )
373 {
374 hexify( output_str, output, ctx2.len );
375
376 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
377 }
378
379 rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000380}
Paul Bakker33b43f12013-08-20 11:48:36 +0200381/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000382
Paul Bakker33b43f12013-08-20 11:48:36 +0200383/* BEGIN_CASE */
384void rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
385 int radix_Q, char *input_Q, int radix_N, char *input_N,
386 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000387{
388 unsigned char message_str[1000];
389 unsigned char output[1000];
390 unsigned char output_str[1000];
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100391 rsa_context ctx, ctx2; /* Also test rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000392 mpi P1, Q1, H, G;
Paul Bakker548957d2013-08-30 10:30:02 +0200393 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200394 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000395
Paul Bakker6c591fa2011-05-05 11:49:20 +0000396 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000397 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100398 rsa_init( &ctx2, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000399
400 memset( message_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200401 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000402
Paul Bakker33b43f12013-08-20 11:48:36 +0200403 ctx.len = mod / 8;
404 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
405 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
406 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
407 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000408
409 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
410 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
411 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
412 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
413 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
414 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
415 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
416 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
417
418 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
419
Paul Bakker33b43f12013-08-20 11:48:36 +0200420 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000421
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200422 /* repeat three times to test updating of blinding values */
423 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000424 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200425 memset( output, 0x00, 1000 );
426 memset( output_str, 0x00, 1000 );
427 TEST_ASSERT( rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
428 message_str, output ) == result );
429 if( result == 0 )
430 {
431 hexify( output_str, output, ctx.len );
Paul Bakker821fb082009-07-12 13:26:42 +0000432
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200433 TEST_ASSERT( strcasecmp( (char *) output_str,
434 result_hex_str ) == 0 );
435 }
Paul Bakker821fb082009-07-12 13:26:42 +0000436 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000437
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100438 /* And now one more time with the copy */
439 TEST_ASSERT( rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100440 rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100441
442 TEST_ASSERT( rsa_check_privkey( &ctx2 ) == 0 );
443
444 memset( output, 0x00, 1000 );
445 memset( output_str, 0x00, 1000 );
446 TEST_ASSERT( rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
447 message_str, output ) == result );
448 if( result == 0 )
449 {
450 hexify( output_str, output, ctx2.len );
451
452 TEST_ASSERT( strcasecmp( (char *) output_str,
453 result_hex_str ) == 0 );
454 }
455
456 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
457 rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000458}
Paul Bakker33b43f12013-08-20 11:48:36 +0200459/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000460
Paul Bakker33b43f12013-08-20 11:48:36 +0200461/* BEGIN_CASE */
462void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000463{
464 rsa_context ctx;
465 memset( &ctx, 0x00, sizeof( rsa_context ) );
466
467 TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
468}
Paul Bakker33b43f12013-08-20 11:48:36 +0200469/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000470
Paul Bakker33b43f12013-08-20 11:48:36 +0200471/* BEGIN_CASE */
472void rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
473 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000474{
475 rsa_context ctx;
476
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000477 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000478
Paul Bakker33b43f12013-08-20 11:48:36 +0200479 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000480 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200481 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000482 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200483 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000484 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200485 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000486 }
487
Paul Bakker33b43f12013-08-20 11:48:36 +0200488 TEST_ASSERT( rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100489
490 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000491}
Paul Bakker33b43f12013-08-20 11:48:36 +0200492/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000493
Paul Bakker33b43f12013-08-20 11:48:36 +0200494/* BEGIN_CASE */
495void rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
496 char *input_Q, int radix_N, char *input_N,
497 int radix_E, char *input_E, int radix_D, char *input_D,
498 int radix_DP, char *input_DP, int radix_DQ,
499 char *input_DQ, int radix_QP, char *input_QP,
500 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000501{
502 rsa_context ctx;
503
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000504 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000505
Paul Bakker33b43f12013-08-20 11:48:36 +0200506 ctx.len = mod / 8;
507 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000508 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200509 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000510 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200511 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000512 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200513 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000514 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200515 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000516 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200517 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000518 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200519 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000520 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200521 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000522 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200523 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000524 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200525 TEST_ASSERT( mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000526 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200527 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000528 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200529 TEST_ASSERT( mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000530 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200531 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000532 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200533 TEST_ASSERT( mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000534 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200535 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000536 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200537 TEST_ASSERT( mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000538 }
Paul Bakker821fb082009-07-12 13:26:42 +0000539
Paul Bakker33b43f12013-08-20 11:48:36 +0200540 TEST_ASSERT( rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100541
542 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000543}
Paul Bakker33b43f12013-08-20 11:48:36 +0200544/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000545
Manuel Pégourié-Gonnarda0d758b2013-12-01 16:27:00 +0100546/* BEGIN_CASE depends_on:POLARSSL_CTR_DRBG_C:POLARSSL_ENTROPY_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200547void rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000548{
549 rsa_context ctx;
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000550 entropy_context entropy;
551 ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200552 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000553
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000554 entropy_init( &entropy );
555 TEST_ASSERT( ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200556 (const unsigned char *) pers, strlen( pers ) ) == 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000557
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000558 rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000559
Paul Bakker33b43f12013-08-20 11:48:36 +0200560 TEST_ASSERT( rsa_gen_key( &ctx, ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
561 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000562 {
563 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
564 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100565
566 rsa_free( &ctx );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200567 entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000568}
Paul Bakker33b43f12013-08-20 11:48:36 +0200569/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000570
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200571/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200572void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +0000573{
574 TEST_ASSERT( rsa_self_test( 0 ) == 0 );
575}
Paul Bakker33b43f12013-08-20 11:48:36 +0200576/* END_CASE */