blob: 4b942748fa8848099d6206a9514cba375e11924a [file] [log] [blame]
Paul Bakker42a29bf2009-07-07 20:18:41 +00001BEGIN_HEADER
Paul Bakker76791f72009-10-03 20:02:00 +00002#include <polarssl/config.h>
Paul Bakker42a29bf2009-07-07 20:18:41 +00003#include <polarssl/rsa.h>
Paul Bakker821fb082009-07-12 13:26:42 +00004#include <polarssl/md2.h>
5#include <polarssl/md4.h>
6#include <polarssl/md5.h>
Paul Bakker42a29bf2009-07-07 20:18:41 +00007#include <polarssl/sha1.h>
8#include <polarssl/sha2.h>
9#include <polarssl/sha4.h>
Paul Bakker821fb082009-07-12 13:26:42 +000010#include <polarssl/havege.h>
Paul Bakker545570e2010-07-18 09:00:25 +000011
12static int myrand( void *rng_state )
13{
14 if( rng_state != NULL )
15 rng_state = NULL;
16
17 return( rand() );
18}
Paul Bakkera6656852010-07-18 19:47:14 +000019
20static int badrand( void *rng_state )
21{
22 if( rng_state != NULL )
23 rng_state = NULL;
24
25 return( 0 );
26}
Paul Bakker42a29bf2009-07-07 20:18:41 +000027END_HEADER
28
29BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +000030rsa_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 +000031{
32 unsigned char message_str[1000];
33 unsigned char hash_result[1000];
34 unsigned char output[1000];
35 unsigned char output_str[1000];
36 rsa_context ctx;
37 mpi P1, Q1, H, G;
Paul Bakker69998dd2009-07-11 19:15:20 +000038 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +000039
40 mpi_init( &P1, &Q1, &H, &G, NULL );
Paul Bakkerebcef6d2010-08-16 11:10:49 +000041 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000042
43 memset( message_str, 0x00, 1000 );
44 memset( hash_result, 0x00, 1000 );
45 memset( output, 0x00, 1000 );
46 memset( output_str, 0x00, 1000 );
47
48 ctx.len = {mod} / 8;
49 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
50 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
51 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
52 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
53
54 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
55 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
56 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
57 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
58 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
59 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
60 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
61 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
62
63 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
64
Paul Bakker69998dd2009-07-11 19:15:20 +000065 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +000066
Paul Bakker76791f72009-10-03 20:02:00 +000067 switch( {digest} )
68 {
69#ifdef POLARSSL_MD2_C
70 case SIG_RSA_MD2:
Paul Bakker821fb082009-07-12 13:26:42 +000071 md2( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +000072 break;
73#endif
74#ifdef POLARSSL_MD4_C
75 case SIG_RSA_MD4:
Paul Bakker821fb082009-07-12 13:26:42 +000076 md4( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +000077 break;
78#endif
79#ifdef POLARSSL_MD5_C
80 case SIG_RSA_MD5:
Paul Bakker821fb082009-07-12 13:26:42 +000081 md5( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +000082 break;
83#endif
84#ifdef POLARSSL_SHA1_C
85 case SIG_RSA_SHA1:
Paul Bakker42a29bf2009-07-07 20:18:41 +000086 sha1( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +000087 break;
88#endif
89#ifdef POLARSSL_SHA2_C
90 case SIG_RSA_SHA224:
Paul Bakker42a29bf2009-07-07 20:18:41 +000091 sha2( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +000092 break;
93 case SIG_RSA_SHA256:
Paul Bakker42a29bf2009-07-07 20:18:41 +000094 sha2( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +000095 break;
96#endif
97#ifdef POLARSSL_SHA4_C
98 case SIG_RSA_SHA384:
Paul Bakker42a29bf2009-07-07 20:18:41 +000099 sha4( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +0000100 break;
101 case SIG_RSA_SHA512:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000102 sha4( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +0000103 break;
104#endif
105 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000106
Paul Bakker821fb082009-07-12 13:26:42 +0000107 TEST_ASSERT( rsa_pkcs1_sign( &ctx, RSA_PRIVATE, {digest}, 0, hash_result, output ) == {result} );
108 if( {result} == 0 )
109 {
110 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000111
Paul Bakker821fb082009-07-12 13:26:42 +0000112 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
113 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000114}
115END_CASE
116
117BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000118rsa_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 +0000119{
120 unsigned char message_str[1000];
121 unsigned char hash_result[1000];
122 unsigned char result_str[1000];
123 rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000124 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000125
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000126 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000127 memset( message_str, 0x00, 1000 );
128 memset( hash_result, 0x00, 1000 );
129 memset( result_str, 0x00, 1000 );
130
131 ctx.len = {mod} / 8;
132 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
133 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
134
135 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
136
Paul Bakker69998dd2009-07-11 19:15:20 +0000137 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000138 unhexify( result_str, {result_hex_str} );
139
Paul Bakker76791f72009-10-03 20:02:00 +0000140 switch( {digest} )
141 {
142#ifdef POLARSSL_MD2_C
143 case SIG_RSA_MD2:
Paul Bakker821fb082009-07-12 13:26:42 +0000144 md2( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000145 break;
146#endif
147#ifdef POLARSSL_MD4_C
148 case SIG_RSA_MD4:
Paul Bakker821fb082009-07-12 13:26:42 +0000149 md4( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000150 break;
151#endif
152#ifdef POLARSSL_MD5_C
153 case SIG_RSA_MD5:
Paul Bakker821fb082009-07-12 13:26:42 +0000154 md5( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000155 break;
156#endif
157#ifdef POLARSSL_SHA1_C
158 case SIG_RSA_SHA1:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000159 sha1( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000160 break;
161#endif
162#ifdef POLARSSL_SHA2_C
163 case SIG_RSA_SHA224:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000164 sha2( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +0000165 break;
166 case SIG_RSA_SHA256:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000167 sha2( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +0000168 break;
169#endif
170#ifdef POLARSSL_SHA4_C
171 case SIG_RSA_SHA384:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000172 sha4( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +0000173 break;
174 case SIG_RSA_SHA512:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000175 sha4( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +0000176 break;
177#endif
178 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000179
Paul Bakker821fb082009-07-12 13:26:42 +0000180 TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, {digest}, 0, hash_result, result_str ) == {result} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000181}
182END_CASE
183
Paul Bakker821fb082009-07-12 13:26:42 +0000184
Paul Bakker42a29bf2009-07-07 20:18:41 +0000185BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000186rsa_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 +0000187{
188 unsigned char message_str[1000];
189 unsigned char hash_result[1000];
190 unsigned char output[1000];
191 unsigned char output_str[1000];
192 rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000193 mpi P1, Q1, H, G;
194 int msg_len, hash_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000195
Paul Bakker821fb082009-07-12 13:26:42 +0000196 mpi_init( &P1, &Q1, &H, &G, NULL );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000197 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000198
Paul Bakker42a29bf2009-07-07 20:18:41 +0000199 memset( message_str, 0x00, 1000 );
200 memset( hash_result, 0x00, 1000 );
201 memset( output, 0x00, 1000 );
202 memset( output_str, 0x00, 1000 );
203
204 ctx.len = {mod} / 8;
Paul Bakker821fb082009-07-12 13:26:42 +0000205 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
206 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
207 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
208 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
209
210 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
211 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
212 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
213 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
214 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
215 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
216 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
217 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
218
219 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
220
221 msg_len = unhexify( message_str, {message_hex_string} );
222 hash_len = unhexify( hash_result, {hash_result_string} );
223
Paul Bakkerfc22c442009-07-19 20:36:27 +0000224 TEST_ASSERT( rsa_pkcs1_sign( &ctx, RSA_PRIVATE, SIG_RSA_RAW, hash_len, hash_result, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000225
226 hexify( output_str, output, ctx.len );
227
228 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
229}
230END_CASE
231
232BEGIN_CASE
233rsa_pkcs1_verify_raw:message_hex_string:hash_result_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:correct
234{
235 unsigned char message_str[1000];
236 unsigned char hash_result[1000];
237 unsigned char result_str[1000];
238 rsa_context ctx;
239 int msg_len, hash_len;
240
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000241 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000242 memset( message_str, 0x00, 1000 );
243 memset( hash_result, 0x00, 1000 );
244 memset( result_str, 0x00, 1000 );
245
246 ctx.len = {mod} / 8;
247 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
248 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
249
250 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
251
252 msg_len = unhexify( message_str, {message_hex_string} );
253 hash_len = unhexify( hash_result, {hash_result_string} );
254 unhexify( result_str, {result_hex_str} );
255
Paul Bakkerfc22c442009-07-19 20:36:27 +0000256 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 +0000257}
258END_CASE
259
260BEGIN_CASE
261rsa_pkcs1_encrypt:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
262{
263 unsigned char message_str[1000];
264 unsigned char output[1000];
265 unsigned char output_str[1000];
266 rsa_context ctx;
267 int msg_len;
268
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000269 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000270 memset( message_str, 0x00, 1000 );
271 memset( output, 0x00, 1000 );
272 memset( output_str, 0x00, 1000 );
273
274 ctx.len = {mod} / 8;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000275 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
276 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
277
278 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
279
Paul Bakker69998dd2009-07-11 19:15:20 +0000280 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000281
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000282 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &myrand, NULL, RSA_PUBLIC, msg_len, message_str, output ) == {result} );
Paul Bakker821fb082009-07-12 13:26:42 +0000283 if( {result} == 0 )
284 {
285 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000286
Paul Bakker821fb082009-07-12 13:26:42 +0000287 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
288 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000289}
290END_CASE
291
292BEGIN_CASE
Paul Bakkera6656852010-07-18 19:47:14 +0000293rsa_pkcs1_encrypt_bad_rng:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
294{
295 unsigned char message_str[1000];
296 unsigned char output[1000];
297 unsigned char output_str[1000];
298 rsa_context ctx;
299 int msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000300
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000301 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000302 memset( message_str, 0x00, 1000 );
303 memset( output, 0x00, 1000 );
304 memset( output_str, 0x00, 1000 );
305
306 ctx.len = {mod} / 8;
307 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
308 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
309
310 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
311
312 msg_len = unhexify( message_str, {message_hex_string} );
313
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000314 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &badrand, NULL, RSA_PUBLIC, msg_len, message_str, output ) == {result} );
Paul Bakkera6656852010-07-18 19:47:14 +0000315 if( {result} == 0 )
316 {
317 hexify( output_str, output, ctx.len );
318
319 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
320 }
321}
322END_CASE
323
324BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000325rsa_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 +0000326{
327 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000328 unsigned char output[1000];
329 unsigned char output_str[1000];
330 rsa_context ctx;
331 mpi P1, Q1, H, G;
Paul Bakker69998dd2009-07-11 19:15:20 +0000332 int output_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000333
334 mpi_init( &P1, &Q1, &H, &G, NULL );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000335 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000336
337 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000338 memset( output, 0x00, 1000 );
339 memset( output_str, 0x00, 1000 );
340
341 ctx.len = {mod} / 8;
342 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
343 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
344 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
345 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
346
347 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
348 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
349 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
350 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
351 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
352 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
353 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
354 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
355
356 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
357
358 unhexify( message_str, {message_hex_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000359 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000360
Paul Bakker821fb082009-07-12 13:26:42 +0000361 TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, {max_output} ) == {result} );
362 if( {result} == 0 )
363 {
364 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000365
Paul Bakker821fb082009-07-12 13:26:42 +0000366 TEST_ASSERT( strncasecmp( (char *) output_str, {result_hex_str}, strlen( {result_hex_str} ) ) == 0 );
367 }
368}
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
411 mpi_init( &P1, &Q1, &H, &G, NULL );
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 Bakker42a29bf2009-07-07 20:18:41 +0000444}
445END_CASE
446
447BEGIN_CASE
Paul Bakker37940d9f2009-07-10 22:38:58 +0000448rsa_check_privkey_null:
449{
450 rsa_context ctx;
451 memset( &ctx, 0x00, sizeof( rsa_context ) );
452
453 TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
454}
455END_CASE
456
457BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000458rsa_check_pubkey:radix_N:input_N:radix_E:input_E:result
459{
460 rsa_context ctx;
461
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000462 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000463
464 if( strlen( {input_N} ) )
465 {
466 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
467 }
468 if( strlen( {input_E} ) )
469 {
470 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
471 }
472
473 TEST_ASSERT( rsa_check_pubkey( &ctx ) == {result} );
474}
475END_CASE
476
477BEGIN_CASE
478rsa_check_privkey:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:radix_D:input_D:result
479{
480 rsa_context ctx;
481
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000482 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000483
484 ctx.len = {mod} / 8;
485 if( strlen( {input_P} ) )
486 {
487 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
488 }
489 if( strlen( {input_Q} ) )
490 {
491 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
492 }
493 if( strlen( {input_N} ) )
494 {
495 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
496 }
497 if( strlen( {input_E} ) )
498 {
499 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
500 }
501 if( strlen( {input_D} ) )
502 {
503 TEST_ASSERT( mpi_read_string( &ctx.D, {radix_D}, {input_D} ) == 0 );
504 }
505
506 TEST_ASSERT( rsa_check_privkey( &ctx ) == {result} );
507}
508END_CASE
509
510BEGIN_CASE
511rsa_gen_key:nrbits:exponent:result
512{
513 rsa_context ctx;
514 havege_state hs;
515
516 havege_init( &hs );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000517 rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000518
Paul Bakkera802e1a2010-08-16 11:56:45 +0000519 TEST_ASSERT( rsa_gen_key( &ctx, havege_rand, &hs, {nrbits}, {exponent} ) == {result} );
Paul Bakker821fb082009-07-12 13:26:42 +0000520 if( {result} == 0 )
521 {
522 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
523 }
524}
525END_CASE
526
527BEGIN_CASE
Paul Bakker42a29bf2009-07-07 20:18:41 +0000528rsa_selftest:
529{
530 TEST_ASSERT( rsa_self_test( 0 ) == 0 );
531}
532END_CASE