blob: 169a677260edc3eb36b6202674be41c0b8a868e1 [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
Paul Bakkera6656852010-07-18 19:47:14 +000012static int badrand( void *rng_state )
13{
14 if( rng_state != NULL )
15 rng_state = NULL;
16
17 return( 0 );
18}
Paul Bakker42a29bf2009-07-07 20:18:41 +000019END_HEADER
20
21BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +000022rsa_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 +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
32 mpi_init( &P1, &Q1, &H, &G, NULL );
Paul Bakkerebcef6d2010-08-16 11:10:49 +000033 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
40 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 );
45
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 Bakker69998dd2009-07-11 19:15:20 +000057 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +000058
Paul Bakker76791f72009-10-03 20:02:00 +000059 switch( {digest} )
60 {
61#ifdef POLARSSL_MD2_C
62 case SIG_RSA_MD2:
Paul Bakker821fb082009-07-12 13:26:42 +000063 md2( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +000064 break;
65#endif
66#ifdef POLARSSL_MD4_C
67 case SIG_RSA_MD4:
Paul Bakker821fb082009-07-12 13:26:42 +000068 md4( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +000069 break;
70#endif
71#ifdef POLARSSL_MD5_C
72 case SIG_RSA_MD5:
Paul Bakker821fb082009-07-12 13:26:42 +000073 md5( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +000074 break;
75#endif
76#ifdef POLARSSL_SHA1_C
77 case SIG_RSA_SHA1:
Paul Bakker42a29bf2009-07-07 20:18:41 +000078 sha1( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +000079 break;
80#endif
81#ifdef POLARSSL_SHA2_C
82 case SIG_RSA_SHA224:
Paul Bakker42a29bf2009-07-07 20:18:41 +000083 sha2( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +000084 break;
85 case SIG_RSA_SHA256:
Paul Bakker42a29bf2009-07-07 20:18:41 +000086 sha2( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +000087 break;
88#endif
89#ifdef POLARSSL_SHA4_C
90 case SIG_RSA_SHA384:
Paul Bakker42a29bf2009-07-07 20:18:41 +000091 sha4( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +000092 break;
93 case SIG_RSA_SHA512:
Paul Bakker42a29bf2009-07-07 20:18:41 +000094 sha4( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +000095 break;
96#endif
97 }
Paul Bakker42a29bf2009-07-07 20:18:41 +000098
Paul Bakker9dcc3222011-03-08 14:16:06 +000099 TEST_ASSERT( rsa_pkcs1_sign( &ctx, NULL, NULL, RSA_PRIVATE, {digest}, 0, hash_result, output ) == {result} );
Paul Bakker821fb082009-07-12 13:26:42 +0000100 if( {result} == 0 )
101 {
102 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000103
Paul Bakker821fb082009-07-12 13:26:42 +0000104 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
105 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000106}
107END_CASE
108
109BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000110rsa_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 +0000111{
112 unsigned char message_str[1000];
113 unsigned char hash_result[1000];
114 unsigned char result_str[1000];
115 rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000116 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000117
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000118 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000119 memset( message_str, 0x00, 1000 );
120 memset( hash_result, 0x00, 1000 );
121 memset( result_str, 0x00, 1000 );
122
123 ctx.len = {mod} / 8;
124 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
125 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
126
127 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
128
Paul Bakker69998dd2009-07-11 19:15:20 +0000129 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000130 unhexify( result_str, {result_hex_str} );
131
Paul Bakker76791f72009-10-03 20:02:00 +0000132 switch( {digest} )
133 {
134#ifdef POLARSSL_MD2_C
135 case SIG_RSA_MD2:
Paul Bakker821fb082009-07-12 13:26:42 +0000136 md2( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000137 break;
138#endif
139#ifdef POLARSSL_MD4_C
140 case SIG_RSA_MD4:
Paul Bakker821fb082009-07-12 13:26:42 +0000141 md4( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000142 break;
143#endif
144#ifdef POLARSSL_MD5_C
145 case SIG_RSA_MD5:
Paul Bakker821fb082009-07-12 13:26:42 +0000146 md5( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000147 break;
148#endif
149#ifdef POLARSSL_SHA1_C
150 case SIG_RSA_SHA1:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000151 sha1( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000152 break;
153#endif
154#ifdef POLARSSL_SHA2_C
155 case SIG_RSA_SHA224:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000156 sha2( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +0000157 break;
158 case SIG_RSA_SHA256:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000159 sha2( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +0000160 break;
161#endif
162#ifdef POLARSSL_SHA4_C
163 case SIG_RSA_SHA384:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000164 sha4( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +0000165 break;
166 case SIG_RSA_SHA512:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000167 sha4( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +0000168 break;
169#endif
170 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000171
Paul Bakker821fb082009-07-12 13:26:42 +0000172 TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, {digest}, 0, hash_result, result_str ) == {result} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000173}
174END_CASE
175
Paul Bakker821fb082009-07-12 13:26:42 +0000176
Paul Bakker42a29bf2009-07-07 20:18:41 +0000177BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000178rsa_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 +0000179{
180 unsigned char message_str[1000];
181 unsigned char hash_result[1000];
182 unsigned char output[1000];
183 unsigned char output_str[1000];
184 rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000185 mpi P1, Q1, H, G;
186 int msg_len, hash_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000187
Paul Bakker821fb082009-07-12 13:26:42 +0000188 mpi_init( &P1, &Q1, &H, &G, NULL );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000189 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000190
Paul Bakker42a29bf2009-07-07 20:18:41 +0000191 memset( message_str, 0x00, 1000 );
192 memset( hash_result, 0x00, 1000 );
193 memset( output, 0x00, 1000 );
194 memset( output_str, 0x00, 1000 );
195
196 ctx.len = {mod} / 8;
Paul Bakker821fb082009-07-12 13:26:42 +0000197 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
198 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
199 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
200 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
201
202 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
203 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
204 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
205 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
206 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
207 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
208 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
209 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
210
211 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
212
213 msg_len = unhexify( message_str, {message_hex_string} );
214 hash_len = unhexify( hash_result, {hash_result_string} );
215
Paul Bakker9dcc3222011-03-08 14:16:06 +0000216 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 +0000217
218 hexify( output_str, output, ctx.len );
219
220 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
221}
222END_CASE
223
224BEGIN_CASE
225rsa_pkcs1_verify_raw:message_hex_string:hash_result_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:correct
226{
227 unsigned char message_str[1000];
228 unsigned char hash_result[1000];
229 unsigned char result_str[1000];
230 rsa_context ctx;
231 int msg_len, hash_len;
232
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000233 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000234 memset( message_str, 0x00, 1000 );
235 memset( hash_result, 0x00, 1000 );
236 memset( result_str, 0x00, 1000 );
237
238 ctx.len = {mod} / 8;
239 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
240 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
241
242 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
243
244 msg_len = unhexify( message_str, {message_hex_string} );
245 hash_len = unhexify( hash_result, {hash_result_string} );
246 unhexify( result_str, {result_hex_str} );
247
Paul Bakkerfc22c442009-07-19 20:36:27 +0000248 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 +0000249}
250END_CASE
251
252BEGIN_CASE
253rsa_pkcs1_encrypt:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
254{
255 unsigned char message_str[1000];
256 unsigned char output[1000];
257 unsigned char output_str[1000];
258 rsa_context ctx;
259 int msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000260 rnd_pseudo_info rnd_info;
261
262 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000263
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000264 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000265 memset( message_str, 0x00, 1000 );
266 memset( output, 0x00, 1000 );
267 memset( output_str, 0x00, 1000 );
268
269 ctx.len = {mod} / 8;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000270 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
271 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
272
273 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
274
Paul Bakker69998dd2009-07-11 19:15:20 +0000275 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000276
Paul Bakker997bbd12011-03-13 15:45:42 +0000277 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 +0000278 if( {result} == 0 )
279 {
280 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000281
Paul Bakker821fb082009-07-12 13:26:42 +0000282 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
283 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000284}
285END_CASE
286
287BEGIN_CASE
Paul Bakkera6656852010-07-18 19:47:14 +0000288rsa_pkcs1_encrypt_bad_rng:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
289{
290 unsigned char message_str[1000];
291 unsigned char output[1000];
292 unsigned char output_str[1000];
293 rsa_context ctx;
294 int msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000295
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000296 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000297 memset( message_str, 0x00, 1000 );
298 memset( output, 0x00, 1000 );
299 memset( output_str, 0x00, 1000 );
300
301 ctx.len = {mod} / 8;
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 );
304
305 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
306
307 msg_len = unhexify( message_str, {message_hex_string} );
308
Paul Bakker997bbd12011-03-13 15:45:42 +0000309 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 +0000310 if( {result} == 0 )
311 {
312 hexify( output_str, output, ctx.len );
313
314 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
315 }
316}
317END_CASE
318
319BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000320rsa_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 +0000321{
322 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000323 unsigned char output[1000];
324 unsigned char output_str[1000];
325 rsa_context ctx;
326 mpi P1, Q1, H, G;
Paul Bakker69998dd2009-07-11 19:15:20 +0000327 int output_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000328
329 mpi_init( &P1, &Q1, &H, &G, NULL );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000330 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000331
332 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000333 memset( output, 0x00, 1000 );
334 memset( output_str, 0x00, 1000 );
335
336 ctx.len = {mod} / 8;
337 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
338 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
339 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
340 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
341
342 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
343 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
344 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
345 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
346 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
347 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
348 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
349 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
350
351 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
352
353 unhexify( message_str, {message_hex_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000354 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000355
Paul Bakker821fb082009-07-12 13:26:42 +0000356 TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, {max_output} ) == {result} );
357 if( {result} == 0 )
358 {
359 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000360
Paul Bakker821fb082009-07-12 13:26:42 +0000361 TEST_ASSERT( strncasecmp( (char *) output_str, {result_hex_str}, strlen( {result_hex_str} ) ) == 0 );
362 }
363}
364END_CASE
Paul Bakker42a29bf2009-07-07 20:18:41 +0000365
Paul Bakker821fb082009-07-12 13:26:42 +0000366BEGIN_CASE
367rsa_public:message_hex_string:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
368{
369 unsigned char message_str[1000];
370 unsigned char output[1000];
371 unsigned char output_str[1000];
372 rsa_context ctx;
373
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000374 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000375 memset( message_str, 0x00, 1000 );
376 memset( output, 0x00, 1000 );
377 memset( output_str, 0x00, 1000 );
378
379 ctx.len = {mod} / 8;
380 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
381 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
382
383 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
384
385 unhexify( message_str, {message_hex_string} );
386
387 TEST_ASSERT( rsa_public( &ctx, message_str, output ) == {result} );
388 if( {result} == 0 )
389 {
390 hexify( output_str, output, ctx.len );
391
392 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
393 }
394}
395END_CASE
396
397BEGIN_CASE
398rsa_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
399{
400 unsigned char message_str[1000];
401 unsigned char output[1000];
402 unsigned char output_str[1000];
403 rsa_context ctx;
404 mpi P1, Q1, H, G;
405
406 mpi_init( &P1, &Q1, &H, &G, NULL );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000407 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000408
409 memset( message_str, 0x00, 1000 );
410 memset( output, 0x00, 1000 );
411 memset( output_str, 0x00, 1000 );
412
413 ctx.len = {mod} / 8;
414 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
415 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
416 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
417 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
418
419 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
420 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
421 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
422 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
423 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
424 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
425 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
426 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
427
428 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
429
430 unhexify( message_str, {message_hex_string} );
431
432 TEST_ASSERT( rsa_private( &ctx, message_str, output ) == {result} );
433 if( {result} == 0 )
434 {
435 hexify( output_str, output, ctx.len );
436
437 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
438 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000439}
440END_CASE
441
442BEGIN_CASE
Paul Bakker37940d9f2009-07-10 22:38:58 +0000443rsa_check_privkey_null:
444{
445 rsa_context ctx;
446 memset( &ctx, 0x00, sizeof( rsa_context ) );
447
448 TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
449}
450END_CASE
451
452BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000453rsa_check_pubkey:radix_N:input_N:radix_E:input_E:result
454{
455 rsa_context ctx;
456
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000457 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000458
459 if( strlen( {input_N} ) )
460 {
461 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
462 }
463 if( strlen( {input_E} ) )
464 {
465 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
466 }
467
468 TEST_ASSERT( rsa_check_pubkey( &ctx ) == {result} );
469}
470END_CASE
471
472BEGIN_CASE
473rsa_check_privkey:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:radix_D:input_D:result
474{
475 rsa_context ctx;
476
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000477 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000478
479 ctx.len = {mod} / 8;
480 if( strlen( {input_P} ) )
481 {
482 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
483 }
484 if( strlen( {input_Q} ) )
485 {
486 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
487 }
488 if( strlen( {input_N} ) )
489 {
490 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
491 }
492 if( strlen( {input_E} ) )
493 {
494 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
495 }
496 if( strlen( {input_D} ) )
497 {
498 TEST_ASSERT( mpi_read_string( &ctx.D, {radix_D}, {input_D} ) == 0 );
499 }
500
501 TEST_ASSERT( rsa_check_privkey( &ctx ) == {result} );
502}
503END_CASE
504
505BEGIN_CASE
506rsa_gen_key:nrbits:exponent:result
507{
508 rsa_context ctx;
509 havege_state hs;
510
511 havege_init( &hs );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000512 rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000513
Paul Bakkera802e1a2010-08-16 11:56:45 +0000514 TEST_ASSERT( rsa_gen_key( &ctx, havege_rand, &hs, {nrbits}, {exponent} ) == {result} );
Paul Bakker821fb082009-07-12 13:26:42 +0000515 if( {result} == 0 )
516 {
517 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
518 }
519}
520END_CASE
521
522BEGIN_CASE
Paul Bakker42a29bf2009-07-07 20:18:41 +0000523rsa_selftest:
524{
525 TEST_ASSERT( rsa_self_test( 0 ) == 0 );
526}
527END_CASE