blob: 896eebbf98c2ba6eb8749464b19730a7780b1ae0 [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 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 Bakker545570e2010-07-18 09:00:25 +000033 rsa_init( &ctx, {padding_mode}, 0, &myrand, NULL );
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 Bakker821fb082009-07-12 13:26:42 +000099 TEST_ASSERT( rsa_pkcs1_sign( &ctx, RSA_PRIVATE, {digest}, 0, hash_result, output ) == {result} );
100 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 Bakker545570e2010-07-18 09:00:25 +0000118 rsa_init( &ctx, {padding_mode}, 0, &myrand, NULL );
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 Bakker545570e2010-07-18 09:00:25 +0000189 rsa_init( &ctx, {padding_mode}, 0, &myrand, NULL );
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 Bakkerfc22c442009-07-19 20:36:27 +0000216 TEST_ASSERT( rsa_pkcs1_sign( &ctx, 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 Bakker545570e2010-07-18 09:00:25 +0000233 rsa_init( &ctx, {padding_mode}, 0, &myrand, NULL );
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;
260
Paul Bakker545570e2010-07-18 09:00:25 +0000261 rsa_init( &ctx, {padding_mode}, 0, &myrand, NULL );
Paul Bakker821fb082009-07-12 13:26:42 +0000262 memset( message_str, 0x00, 1000 );
263 memset( output, 0x00, 1000 );
264 memset( output_str, 0x00, 1000 );
265
266 ctx.len = {mod} / 8;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000267 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
268 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
269
270 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
271
Paul Bakker69998dd2009-07-11 19:15:20 +0000272 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000273
Paul Bakker821fb082009-07-12 13:26:42 +0000274 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, RSA_PUBLIC, msg_len, message_str, output ) == {result} );
275 if( {result} == 0 )
276 {
277 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000278
Paul Bakker821fb082009-07-12 13:26:42 +0000279 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
280 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000281}
282END_CASE
283
284BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000285rsa_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 +0000286{
287 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000288 unsigned char output[1000];
289 unsigned char output_str[1000];
290 rsa_context ctx;
291 mpi P1, Q1, H, G;
Paul Bakker69998dd2009-07-11 19:15:20 +0000292 int output_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000293
294 mpi_init( &P1, &Q1, &H, &G, NULL );
Paul Bakker545570e2010-07-18 09:00:25 +0000295 rsa_init( &ctx, {padding_mode}, 0, &myrand, NULL );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000296
297 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000298 memset( output, 0x00, 1000 );
299 memset( output_str, 0x00, 1000 );
300
301 ctx.len = {mod} / 8;
302 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
303 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
304 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
305 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
306
307 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
308 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
309 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
310 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
311 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
312 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
313 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
314 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
315
316 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
317
318 unhexify( message_str, {message_hex_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000319 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000320
Paul Bakker821fb082009-07-12 13:26:42 +0000321 TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, {max_output} ) == {result} );
322 if( {result} == 0 )
323 {
324 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000325
Paul Bakker821fb082009-07-12 13:26:42 +0000326 TEST_ASSERT( strncasecmp( (char *) output_str, {result_hex_str}, strlen( {result_hex_str} ) ) == 0 );
327 }
328}
329END_CASE
Paul Bakker42a29bf2009-07-07 20:18:41 +0000330
Paul Bakker821fb082009-07-12 13:26:42 +0000331BEGIN_CASE
332rsa_public:message_hex_string:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
333{
334 unsigned char message_str[1000];
335 unsigned char output[1000];
336 unsigned char output_str[1000];
337 rsa_context ctx;
338
Paul Bakker545570e2010-07-18 09:00:25 +0000339 rsa_init( &ctx, RSA_PKCS_V15, 0, &myrand, NULL );
Paul Bakker821fb082009-07-12 13:26:42 +0000340 memset( message_str, 0x00, 1000 );
341 memset( output, 0x00, 1000 );
342 memset( output_str, 0x00, 1000 );
343
344 ctx.len = {mod} / 8;
345 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
346 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
347
348 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
349
350 unhexify( message_str, {message_hex_string} );
351
352 TEST_ASSERT( rsa_public( &ctx, message_str, output ) == {result} );
353 if( {result} == 0 )
354 {
355 hexify( output_str, output, ctx.len );
356
357 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
358 }
359}
360END_CASE
361
362BEGIN_CASE
363rsa_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
364{
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
371 mpi_init( &P1, &Q1, &H, &G, NULL );
Paul Bakker545570e2010-07-18 09:00:25 +0000372 rsa_init( &ctx, RSA_PKCS_V15, 0, &myrand, NULL );
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
378 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 );
383
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
395 unhexify( message_str, {message_hex_string} );
396
397 TEST_ASSERT( rsa_private( &ctx, message_str, output ) == {result} );
398 if( {result} == 0 )
399 {
400 hexify( output_str, output, ctx.len );
401
402 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
403 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000404}
405END_CASE
406
407BEGIN_CASE
Paul Bakker37940d9f2009-07-10 22:38:58 +0000408rsa_check_privkey_null:
409{
410 rsa_context ctx;
411 memset( &ctx, 0x00, sizeof( rsa_context ) );
412
413 TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
414}
415END_CASE
416
417BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000418rsa_check_pubkey:radix_N:input_N:radix_E:input_E:result
419{
420 rsa_context ctx;
421
Paul Bakker545570e2010-07-18 09:00:25 +0000422 rsa_init( &ctx, RSA_PKCS_V15, 0, &myrand, NULL );
Paul Bakker821fb082009-07-12 13:26:42 +0000423
424 if( strlen( {input_N} ) )
425 {
426 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
427 }
428 if( strlen( {input_E} ) )
429 {
430 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
431 }
432
433 TEST_ASSERT( rsa_check_pubkey( &ctx ) == {result} );
434}
435END_CASE
436
437BEGIN_CASE
438rsa_check_privkey:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:radix_D:input_D:result
439{
440 rsa_context ctx;
441
Paul Bakker545570e2010-07-18 09:00:25 +0000442 rsa_init( &ctx, RSA_PKCS_V15, 0, &myrand, NULL );
Paul Bakker821fb082009-07-12 13:26:42 +0000443
444 ctx.len = {mod} / 8;
445 if( strlen( {input_P} ) )
446 {
447 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
448 }
449 if( strlen( {input_Q} ) )
450 {
451 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
452 }
453 if( strlen( {input_N} ) )
454 {
455 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
456 }
457 if( strlen( {input_E} ) )
458 {
459 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
460 }
461 if( strlen( {input_D} ) )
462 {
463 TEST_ASSERT( mpi_read_string( &ctx.D, {radix_D}, {input_D} ) == 0 );
464 }
465
466 TEST_ASSERT( rsa_check_privkey( &ctx ) == {result} );
467}
468END_CASE
469
470BEGIN_CASE
471rsa_gen_key:nrbits:exponent:result
472{
473 rsa_context ctx;
474 havege_state hs;
475
476 havege_init( &hs );
477 rsa_init( &ctx, 0, 0, havege_rand, &hs );
478
479 TEST_ASSERT( rsa_gen_key( &ctx, {nrbits}, {exponent} ) == {result} );
480 if( {result} == 0 )
481 {
482 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
483 }
484}
485END_CASE
486
487BEGIN_CASE
Paul Bakker42a29bf2009-07-07 20:18:41 +0000488rsa_selftest:
489{
490 TEST_ASSERT( rsa_self_test( 0 ) == 0 );
491}
492END_CASE