blob: 2ac03b8f79b946204469eb70757710cf1e92a44b [file] [log] [blame]
Paul Bakker42a29bf2009-07-07 20:18:41 +00001BEGIN_HEADER
2#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>
7#include <polarssl/sha2.h>
8#include <polarssl/sha4.h>
Paul Bakker821fb082009-07-12 13:26:42 +00009#include <polarssl/havege.h>
Paul Bakker545570e2010-07-18 09:00:25 +000010
Paul Bakkera6656852010-07-18 19:47:14 +000011static int badrand( void *rng_state )
12{
13 if( rng_state != NULL )
14 rng_state = NULL;
15
16 return( 0 );
17}
Paul Bakker42a29bf2009-07-07 20:18:41 +000018END_HEADER
19
20BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +000021rsa_pkcs1_sign:message_hex_string:padding_mode:digest:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str:result
Paul Bakker42a29bf2009-07-07 20:18:41 +000022{
23 unsigned char message_str[1000];
24 unsigned char hash_result[1000];
25 unsigned char output[1000];
26 unsigned char output_str[1000];
27 rsa_context ctx;
28 mpi P1, Q1, H, G;
Paul Bakker69998dd2009-07-11 19:15:20 +000029 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +000030
Paul Bakker6c591fa2011-05-05 11:49:20 +000031 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakkerebcef6d2010-08-16 11:10:49 +000032 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000033
34 memset( message_str, 0x00, 1000 );
35 memset( hash_result, 0x00, 1000 );
36 memset( output, 0x00, 1000 );
37 memset( output_str, 0x00, 1000 );
38
39 ctx.len = {mod} / 8;
40 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
41 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
42 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
43 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
44
45 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
46 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
47 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
48 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
49 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
50 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
51 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
52 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
53
54 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
55
Paul Bakker69998dd2009-07-11 19:15:20 +000056 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +000057
Paul Bakker76791f72009-10-03 20:02:00 +000058 switch( {digest} )
59 {
60#ifdef POLARSSL_MD2_C
61 case SIG_RSA_MD2:
Paul Bakker821fb082009-07-12 13:26:42 +000062 md2( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +000063 break;
64#endif
65#ifdef POLARSSL_MD4_C
66 case SIG_RSA_MD4:
Paul Bakker821fb082009-07-12 13:26:42 +000067 md4( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +000068 break;
69#endif
70#ifdef POLARSSL_MD5_C
71 case SIG_RSA_MD5:
Paul Bakker821fb082009-07-12 13:26:42 +000072 md5( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +000073 break;
74#endif
75#ifdef POLARSSL_SHA1_C
76 case SIG_RSA_SHA1:
Paul Bakker42a29bf2009-07-07 20:18:41 +000077 sha1( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +000078 break;
79#endif
80#ifdef POLARSSL_SHA2_C
81 case SIG_RSA_SHA224:
Paul Bakker42a29bf2009-07-07 20:18:41 +000082 sha2( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +000083 break;
84 case SIG_RSA_SHA256:
Paul Bakker42a29bf2009-07-07 20:18:41 +000085 sha2( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +000086 break;
87#endif
88#ifdef POLARSSL_SHA4_C
89 case SIG_RSA_SHA384:
Paul Bakker42a29bf2009-07-07 20:18:41 +000090 sha4( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +000091 break;
92 case SIG_RSA_SHA512:
Paul Bakker42a29bf2009-07-07 20:18:41 +000093 sha4( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +000094 break;
95#endif
96 }
Paul Bakker42a29bf2009-07-07 20:18:41 +000097
Paul Bakker9dcc3222011-03-08 14:16:06 +000098 TEST_ASSERT( rsa_pkcs1_sign( &ctx, NULL, NULL, RSA_PRIVATE, {digest}, 0, hash_result, output ) == {result} );
Paul Bakker821fb082009-07-12 13:26:42 +000099 if( {result} == 0 )
100 {
101 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000102
Paul Bakker821fb082009-07-12 13:26:42 +0000103 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
104 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000105
106 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000107}
108END_CASE
109
110BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000111rsa_pkcs1_verify:message_hex_string:padding_mode:digest:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
Paul Bakker42a29bf2009-07-07 20:18:41 +0000112{
113 unsigned char message_str[1000];
114 unsigned char hash_result[1000];
115 unsigned char result_str[1000];
116 rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000117 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000118
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000119 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000120 memset( message_str, 0x00, 1000 );
121 memset( hash_result, 0x00, 1000 );
122 memset( result_str, 0x00, 1000 );
123
124 ctx.len = {mod} / 8;
125 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
126 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
127
128 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
129
Paul Bakker69998dd2009-07-11 19:15:20 +0000130 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000131 unhexify( result_str, {result_hex_str} );
132
Paul Bakker76791f72009-10-03 20:02:00 +0000133 switch( {digest} )
134 {
135#ifdef POLARSSL_MD2_C
136 case SIG_RSA_MD2:
Paul Bakker821fb082009-07-12 13:26:42 +0000137 md2( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000138 break;
139#endif
140#ifdef POLARSSL_MD4_C
141 case SIG_RSA_MD4:
Paul Bakker821fb082009-07-12 13:26:42 +0000142 md4( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000143 break;
144#endif
145#ifdef POLARSSL_MD5_C
146 case SIG_RSA_MD5:
Paul Bakker821fb082009-07-12 13:26:42 +0000147 md5( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000148 break;
149#endif
150#ifdef POLARSSL_SHA1_C
151 case SIG_RSA_SHA1:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000152 sha1( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000153 break;
154#endif
155#ifdef POLARSSL_SHA2_C
156 case SIG_RSA_SHA224:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000157 sha2( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +0000158 break;
159 case SIG_RSA_SHA256:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000160 sha2( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +0000161 break;
162#endif
163#ifdef POLARSSL_SHA4_C
164 case SIG_RSA_SHA384:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000165 sha4( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +0000166 break;
167 case SIG_RSA_SHA512:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000168 sha4( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +0000169 break;
170#endif
171 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000172
Paul Bakker821fb082009-07-12 13:26:42 +0000173 TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, {digest}, 0, hash_result, result_str ) == {result} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000174}
175END_CASE
176
Paul Bakker821fb082009-07-12 13:26:42 +0000177
Paul Bakker42a29bf2009-07-07 20:18:41 +0000178BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000179rsa_pkcs1_sign_raw:message_hex_string:hash_result_string:padding_mode:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str
Paul Bakker42a29bf2009-07-07 20:18:41 +0000180{
181 unsigned char message_str[1000];
182 unsigned char hash_result[1000];
183 unsigned char output[1000];
184 unsigned char output_str[1000];
185 rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000186 mpi P1, Q1, H, G;
187 int msg_len, hash_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000188
Paul Bakker6c591fa2011-05-05 11:49:20 +0000189 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000190 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000191
Paul Bakker42a29bf2009-07-07 20:18:41 +0000192 memset( message_str, 0x00, 1000 );
193 memset( hash_result, 0x00, 1000 );
194 memset( output, 0x00, 1000 );
195 memset( output_str, 0x00, 1000 );
196
197 ctx.len = {mod} / 8;
Paul Bakker821fb082009-07-12 13:26:42 +0000198 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
199 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
200 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
201 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
202
203 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
204 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
205 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
206 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
207 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
208 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
209 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
210 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
211
212 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
213
214 msg_len = unhexify( message_str, {message_hex_string} );
215 hash_len = unhexify( hash_result, {hash_result_string} );
216
Paul Bakker9dcc3222011-03-08 14:16:06 +0000217 TEST_ASSERT( rsa_pkcs1_sign( &ctx, NULL, NULL, RSA_PRIVATE, SIG_RSA_RAW, hash_len, hash_result, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000218
219 hexify( output_str, output, ctx.len );
220
221 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000222
223 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker821fb082009-07-12 13:26:42 +0000224}
225END_CASE
226
227BEGIN_CASE
228rsa_pkcs1_verify_raw:message_hex_string:hash_result_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:correct
229{
230 unsigned char message_str[1000];
231 unsigned char hash_result[1000];
232 unsigned char result_str[1000];
233 rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000234 size_t msg_len, hash_len;
Paul Bakker821fb082009-07-12 13:26:42 +0000235
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000236 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000237 memset( message_str, 0x00, 1000 );
238 memset( hash_result, 0x00, 1000 );
239 memset( result_str, 0x00, 1000 );
240
241 ctx.len = {mod} / 8;
242 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
243 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
244
245 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
246
247 msg_len = unhexify( message_str, {message_hex_string} );
248 hash_len = unhexify( hash_result, {hash_result_string} );
249 unhexify( result_str, {result_hex_str} );
250
Paul Bakkerfc22c442009-07-19 20:36:27 +0000251 TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, SIG_RSA_RAW, hash_len, hash_result, result_str ) == {correct} );
Paul Bakker821fb082009-07-12 13:26:42 +0000252}
253END_CASE
254
255BEGIN_CASE
256rsa_pkcs1_encrypt:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
257{
258 unsigned char message_str[1000];
259 unsigned char output[1000];
260 unsigned char output_str[1000];
261 rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000262 size_t msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000263 rnd_pseudo_info rnd_info;
264
265 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000266
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000267 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000268 memset( message_str, 0x00, 1000 );
269 memset( output, 0x00, 1000 );
270 memset( output_str, 0x00, 1000 );
271
272 ctx.len = {mod} / 8;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000273 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
274 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
275
276 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
277
Paul Bakker69998dd2009-07-11 19:15:20 +0000278 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000279
Paul Bakker997bbd12011-03-13 15:45:42 +0000280 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PUBLIC, msg_len, message_str, output ) == {result} );
Paul Bakker821fb082009-07-12 13:26:42 +0000281 if( {result} == 0 )
282 {
283 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000284
Paul Bakker821fb082009-07-12 13:26:42 +0000285 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
286 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000287}
288END_CASE
289
290BEGIN_CASE
Paul Bakkera6656852010-07-18 19:47:14 +0000291rsa_pkcs1_encrypt_bad_rng:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
292{
293 unsigned char message_str[1000];
294 unsigned char output[1000];
295 unsigned char output_str[1000];
296 rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000297 size_t msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000298
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000299 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000300 memset( message_str, 0x00, 1000 );
301 memset( output, 0x00, 1000 );
302 memset( output_str, 0x00, 1000 );
303
304 ctx.len = {mod} / 8;
305 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
306 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
307
308 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
309
310 msg_len = unhexify( message_str, {message_hex_string} );
311
Paul Bakker997bbd12011-03-13 15:45:42 +0000312 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL, RSA_PUBLIC, msg_len, message_str, output ) == {result} );
Paul Bakkera6656852010-07-18 19:47:14 +0000313 if( {result} == 0 )
314 {
315 hexify( output_str, output, ctx.len );
316
317 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
318 }
319}
320END_CASE
321
322BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000323rsa_pkcs1_decrypt:message_hex_string:padding_mode:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:max_output:result_hex_str:result
Paul Bakker42a29bf2009-07-07 20:18:41 +0000324{
325 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000326 unsigned char output[1000];
327 unsigned char output_str[1000];
328 rsa_context ctx;
329 mpi P1, Q1, H, G;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000330 size_t output_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000331
Paul Bakker6c591fa2011-05-05 11:49:20 +0000332 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000333 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000334
335 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000336 memset( output, 0x00, 1000 );
337 memset( output_str, 0x00, 1000 );
338
339 ctx.len = {mod} / 8;
340 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
341 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
342 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
343 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
344
345 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
346 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
347 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
348 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
349 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
350 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
351 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
352 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
353
354 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
355
356 unhexify( message_str, {message_hex_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000357 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000358
Paul Bakker821fb082009-07-12 13:26:42 +0000359 TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, {max_output} ) == {result} );
360 if( {result} == 0 )
361 {
362 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000363
Paul Bakker821fb082009-07-12 13:26:42 +0000364 TEST_ASSERT( strncasecmp( (char *) output_str, {result_hex_str}, strlen( {result_hex_str} ) ) == 0 );
365 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000366
367 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker821fb082009-07-12 13:26:42 +0000368}
369END_CASE
Paul Bakker42a29bf2009-07-07 20:18:41 +0000370
Paul Bakker821fb082009-07-12 13:26:42 +0000371BEGIN_CASE
372rsa_public:message_hex_string:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
373{
374 unsigned char message_str[1000];
375 unsigned char output[1000];
376 unsigned char output_str[1000];
377 rsa_context ctx;
378
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000379 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000380 memset( message_str, 0x00, 1000 );
381 memset( output, 0x00, 1000 );
382 memset( output_str, 0x00, 1000 );
383
384 ctx.len = {mod} / 8;
385 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
386 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
387
388 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
389
390 unhexify( message_str, {message_hex_string} );
391
392 TEST_ASSERT( rsa_public( &ctx, message_str, output ) == {result} );
393 if( {result} == 0 )
394 {
395 hexify( output_str, output, ctx.len );
396
397 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
398 }
399}
400END_CASE
401
402BEGIN_CASE
403rsa_private:message_hex_string:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str:result
404{
405 unsigned char message_str[1000];
406 unsigned char output[1000];
407 unsigned char output_str[1000];
408 rsa_context ctx;
409 mpi P1, Q1, H, G;
410
Paul Bakker6c591fa2011-05-05 11:49:20 +0000411 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000412 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000413
414 memset( message_str, 0x00, 1000 );
415 memset( output, 0x00, 1000 );
416 memset( output_str, 0x00, 1000 );
417
418 ctx.len = {mod} / 8;
419 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
420 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
421 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
422 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
423
424 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
425 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
426 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
427 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
428 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
429 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
430 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
431 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
432
433 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
434
435 unhexify( message_str, {message_hex_string} );
436
437 TEST_ASSERT( rsa_private( &ctx, message_str, output ) == {result} );
438 if( {result} == 0 )
439 {
440 hexify( output_str, output, ctx.len );
441
442 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
443 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000444
445 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000446}
447END_CASE
448
449BEGIN_CASE
Paul Bakker37940d9f2009-07-10 22:38:58 +0000450rsa_check_privkey_null:
451{
452 rsa_context ctx;
453 memset( &ctx, 0x00, sizeof( rsa_context ) );
454
455 TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
456}
457END_CASE
458
459BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000460rsa_check_pubkey:radix_N:input_N:radix_E:input_E:result
461{
462 rsa_context ctx;
463
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000464 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000465
466 if( strlen( {input_N} ) )
467 {
468 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
469 }
470 if( strlen( {input_E} ) )
471 {
472 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
473 }
474
475 TEST_ASSERT( rsa_check_pubkey( &ctx ) == {result} );
476}
477END_CASE
478
479BEGIN_CASE
480rsa_check_privkey:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:radix_D:input_D:result
481{
482 rsa_context ctx;
483
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000484 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000485
486 ctx.len = {mod} / 8;
487 if( strlen( {input_P} ) )
488 {
489 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
490 }
491 if( strlen( {input_Q} ) )
492 {
493 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
494 }
495 if( strlen( {input_N} ) )
496 {
497 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
498 }
499 if( strlen( {input_E} ) )
500 {
501 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
502 }
503 if( strlen( {input_D} ) )
504 {
505 TEST_ASSERT( mpi_read_string( &ctx.D, {radix_D}, {input_D} ) == 0 );
506 }
507
508 TEST_ASSERT( rsa_check_privkey( &ctx ) == {result} );
509}
510END_CASE
511
512BEGIN_CASE
513rsa_gen_key:nrbits:exponent:result
514{
515 rsa_context ctx;
516 havege_state hs;
517
518 havege_init( &hs );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000519 rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000520
Paul Bakkera802e1a2010-08-16 11:56:45 +0000521 TEST_ASSERT( rsa_gen_key( &ctx, havege_rand, &hs, {nrbits}, {exponent} ) == {result} );
Paul Bakker821fb082009-07-12 13:26:42 +0000522 if( {result} == 0 )
523 {
524 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
525 }
526}
527END_CASE
528
529BEGIN_CASE
Paul Bakker42a29bf2009-07-07 20:18:41 +0000530rsa_selftest:
531{
532 TEST_ASSERT( rsa_self_test( 0 ) == 0 );
533}
534END_CASE