blob: 9aba577f58dba818b246a0253eac304cf522266d [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
31 mpi_init( &P1, &Q1, &H, &G, NULL );
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 Bakker42a29bf2009-07-07 20:18:41 +0000105}
106END_CASE
107
108BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000109rsa_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 +0000110{
111 unsigned char message_str[1000];
112 unsigned char hash_result[1000];
113 unsigned char result_str[1000];
114 rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000115 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000116
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000117 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000118 memset( message_str, 0x00, 1000 );
119 memset( hash_result, 0x00, 1000 );
120 memset( result_str, 0x00, 1000 );
121
122 ctx.len = {mod} / 8;
123 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
124 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
125
126 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
127
Paul Bakker69998dd2009-07-11 19:15:20 +0000128 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000129 unhexify( result_str, {result_hex_str} );
130
Paul Bakker76791f72009-10-03 20:02:00 +0000131 switch( {digest} )
132 {
133#ifdef POLARSSL_MD2_C
134 case SIG_RSA_MD2:
Paul Bakker821fb082009-07-12 13:26:42 +0000135 md2( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000136 break;
137#endif
138#ifdef POLARSSL_MD4_C
139 case SIG_RSA_MD4:
Paul Bakker821fb082009-07-12 13:26:42 +0000140 md4( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000141 break;
142#endif
143#ifdef POLARSSL_MD5_C
144 case SIG_RSA_MD5:
Paul Bakker821fb082009-07-12 13:26:42 +0000145 md5( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000146 break;
147#endif
148#ifdef POLARSSL_SHA1_C
149 case SIG_RSA_SHA1:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000150 sha1( message_str, msg_len, hash_result );
Paul Bakker76791f72009-10-03 20:02:00 +0000151 break;
152#endif
153#ifdef POLARSSL_SHA2_C
154 case SIG_RSA_SHA224:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000155 sha2( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +0000156 break;
157 case SIG_RSA_SHA256:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000158 sha2( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +0000159 break;
160#endif
161#ifdef POLARSSL_SHA4_C
162 case SIG_RSA_SHA384:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000163 sha4( message_str, msg_len, hash_result, 1 );
Paul Bakker76791f72009-10-03 20:02:00 +0000164 break;
165 case SIG_RSA_SHA512:
Paul Bakker42a29bf2009-07-07 20:18:41 +0000166 sha4( message_str, msg_len, hash_result, 0 );
Paul Bakker76791f72009-10-03 20:02:00 +0000167 break;
168#endif
169 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000170
Paul Bakker821fb082009-07-12 13:26:42 +0000171 TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, {digest}, 0, hash_result, result_str ) == {result} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000172}
173END_CASE
174
Paul Bakker821fb082009-07-12 13:26:42 +0000175
Paul Bakker42a29bf2009-07-07 20:18:41 +0000176BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000177rsa_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 +0000178{
179 unsigned char message_str[1000];
180 unsigned char hash_result[1000];
181 unsigned char output[1000];
182 unsigned char output_str[1000];
183 rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000184 mpi P1, Q1, H, G;
185 int msg_len, hash_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000186
Paul Bakker821fb082009-07-12 13:26:42 +0000187 mpi_init( &P1, &Q1, &H, &G, NULL );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000188 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000189
Paul Bakker42a29bf2009-07-07 20:18:41 +0000190 memset( message_str, 0x00, 1000 );
191 memset( hash_result, 0x00, 1000 );
192 memset( output, 0x00, 1000 );
193 memset( output_str, 0x00, 1000 );
194
195 ctx.len = {mod} / 8;
Paul Bakker821fb082009-07-12 13:26:42 +0000196 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
197 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
198 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
199 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
200
201 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
202 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
203 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
204 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
205 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
206 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
207 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
208 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
209
210 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
211
212 msg_len = unhexify( message_str, {message_hex_string} );
213 hash_len = unhexify( hash_result, {hash_result_string} );
214
Paul Bakker9dcc3222011-03-08 14:16:06 +0000215 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 +0000216
217 hexify( output_str, output, ctx.len );
218
219 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
220}
221END_CASE
222
223BEGIN_CASE
224rsa_pkcs1_verify_raw:message_hex_string:hash_result_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:correct
225{
226 unsigned char message_str[1000];
227 unsigned char hash_result[1000];
228 unsigned char result_str[1000];
229 rsa_context ctx;
230 int msg_len, hash_len;
231
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000232 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000233 memset( message_str, 0x00, 1000 );
234 memset( hash_result, 0x00, 1000 );
235 memset( result_str, 0x00, 1000 );
236
237 ctx.len = {mod} / 8;
238 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
239 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
240
241 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
242
243 msg_len = unhexify( message_str, {message_hex_string} );
244 hash_len = unhexify( hash_result, {hash_result_string} );
245 unhexify( result_str, {result_hex_str} );
246
Paul Bakkerfc22c442009-07-19 20:36:27 +0000247 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 +0000248}
249END_CASE
250
251BEGIN_CASE
252rsa_pkcs1_encrypt:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
253{
254 unsigned char message_str[1000];
255 unsigned char output[1000];
256 unsigned char output_str[1000];
257 rsa_context ctx;
258 int msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000259 rnd_pseudo_info rnd_info;
260
261 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000262
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000263 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000264 memset( message_str, 0x00, 1000 );
265 memset( output, 0x00, 1000 );
266 memset( output_str, 0x00, 1000 );
267
268 ctx.len = {mod} / 8;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000269 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
270 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
271
272 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
273
Paul Bakker69998dd2009-07-11 19:15:20 +0000274 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000275
Paul Bakker997bbd12011-03-13 15:45:42 +0000276 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 +0000277 if( {result} == 0 )
278 {
279 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000280
Paul Bakker821fb082009-07-12 13:26:42 +0000281 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
282 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000283}
284END_CASE
285
286BEGIN_CASE
Paul Bakkera6656852010-07-18 19:47:14 +0000287rsa_pkcs1_encrypt_bad_rng:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
288{
289 unsigned char message_str[1000];
290 unsigned char output[1000];
291 unsigned char output_str[1000];
292 rsa_context ctx;
293 int msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000294
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000295 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000296 memset( message_str, 0x00, 1000 );
297 memset( output, 0x00, 1000 );
298 memset( output_str, 0x00, 1000 );
299
300 ctx.len = {mod} / 8;
301 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
302 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
303
304 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
305
306 msg_len = unhexify( message_str, {message_hex_string} );
307
Paul Bakker997bbd12011-03-13 15:45:42 +0000308 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 +0000309 if( {result} == 0 )
310 {
311 hexify( output_str, output, ctx.len );
312
313 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
314 }
315}
316END_CASE
317
318BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000319rsa_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 +0000320{
321 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000322 unsigned char output[1000];
323 unsigned char output_str[1000];
324 rsa_context ctx;
325 mpi P1, Q1, H, G;
Paul Bakker69998dd2009-07-11 19:15:20 +0000326 int output_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000327
328 mpi_init( &P1, &Q1, &H, &G, NULL );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000329 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000330
331 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000332 memset( output, 0x00, 1000 );
333 memset( output_str, 0x00, 1000 );
334
335 ctx.len = {mod} / 8;
336 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
337 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
338 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
339 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
340
341 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
342 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
343 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
344 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
345 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
346 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
347 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
348 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
349
350 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
351
352 unhexify( message_str, {message_hex_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000353 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000354
Paul Bakker821fb082009-07-12 13:26:42 +0000355 TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, {max_output} ) == {result} );
356 if( {result} == 0 )
357 {
358 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000359
Paul Bakker821fb082009-07-12 13:26:42 +0000360 TEST_ASSERT( strncasecmp( (char *) output_str, {result_hex_str}, strlen( {result_hex_str} ) ) == 0 );
361 }
362}
363END_CASE
Paul Bakker42a29bf2009-07-07 20:18:41 +0000364
Paul Bakker821fb082009-07-12 13:26:42 +0000365BEGIN_CASE
366rsa_public:message_hex_string:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
367{
368 unsigned char message_str[1000];
369 unsigned char output[1000];
370 unsigned char output_str[1000];
371 rsa_context ctx;
372
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000373 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000374 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.N, {radix_N}, {input_N} ) == 0 );
380 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
381
382 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
383
384 unhexify( message_str, {message_hex_string} );
385
386 TEST_ASSERT( rsa_public( &ctx, message_str, output ) == {result} );
387 if( {result} == 0 )
388 {
389 hexify( output_str, output, ctx.len );
390
391 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
392 }
393}
394END_CASE
395
396BEGIN_CASE
397rsa_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
398{
399 unsigned char message_str[1000];
400 unsigned char output[1000];
401 unsigned char output_str[1000];
402 rsa_context ctx;
403 mpi P1, Q1, H, G;
404
405 mpi_init( &P1, &Q1, &H, &G, NULL );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000406 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000407
408 memset( message_str, 0x00, 1000 );
409 memset( output, 0x00, 1000 );
410 memset( output_str, 0x00, 1000 );
411
412 ctx.len = {mod} / 8;
413 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
414 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
415 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
416 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
417
418 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
419 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
420 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
421 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
422 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
423 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
424 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
425 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
426
427 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
428
429 unhexify( message_str, {message_hex_string} );
430
431 TEST_ASSERT( rsa_private( &ctx, message_str, output ) == {result} );
432 if( {result} == 0 )
433 {
434 hexify( output_str, output, ctx.len );
435
436 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
437 }
Paul Bakker42a29bf2009-07-07 20:18:41 +0000438}
439END_CASE
440
441BEGIN_CASE
Paul Bakker37940d9f2009-07-10 22:38:58 +0000442rsa_check_privkey_null:
443{
444 rsa_context ctx;
445 memset( &ctx, 0x00, sizeof( rsa_context ) );
446
447 TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
448}
449END_CASE
450
451BEGIN_CASE
Paul Bakker821fb082009-07-12 13:26:42 +0000452rsa_check_pubkey:radix_N:input_N:radix_E:input_E:result
453{
454 rsa_context ctx;
455
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000456 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000457
458 if( strlen( {input_N} ) )
459 {
460 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
461 }
462 if( strlen( {input_E} ) )
463 {
464 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
465 }
466
467 TEST_ASSERT( rsa_check_pubkey( &ctx ) == {result} );
468}
469END_CASE
470
471BEGIN_CASE
472rsa_check_privkey:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:radix_D:input_D:result
473{
474 rsa_context ctx;
475
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000476 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000477
478 ctx.len = {mod} / 8;
479 if( strlen( {input_P} ) )
480 {
481 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
482 }
483 if( strlen( {input_Q} ) )
484 {
485 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
486 }
487 if( strlen( {input_N} ) )
488 {
489 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
490 }
491 if( strlen( {input_E} ) )
492 {
493 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
494 }
495 if( strlen( {input_D} ) )
496 {
497 TEST_ASSERT( mpi_read_string( &ctx.D, {radix_D}, {input_D} ) == 0 );
498 }
499
500 TEST_ASSERT( rsa_check_privkey( &ctx ) == {result} );
501}
502END_CASE
503
504BEGIN_CASE
505rsa_gen_key:nrbits:exponent:result
506{
507 rsa_context ctx;
508 havege_state hs;
509
510 havege_init( &hs );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000511 rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000512
Paul Bakkera802e1a2010-08-16 11:56:45 +0000513 TEST_ASSERT( rsa_gen_key( &ctx, havege_rand, &hs, {nrbits}, {exponent} ) == {result} );
Paul Bakker821fb082009-07-12 13:26:42 +0000514 if( {result} == 0 )
515 {
516 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
517 }
518}
519END_CASE
520
521BEGIN_CASE
Paul Bakker42a29bf2009-07-07 20:18:41 +0000522rsa_selftest:
523{
524 TEST_ASSERT( rsa_self_test( 0 ) == 0 );
525}
526END_CASE