blob: af26264d0aad75caab4aa443b3f696dac65e2f2a [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 Bakker42a29bf2009-07-07 20:18:41 +000031
Paul Bakker6c591fa2011-05-05 11:49:20 +000032 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakker33b43f12013-08-20 11:48:36 +020033 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000034
35 memset( message_str, 0x00, 1000 );
36 memset( hash_result, 0x00, 1000 );
37 memset( output, 0x00, 1000 );
38 memset( output_str, 0x00, 1000 );
39
Paul Bakker33b43f12013-08-20 11:48:36 +020040 ctx.len = mod / 8;
41 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
42 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
43 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
44 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000045
46 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
47 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
48 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
49 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
50 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
51 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
52 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
53 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
54
55 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
56
Paul Bakker33b43f12013-08-20 11:48:36 +020057 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +000058
Paul Bakker33b43f12013-08-20 11:48:36 +020059 if( md_info_from_type( digest ) != NULL )
60 TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000061
Paul Bakker33b43f12013-08-20 11:48:36 +020062 TEST_ASSERT( rsa_pkcs1_sign( &ctx, NULL, NULL, RSA_PRIVATE, digest, 0, hash_result, output ) == result );
63 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +000064 {
65 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +000066
Paul Bakker33b43f12013-08-20 11:48:36 +020067 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +000068 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000069
70 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010071 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +000072}
Paul Bakker33b43f12013-08-20 11:48:36 +020073/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +000074
Paul Bakker33b43f12013-08-20 11:48:36 +020075/* BEGIN_CASE */
76void rsa_pkcs1_verify( char *message_hex_string, int padding_mode, int digest,
77 int mod, int radix_N, char *input_N, int radix_E,
78 char *input_E, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000079{
80 unsigned char message_str[1000];
81 unsigned char hash_result[1000];
82 unsigned char result_str[1000];
83 rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000084 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +000085
Paul Bakker33b43f12013-08-20 11:48:36 +020086 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000087 memset( message_str, 0x00, 1000 );
88 memset( hash_result, 0x00, 1000 );
89 memset( result_str, 0x00, 1000 );
90
Paul Bakker33b43f12013-08-20 11:48:36 +020091 ctx.len = mod / 8;
92 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
93 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000094
95 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
96
Paul Bakker33b43f12013-08-20 11:48:36 +020097 msg_len = unhexify( message_str, message_hex_string );
98 unhexify( result_str, result_hex_str );
Paul Bakker42a29bf2009-07-07 20:18:41 +000099
Paul Bakker33b43f12013-08-20 11:48:36 +0200100 if( md_info_from_type( digest ) != NULL )
101 TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000102
Paul Bakker33b43f12013-08-20 11:48:36 +0200103 TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, digest, 0, hash_result, result_str ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100104
105 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000106}
Paul Bakker33b43f12013-08-20 11:48:36 +0200107/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000108
Paul Bakker821fb082009-07-12 13:26:42 +0000109
Paul Bakker33b43f12013-08-20 11:48:36 +0200110/* BEGIN_CASE */
111void rsa_pkcs1_sign_raw( char *message_hex_string, char *hash_result_string,
112 int padding_mode, int mod, int radix_P, char *input_P,
113 int radix_Q, char *input_Q, int radix_N,
114 char *input_N, int radix_E, char *input_E,
115 char *result_hex_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000116{
117 unsigned char message_str[1000];
118 unsigned char hash_result[1000];
119 unsigned char output[1000];
120 unsigned char output_str[1000];
121 rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000122 mpi P1, Q1, H, G;
Paul Bakkereaf90d92011-07-13 14:21:52 +0000123 int hash_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000124
Paul Bakker6c591fa2011-05-05 11:49:20 +0000125 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakker33b43f12013-08-20 11:48:36 +0200126 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000127
Paul Bakker42a29bf2009-07-07 20:18:41 +0000128 memset( message_str, 0x00, 1000 );
129 memset( hash_result, 0x00, 1000 );
130 memset( output, 0x00, 1000 );
131 memset( output_str, 0x00, 1000 );
132
Paul Bakker33b43f12013-08-20 11:48:36 +0200133 ctx.len = mod / 8;
134 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
135 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
136 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
137 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000138
139 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
140 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
141 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
142 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
143 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
144 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
145 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
146 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
147
148 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
149
Paul Bakker33b43f12013-08-20 11:48:36 +0200150 unhexify( message_str, message_hex_string );
151 hash_len = unhexify( hash_result, hash_result_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000152
Paul Bakkerc70b9822013-04-07 22:00:46 +0200153 TEST_ASSERT( rsa_pkcs1_sign( &ctx, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_NONE, hash_len, hash_result, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000154
155 hexify( output_str, output, ctx.len );
156
Paul Bakker33b43f12013-08-20 11:48:36 +0200157 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000158
159 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100160 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000161}
Paul Bakker33b43f12013-08-20 11:48:36 +0200162/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000163
Paul Bakker33b43f12013-08-20 11:48:36 +0200164/* BEGIN_CASE */
165void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string,
166 int padding_mode, int mod, int radix_N,
167 char *input_N, int radix_E, char *input_E,
168 char *result_hex_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000169{
170 unsigned char message_str[1000];
171 unsigned char hash_result[1000];
172 unsigned char result_str[1000];
173 rsa_context ctx;
Paul Bakkereaf90d92011-07-13 14:21:52 +0000174 size_t hash_len;
Paul Bakker821fb082009-07-12 13:26:42 +0000175
Paul Bakker33b43f12013-08-20 11:48:36 +0200176 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000177 memset( message_str, 0x00, 1000 );
178 memset( hash_result, 0x00, 1000 );
179 memset( result_str, 0x00, 1000 );
180
Paul Bakker33b43f12013-08-20 11:48:36 +0200181 ctx.len = mod / 8;
182 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
183 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000184
185 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
186
Paul Bakker33b43f12013-08-20 11:48:36 +0200187 unhexify( message_str, message_hex_string );
188 hash_len = unhexify( hash_result, hash_result_string );
189 unhexify( result_str, result_hex_str );
Paul Bakker821fb082009-07-12 13:26:42 +0000190
Paul Bakker33b43f12013-08-20 11:48:36 +0200191 TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, POLARSSL_MD_NONE, hash_len, hash_result, result_str ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100192
193 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000194}
Paul Bakker33b43f12013-08-20 11:48:36 +0200195/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000196
Paul Bakker33b43f12013-08-20 11:48:36 +0200197/* BEGIN_CASE */
198void rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod,
199 int radix_N, char *input_N, int radix_E, char *input_E,
200 char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000201{
202 unsigned char message_str[1000];
203 unsigned char output[1000];
204 unsigned char output_str[1000];
205 rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000206 size_t msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000207 rnd_pseudo_info rnd_info;
208
209 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000210
Paul Bakker33b43f12013-08-20 11:48:36 +0200211 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000212 memset( message_str, 0x00, 1000 );
213 memset( output, 0x00, 1000 );
214 memset( output_str, 0x00, 1000 );
215
Paul Bakker33b43f12013-08-20 11:48:36 +0200216 ctx.len = mod / 8;
217 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
218 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000219
220 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
221
Paul Bakker33b43f12013-08-20 11:48:36 +0200222 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000223
Paul Bakker33b43f12013-08-20 11:48:36 +0200224 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PUBLIC, msg_len, message_str, output ) == result );
225 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000226 {
227 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000228
Paul Bakker33b43f12013-08-20 11:48:36 +0200229 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000230 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100231
232 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000233}
Paul Bakker33b43f12013-08-20 11:48:36 +0200234/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000235
Paul Bakker33b43f12013-08-20 11:48:36 +0200236/* BEGIN_CASE */
237void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode,
238 int mod, int radix_N, char *input_N,
239 int radix_E, char *input_E,
240 char *result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000241{
242 unsigned char message_str[1000];
243 unsigned char output[1000];
244 unsigned char output_str[1000];
245 rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000246 size_t msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000247
Paul Bakker33b43f12013-08-20 11:48:36 +0200248 rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000249 memset( message_str, 0x00, 1000 );
250 memset( output, 0x00, 1000 );
251 memset( output_str, 0x00, 1000 );
252
Paul Bakker33b43f12013-08-20 11:48:36 +0200253 ctx.len = mod / 8;
254 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
255 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000256
257 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
258
Paul Bakker33b43f12013-08-20 11:48:36 +0200259 msg_len = unhexify( message_str, message_hex_string );
Paul Bakkera6656852010-07-18 19:47:14 +0000260
Paul Bakker33b43f12013-08-20 11:48:36 +0200261 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL, RSA_PUBLIC, msg_len, message_str, output ) == result );
262 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000263 {
264 hexify( output_str, output, ctx.len );
265
Paul Bakker33b43f12013-08-20 11:48:36 +0200266 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000267 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100268
269 rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000270}
Paul Bakker33b43f12013-08-20 11:48:36 +0200271/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000272
Paul Bakker33b43f12013-08-20 11:48:36 +0200273/* BEGIN_CASE */
274void rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod,
275 int radix_P, char *input_P, int radix_Q, char *input_Q,
276 int radix_N, char *input_N, int radix_E, char *input_E,
277 int max_output, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000278{
279 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000280 unsigned char output[1000];
281 unsigned char output_str[1000];
282 rsa_context ctx;
283 mpi P1, Q1, H, G;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000284 size_t output_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000285
Paul Bakker6c591fa2011-05-05 11:49:20 +0000286 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakker33b43f12013-08-20 11:48:36 +0200287 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000288
289 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000290 memset( output, 0x00, 1000 );
291 memset( output_str, 0x00, 1000 );
292
Paul Bakker33b43f12013-08-20 11:48:36 +0200293 ctx.len = mod / 8;
294 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
295 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
296 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
297 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000298
299 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
300 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
301 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
302 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
303 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
304 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
305 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
306 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
307
308 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
309
Paul Bakker33b43f12013-08-20 11:48:36 +0200310 unhexify( message_str, message_hex_string );
Paul Bakker69998dd2009-07-11 19:15:20 +0000311 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000312
Paul Bakker33b43f12013-08-20 11:48:36 +0200313 TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, max_output ) == result );
314 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000315 {
316 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000317
Paul Bakker33b43f12013-08-20 11:48:36 +0200318 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000319 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000320
321 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100322 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000323}
Paul Bakker33b43f12013-08-20 11:48:36 +0200324/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000325
Paul Bakker33b43f12013-08-20 11:48:36 +0200326/* BEGIN_CASE */
327void rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N,
328 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000329{
330 unsigned char message_str[1000];
331 unsigned char output[1000];
332 unsigned char output_str[1000];
333 rsa_context ctx;
334
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000335 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000336 memset( message_str, 0x00, 1000 );
337 memset( output, 0x00, 1000 );
338 memset( output_str, 0x00, 1000 );
339
Paul Bakker33b43f12013-08-20 11:48:36 +0200340 ctx.len = mod / 8;
341 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
342 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000343
344 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
345
Paul Bakker33b43f12013-08-20 11:48:36 +0200346 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000347
Paul Bakker33b43f12013-08-20 11:48:36 +0200348 TEST_ASSERT( rsa_public( &ctx, message_str, output ) == result );
349 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000350 {
351 hexify( output_str, output, ctx.len );
352
Paul Bakker33b43f12013-08-20 11:48:36 +0200353 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000354 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100355
356 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000357}
Paul Bakker33b43f12013-08-20 11:48:36 +0200358/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000359
Paul Bakker33b43f12013-08-20 11:48:36 +0200360/* BEGIN_CASE */
361void rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
362 int radix_Q, char *input_Q, int radix_N, char *input_N,
363 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000364{
365 unsigned char message_str[1000];
366 unsigned char output[1000];
367 unsigned char output_str[1000];
368 rsa_context ctx;
369 mpi P1, Q1, H, G;
370
Paul Bakker6c591fa2011-05-05 11:49:20 +0000371 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000372 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000373
374 memset( message_str, 0x00, 1000 );
375 memset( output, 0x00, 1000 );
376 memset( output_str, 0x00, 1000 );
377
Paul Bakker33b43f12013-08-20 11:48:36 +0200378 ctx.len = mod / 8;
379 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
380 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
381 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
382 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000383
384 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
385 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
386 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
387 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
388 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
389 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
390 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
391 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
392
393 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
394
Paul Bakker33b43f12013-08-20 11:48:36 +0200395 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000396
Paul Bakker33b43f12013-08-20 11:48:36 +0200397 TEST_ASSERT( rsa_private( &ctx, message_str, output ) == result );
398 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000399 {
400 hexify( output_str, output, ctx.len );
401
Paul Bakker33b43f12013-08-20 11:48:36 +0200402 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000403 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000404
405 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100406 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000407}
Paul Bakker33b43f12013-08-20 11:48:36 +0200408/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000409
Paul Bakker33b43f12013-08-20 11:48:36 +0200410/* BEGIN_CASE */
411void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000412{
413 rsa_context ctx;
414 memset( &ctx, 0x00, sizeof( rsa_context ) );
415
416 TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
417}
Paul Bakker33b43f12013-08-20 11:48:36 +0200418/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000419
Paul Bakker33b43f12013-08-20 11:48:36 +0200420/* BEGIN_CASE */
421void rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
422 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000423{
424 rsa_context ctx;
425
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000426 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000427
Paul Bakker33b43f12013-08-20 11:48:36 +0200428 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000429 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200430 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000431 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200432 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000433 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200434 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000435 }
436
Paul Bakker33b43f12013-08-20 11:48:36 +0200437 TEST_ASSERT( rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100438
439 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000440}
Paul Bakker33b43f12013-08-20 11:48:36 +0200441/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000442
Paul Bakker33b43f12013-08-20 11:48:36 +0200443/* BEGIN_CASE */
444void rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
445 char *input_Q, int radix_N, char *input_N,
446 int radix_E, char *input_E, int radix_D, char *input_D,
447 int radix_DP, char *input_DP, int radix_DQ,
448 char *input_DQ, int radix_QP, char *input_QP,
449 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000450{
451 rsa_context ctx;
452
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000453 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000454
Paul Bakker33b43f12013-08-20 11:48:36 +0200455 ctx.len = mod / 8;
456 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000457 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200458 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000459 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200460 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000461 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200462 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000463 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200464 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000465 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200466 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000467 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200468 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000469 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200470 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000471 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200472 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000473 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200474 TEST_ASSERT( mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000475 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200476 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000477 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200478 TEST_ASSERT( mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000479 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200480 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000481 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200482 TEST_ASSERT( mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000483 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200484 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000485 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200486 TEST_ASSERT( mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000487 }
Paul Bakker821fb082009-07-12 13:26:42 +0000488
Paul Bakker33b43f12013-08-20 11:48:36 +0200489 TEST_ASSERT( rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100490
491 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000492}
Paul Bakker33b43f12013-08-20 11:48:36 +0200493/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000494
Paul Bakker33b43f12013-08-20 11:48:36 +0200495/* BEGIN_CASE */
496void rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000497{
498 rsa_context ctx;
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000499 entropy_context entropy;
500 ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200501 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000502
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000503 entropy_init( &entropy );
504 TEST_ASSERT( ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200505 (const unsigned char *) pers, strlen( pers ) ) == 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000506
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000507 rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000508
Paul Bakker33b43f12013-08-20 11:48:36 +0200509 TEST_ASSERT( rsa_gen_key( &ctx, ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
510 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000511 {
512 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
513 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100514
515 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000516}
Paul Bakker33b43f12013-08-20 11:48:36 +0200517/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000518
Paul Bakker33b43f12013-08-20 11:48:36 +0200519/* BEGIN_CASE */
520void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +0000521{
522 TEST_ASSERT( rsa_self_test( 0 ) == 0 );
523}
Paul Bakker33b43f12013-08-20 11:48:36 +0200524/* END_CASE */