blob: 30eff8fb53bb227fe47a493af9e8bef0ee4c3934 [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>
Paul Bakkerd2681d82013-06-30 14:49:12 +02007#include <polarssl/sha256.h>
8#include <polarssl/sha512.h>
Paul Bakkerc0a1a312011-12-04 17:12:15 +00009#include <polarssl/entropy.h>
10#include <polarssl/ctr_drbg.h>
Paul Bakker42a29bf2009-07-07 20:18:41 +000011END_HEADER
12
Paul Bakker5690efc2011-05-26 13:16:06 +000013BEGIN_DEPENDENCIES
Paul Bakkerd7d8dbe2011-05-26 15:29:38 +000014depends_on:POLARSSL_RSA_C:POLARSSL_BIGNUM_C:POLARSSL_GENPRIME
Paul Bakker5690efc2011-05-26 13:16:06 +000015END_DEPENDENCIES
16
Paul Bakker42a29bf2009-07-07 20:18:41 +000017BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +020018rsa_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 +000019{
20 unsigned char message_str[1000];
21 unsigned char hash_result[1000];
22 unsigned char output[1000];
23 unsigned char output_str[1000];
24 rsa_context ctx;
25 mpi P1, Q1, H, G;
Paul Bakker69998dd2009-07-11 19:15:20 +000026 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +000027
Paul Bakker6c591fa2011-05-05 11:49:20 +000028 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakkerebcef6d2010-08-16 11:10:49 +000029 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000030
31 memset( message_str, 0x00, 1000 );
32 memset( hash_result, 0x00, 1000 );
33 memset( output, 0x00, 1000 );
34 memset( output_str, 0x00, 1000 );
35
36 ctx.len = {mod} / 8;
37 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
38 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
39 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
40 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
41
42 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
43 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
44 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
45 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
46 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
47 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
48 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
49 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
50
51 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
52
Paul Bakker69998dd2009-07-11 19:15:20 +000053 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +000054
Paul Bakkerc70b9822013-04-07 22:00:46 +020055 if( md_info_from_type( {digest} ) != NULL )
56 TEST_ASSERT( md( md_info_from_type( {digest} ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000057
Paul Bakker9dcc3222011-03-08 14:16:06 +000058 TEST_ASSERT( rsa_pkcs1_sign( &ctx, NULL, NULL, RSA_PRIVATE, {digest}, 0, hash_result, output ) == {result} );
Paul Bakker821fb082009-07-12 13:26:42 +000059 if( {result} == 0 )
60 {
61 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +000062
Paul Bakker821fb082009-07-12 13:26:42 +000063 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
64 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000065
66 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010067 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +000068}
69END_CASE
70
71BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +020072rsa_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 +000073{
74 unsigned char message_str[1000];
75 unsigned char hash_result[1000];
76 unsigned char result_str[1000];
77 rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000078 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +000079
Paul Bakkerebcef6d2010-08-16 11:10:49 +000080 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000081 memset( message_str, 0x00, 1000 );
82 memset( hash_result, 0x00, 1000 );
83 memset( result_str, 0x00, 1000 );
84
85 ctx.len = {mod} / 8;
86 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
87 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
88
89 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
90
Paul Bakker69998dd2009-07-11 19:15:20 +000091 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +000092 unhexify( result_str, {result_hex_str} );
93
Paul Bakkerc70b9822013-04-07 22:00:46 +020094 if( md_info_from_type( {digest} ) != NULL )
95 TEST_ASSERT( md( md_info_from_type( {digest} ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000096
Paul Bakker821fb082009-07-12 13:26:42 +000097 TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, {digest}, 0, hash_result, result_str ) == {result} );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010098
99 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000100}
101END_CASE
102
Paul Bakker821fb082009-07-12 13:26:42 +0000103
Paul Bakker42a29bf2009-07-07 20:18:41 +0000104BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200105rsa_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 +0000106{
107 unsigned char message_str[1000];
108 unsigned char hash_result[1000];
109 unsigned char output[1000];
110 unsigned char output_str[1000];
111 rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000112 mpi P1, Q1, H, G;
Paul Bakkereaf90d92011-07-13 14:21:52 +0000113 int hash_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000114
Paul Bakker6c591fa2011-05-05 11:49:20 +0000115 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000116 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000117
Paul Bakker42a29bf2009-07-07 20:18:41 +0000118 memset( message_str, 0x00, 1000 );
119 memset( hash_result, 0x00, 1000 );
120 memset( output, 0x00, 1000 );
121 memset( output_str, 0x00, 1000 );
122
123 ctx.len = {mod} / 8;
Paul Bakker821fb082009-07-12 13:26:42 +0000124 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
125 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
126 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
127 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
128
129 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
130 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
131 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
132 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
133 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
134 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
135 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
136 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
137
138 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
139
Paul Bakkereaf90d92011-07-13 14:21:52 +0000140 unhexify( message_str, {message_hex_string} );
Paul Bakker821fb082009-07-12 13:26:42 +0000141 hash_len = unhexify( hash_result, {hash_result_string} );
142
Paul Bakkerc70b9822013-04-07 22:00:46 +0200143 TEST_ASSERT( rsa_pkcs1_sign( &ctx, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_NONE, hash_len, hash_result, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000144
145 hexify( output_str, output, ctx.len );
146
147 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000148
149 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100150 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000151}
152END_CASE
153
154BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200155rsa_pkcs1_verify_raw:message_hex_string:hash_result_string:#padding_mode:#mod:#radix_N:input_N:#radix_E:input_E:result_hex_str:#correct
Paul Bakker821fb082009-07-12 13:26:42 +0000156{
157 unsigned char message_str[1000];
158 unsigned char hash_result[1000];
159 unsigned char result_str[1000];
160 rsa_context ctx;
Paul Bakkereaf90d92011-07-13 14:21:52 +0000161 size_t hash_len;
Paul Bakker821fb082009-07-12 13:26:42 +0000162
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000163 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000164 memset( message_str, 0x00, 1000 );
165 memset( hash_result, 0x00, 1000 );
166 memset( result_str, 0x00, 1000 );
167
168 ctx.len = {mod} / 8;
169 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
170 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
171
172 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
173
Paul Bakkereaf90d92011-07-13 14:21:52 +0000174 unhexify( message_str, {message_hex_string} );
Paul Bakker821fb082009-07-12 13:26:42 +0000175 hash_len = unhexify( hash_result, {hash_result_string} );
176 unhexify( result_str, {result_hex_str} );
177
Paul Bakkerc70b9822013-04-07 22:00:46 +0200178 TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, POLARSSL_MD_NONE, hash_len, hash_result, result_str ) == {correct} );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100179
180 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000181}
182END_CASE
183
184BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200185rsa_pkcs1_encrypt:message_hex_string:#padding_mode:#mod:#radix_N:input_N:#radix_E:input_E:result_hex_str:#result
Paul Bakker821fb082009-07-12 13:26:42 +0000186{
187 unsigned char message_str[1000];
188 unsigned char output[1000];
189 unsigned char output_str[1000];
190 rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000191 size_t msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000192 rnd_pseudo_info rnd_info;
193
194 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000195
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000196 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000197 memset( message_str, 0x00, 1000 );
198 memset( output, 0x00, 1000 );
199 memset( output_str, 0x00, 1000 );
200
201 ctx.len = {mod} / 8;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000202 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
203 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
204
205 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
206
Paul Bakker69998dd2009-07-11 19:15:20 +0000207 msg_len = unhexify( message_str, {message_hex_string} );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000208
Paul Bakker997bbd12011-03-13 15:45:42 +0000209 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 +0000210 if( {result} == 0 )
211 {
212 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000213
Paul Bakker821fb082009-07-12 13:26:42 +0000214 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
215 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100216
217 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000218}
219END_CASE
220
221BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200222rsa_pkcs1_encrypt_bad_rng:message_hex_string:#padding_mode:#mod:#radix_N:input_N:#radix_E:input_E:result_hex_str:#result
Paul Bakkera6656852010-07-18 19:47:14 +0000223{
224 unsigned char message_str[1000];
225 unsigned char output[1000];
226 unsigned char output_str[1000];
227 rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000228 size_t msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000229
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000230 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000231 memset( message_str, 0x00, 1000 );
232 memset( output, 0x00, 1000 );
233 memset( output_str, 0x00, 1000 );
234
235 ctx.len = {mod} / 8;
236 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
237 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
238
239 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
240
241 msg_len = unhexify( message_str, {message_hex_string} );
242
Paul Bakker997bbd12011-03-13 15:45:42 +0000243 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 +0000244 if( {result} == 0 )
245 {
246 hexify( output_str, output, ctx.len );
247
248 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
249 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100250
251 rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000252}
253END_CASE
254
255BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200256rsa_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 +0000257{
258 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000259 unsigned char output[1000];
260 unsigned char output_str[1000];
261 rsa_context ctx;
262 mpi P1, Q1, H, G;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000263 size_t output_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000264
Paul Bakker6c591fa2011-05-05 11:49:20 +0000265 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000266 rsa_init( &ctx, {padding_mode}, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000267
268 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000269 memset( output, 0x00, 1000 );
270 memset( output_str, 0x00, 1000 );
271
272 ctx.len = {mod} / 8;
273 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
274 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
275 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( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
279 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
280 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
281 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
282 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
283 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
284 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
285 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
286
287 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
288
289 unhexify( message_str, {message_hex_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000290 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000291
Paul Bakker821fb082009-07-12 13:26:42 +0000292 TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, {max_output} ) == {result} );
293 if( {result} == 0 )
294 {
295 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000296
Paul Bakker821fb082009-07-12 13:26:42 +0000297 TEST_ASSERT( strncasecmp( (char *) output_str, {result_hex_str}, strlen( {result_hex_str} ) ) == 0 );
298 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000299
300 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100301 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000302}
303END_CASE
Paul Bakker42a29bf2009-07-07 20:18:41 +0000304
Paul Bakker821fb082009-07-12 13:26:42 +0000305BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200306rsa_public:message_hex_string:#mod:#radix_N:input_N:#radix_E:input_E:result_hex_str:#result
Paul Bakker821fb082009-07-12 13:26:42 +0000307{
308 unsigned char message_str[1000];
309 unsigned char output[1000];
310 unsigned char output_str[1000];
311 rsa_context ctx;
312
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000313 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000314 memset( message_str, 0x00, 1000 );
315 memset( output, 0x00, 1000 );
316 memset( output_str, 0x00, 1000 );
317
318 ctx.len = {mod} / 8;
319 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
320 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
321
322 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
323
324 unhexify( message_str, {message_hex_string} );
325
326 TEST_ASSERT( rsa_public( &ctx, message_str, output ) == {result} );
327 if( {result} == 0 )
328 {
329 hexify( output_str, output, ctx.len );
330
331 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
332 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100333
334 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000335}
336END_CASE
337
338BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200339rsa_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
Paul Bakker821fb082009-07-12 13:26:42 +0000340{
341 unsigned char message_str[1000];
342 unsigned char output[1000];
343 unsigned char output_str[1000];
344 rsa_context ctx;
345 mpi P1, Q1, H, G;
346
Paul Bakker6c591fa2011-05-05 11:49:20 +0000347 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000348 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000349
350 memset( message_str, 0x00, 1000 );
351 memset( output, 0x00, 1000 );
352 memset( output_str, 0x00, 1000 );
353
354 ctx.len = {mod} / 8;
355 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
356 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
357 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
358 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
359
360 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
361 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
362 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
363 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
364 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
365 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
366 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
367 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
368
369 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
370
371 unhexify( message_str, {message_hex_string} );
372
373 TEST_ASSERT( rsa_private( &ctx, message_str, output ) == {result} );
374 if( {result} == 0 )
375 {
376 hexify( output_str, output, ctx.len );
377
378 TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
379 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000380
381 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100382 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000383}
384END_CASE
385
386BEGIN_CASE
Paul Bakker37940d9f2009-07-10 22:38:58 +0000387rsa_check_privkey_null:
388{
389 rsa_context ctx;
390 memset( &ctx, 0x00, sizeof( rsa_context ) );
391
392 TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
393}
394END_CASE
395
396BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200397rsa_check_pubkey:#radix_N:input_N:#radix_E:input_E:#result
Paul Bakker821fb082009-07-12 13:26:42 +0000398{
399 rsa_context ctx;
400
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000401 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000402
403 if( strlen( {input_N} ) )
404 {
405 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
406 }
407 if( strlen( {input_E} ) )
408 {
409 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
410 }
411
412 TEST_ASSERT( rsa_check_pubkey( &ctx ) == {result} );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100413
414 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000415}
416END_CASE
417
418BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200419rsa_check_privkey:#mod:#radix_P:input_P:#radix_Q:input_Q:#radix_N:input_N:#radix_E:input_E:#radix_D:input_D:#radix_DP:input_DP:#radix_DQ:input_DQ:#radix_QP:input_QP:#result
Paul Bakker821fb082009-07-12 13:26:42 +0000420{
421 rsa_context ctx;
422
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000423 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000424
425 ctx.len = {mod} / 8;
426 if( strlen( {input_P} ) )
427 {
428 TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
429 }
430 if( strlen( {input_Q} ) )
431 {
432 TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
433 }
434 if( strlen( {input_N} ) )
435 {
436 TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
437 }
438 if( strlen( {input_E} ) )
439 {
440 TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
441 }
442 if( strlen( {input_D} ) )
443 {
444 TEST_ASSERT( mpi_read_string( &ctx.D, {radix_D}, {input_D} ) == 0 );
445 }
Paul Bakker31417a72012-09-27 20:41:37 +0000446 if( strlen( {input_DP} ) )
447 {
448 TEST_ASSERT( mpi_read_string( &ctx.DP, {radix_DP}, {input_DP} ) == 0 );
449 }
450 if( strlen( {input_DQ} ) )
451 {
452 TEST_ASSERT( mpi_read_string( &ctx.DQ, {radix_DQ}, {input_DQ} ) == 0 );
453 }
454 if( strlen( {input_QP} ) )
455 {
456 TEST_ASSERT( mpi_read_string( &ctx.QP, {radix_QP}, {input_QP} ) == 0 );
457 }
Paul Bakker821fb082009-07-12 13:26:42 +0000458
459 TEST_ASSERT( rsa_check_privkey( &ctx ) == {result} );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100460
461 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000462}
463END_CASE
464
465BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200466rsa_gen_key:#nrbits:#exponent:#result
Paul Bakker821fb082009-07-12 13:26:42 +0000467{
468 rsa_context ctx;
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000469 entropy_context entropy;
470 ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200471 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000472
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000473 entropy_init( &entropy );
474 TEST_ASSERT( ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200475 (const unsigned char *) pers, strlen( pers ) ) == 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000476
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000477 rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000478
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000479 TEST_ASSERT( rsa_gen_key( &ctx, ctr_drbg_random, &ctr_drbg, {nrbits}, {exponent} ) == {result} );
Paul Bakker821fb082009-07-12 13:26:42 +0000480 if( {result} == 0 )
481 {
482 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
483 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100484
485 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000486}
487END_CASE
488
489BEGIN_CASE
Paul Bakker42a29bf2009-07-07 20:18:41 +0000490rsa_selftest:
491{
492 TEST_ASSERT( rsa_self_test( 0 ) == 0 );
493}
494END_CASE