blob: 29651ccea326b4461745a3f2865129831255932c [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];
339 rsa_context ctx;
340
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000341 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000342 memset( message_str, 0x00, 1000 );
343 memset( output, 0x00, 1000 );
344 memset( output_str, 0x00, 1000 );
345
Paul Bakker33b43f12013-08-20 11:48:36 +0200346 ctx.len = mod / 8;
347 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
348 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000349
350 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
351
Paul Bakker33b43f12013-08-20 11:48:36 +0200352 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000353
Paul Bakker33b43f12013-08-20 11:48:36 +0200354 TEST_ASSERT( rsa_public( &ctx, message_str, output ) == result );
355 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000356 {
357 hexify( output_str, output, ctx.len );
358
Paul Bakker33b43f12013-08-20 11:48:36 +0200359 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000360 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100361
362 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000363}
Paul Bakker33b43f12013-08-20 11:48:36 +0200364/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000365
Paul Bakker33b43f12013-08-20 11:48:36 +0200366/* BEGIN_CASE */
367void rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
368 int radix_Q, char *input_Q, int radix_N, char *input_N,
369 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000370{
371 unsigned char message_str[1000];
372 unsigned char output[1000];
373 unsigned char output_str[1000];
374 rsa_context ctx;
375 mpi P1, Q1, H, G;
Paul Bakker548957d2013-08-30 10:30:02 +0200376 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200377 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000378
Paul Bakker6c591fa2011-05-05 11:49:20 +0000379 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000380 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000381
382 memset( message_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200383 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000384
Paul Bakker33b43f12013-08-20 11:48:36 +0200385 ctx.len = mod / 8;
386 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
387 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
388 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
389 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000390
391 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
392 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
393 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
394 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
395 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
396 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
397 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
398 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
399
400 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
401
Paul Bakker33b43f12013-08-20 11:48:36 +0200402 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000403
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200404 /* repeat three times to test updating of blinding values */
405 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000406 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200407 memset( output, 0x00, 1000 );
408 memset( output_str, 0x00, 1000 );
409 TEST_ASSERT( rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
410 message_str, output ) == result );
411 if( result == 0 )
412 {
413 hexify( output_str, output, ctx.len );
Paul Bakker821fb082009-07-12 13:26:42 +0000414
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200415 TEST_ASSERT( strcasecmp( (char *) output_str,
416 result_hex_str ) == 0 );
417 }
Paul Bakker821fb082009-07-12 13:26:42 +0000418 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000419
420 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100421 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000422}
Paul Bakker33b43f12013-08-20 11:48:36 +0200423/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000424
Paul Bakker33b43f12013-08-20 11:48:36 +0200425/* BEGIN_CASE */
426void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000427{
428 rsa_context ctx;
429 memset( &ctx, 0x00, sizeof( rsa_context ) );
430
431 TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
432}
Paul Bakker33b43f12013-08-20 11:48:36 +0200433/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000434
Paul Bakker33b43f12013-08-20 11:48:36 +0200435/* BEGIN_CASE */
436void rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
437 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000438{
439 rsa_context ctx;
440
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000441 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000442
Paul Bakker33b43f12013-08-20 11:48:36 +0200443 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000444 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200445 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000446 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200447 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000448 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200449 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000450 }
451
Paul Bakker33b43f12013-08-20 11:48:36 +0200452 TEST_ASSERT( rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100453
454 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000455}
Paul Bakker33b43f12013-08-20 11:48:36 +0200456/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000457
Paul Bakker33b43f12013-08-20 11:48:36 +0200458/* BEGIN_CASE */
459void rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
460 char *input_Q, int radix_N, char *input_N,
461 int radix_E, char *input_E, int radix_D, char *input_D,
462 int radix_DP, char *input_DP, int radix_DQ,
463 char *input_DQ, int radix_QP, char *input_QP,
464 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000465{
466 rsa_context ctx;
467
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000468 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000469
Paul Bakker33b43f12013-08-20 11:48:36 +0200470 ctx.len = mod / 8;
471 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000472 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200473 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000474 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200475 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000476 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200477 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 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 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200487 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000488 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200489 TEST_ASSERT( mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000490 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200491 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000492 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200493 TEST_ASSERT( mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000494 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200495 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000496 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200497 TEST_ASSERT( mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000498 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200499 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000500 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200501 TEST_ASSERT( mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000502 }
Paul Bakker821fb082009-07-12 13:26:42 +0000503
Paul Bakker33b43f12013-08-20 11:48:36 +0200504 TEST_ASSERT( rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100505
506 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000507}
Paul Bakker33b43f12013-08-20 11:48:36 +0200508/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000509
Paul Bakker33b43f12013-08-20 11:48:36 +0200510/* BEGIN_CASE */
511void rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000512{
513 rsa_context ctx;
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000514 entropy_context entropy;
515 ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200516 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000517
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000518 entropy_init( &entropy );
519 TEST_ASSERT( ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200520 (const unsigned char *) pers, strlen( pers ) ) == 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000521
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000522 rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000523
Paul Bakker33b43f12013-08-20 11:48:36 +0200524 TEST_ASSERT( rsa_gen_key( &ctx, ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
525 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000526 {
527 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
528 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100529
530 rsa_free( &ctx );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200531 entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000532}
Paul Bakker33b43f12013-08-20 11:48:36 +0200533/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000534
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200535/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200536void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +0000537{
538 TEST_ASSERT( rsa_self_test( 0 ) == 0 );
539}
Paul Bakker33b43f12013-08-20 11:48:36 +0200540/* END_CASE */