blob: a01b21710899ac7c4029fd2f5cc941df96886f3e [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +00002#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 Bakker33b43f12013-08-20 11:48:36 +020011/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +000012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* BEGIN_DEPENDENCIES
14 * depends_on:POLARSSL_RSA_C:POLARSSL_BIGNUM_C:POLARSSL_GENPRIME
15 * END_DEPENDENCIES
16 */
Paul Bakker5690efc2011-05-26 13:16:06 +000017
Paul Bakker33b43f12013-08-20 11:48:36 +020018/* BEGIN_CASE */
19void rsa_pkcs1_sign( char *message_hex_string, int padding_mode, int digest,
20 int mod, int radix_P, char *input_P, int radix_Q,
21 char *input_Q, int radix_N, char *input_N, int radix_E,
22 char *input_E, char *result_hex_str, int 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 Bakker548957d2013-08-30 10:30:02 +020031 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +000032
Paul Bakker6c591fa2011-05-05 11:49:20 +000033 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakker33b43f12013-08-20 11:48:36 +020034 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000035
36 memset( message_str, 0x00, 1000 );
37 memset( hash_result, 0x00, 1000 );
38 memset( output, 0x00, 1000 );
39 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +020040 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +000041
Paul Bakker33b43f12013-08-20 11:48:36 +020042 ctx.len = mod / 8;
43 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
44 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
45 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
46 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000047
48 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
49 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
50 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
51 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
52 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
53 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
54 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
55 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
56
57 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
58
Paul Bakker33b43f12013-08-20 11:48:36 +020059 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +000060
Paul Bakker33b43f12013-08-20 11:48:36 +020061 if( md_info_from_type( digest ) != NULL )
62 TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000063
Paul Bakker548957d2013-08-30 10:30:02 +020064 TEST_ASSERT( rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, digest, 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020065 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +000066 {
67 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +000068
Paul Bakker33b43f12013-08-20 11:48:36 +020069 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +000070 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000071
72 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010073 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +000074}
Paul Bakker33b43f12013-08-20 11:48:36 +020075/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +000076
Paul Bakker33b43f12013-08-20 11:48:36 +020077/* BEGIN_CASE */
78void rsa_pkcs1_verify( char *message_hex_string, int padding_mode, int digest,
79 int mod, int radix_N, char *input_N, int radix_E,
80 char *input_E, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000081{
82 unsigned char message_str[1000];
83 unsigned char hash_result[1000];
84 unsigned char result_str[1000];
85 rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000086 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +000087
Paul Bakker33b43f12013-08-20 11:48:36 +020088 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000089 memset( message_str, 0x00, 1000 );
90 memset( hash_result, 0x00, 1000 );
91 memset( result_str, 0x00, 1000 );
92
Paul Bakker33b43f12013-08-20 11:48:36 +020093 ctx.len = mod / 8;
94 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
95 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000096
97 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
98
Paul Bakker33b43f12013-08-20 11:48:36 +020099 msg_len = unhexify( message_str, message_hex_string );
100 unhexify( result_str, result_hex_str );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000101
Paul Bakker33b43f12013-08-20 11:48:36 +0200102 if( md_info_from_type( digest ) != NULL )
103 TEST_ASSERT( md( md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000104
Paul Bakker548957d2013-08-30 10:30:02 +0200105 TEST_ASSERT( rsa_pkcs1_verify( &ctx, NULL, NULL, RSA_PUBLIC, digest, 0, hash_result, result_str ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100106
107 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000108}
Paul Bakker33b43f12013-08-20 11:48:36 +0200109/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000110
Paul Bakker821fb082009-07-12 13:26:42 +0000111
Paul Bakker33b43f12013-08-20 11:48:36 +0200112/* BEGIN_CASE */
113void rsa_pkcs1_sign_raw( char *message_hex_string, char *hash_result_string,
114 int padding_mode, int mod, int radix_P, char *input_P,
115 int radix_Q, char *input_Q, int radix_N,
116 char *input_N, int radix_E, char *input_E,
117 char *result_hex_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000118{
119 unsigned char message_str[1000];
120 unsigned char hash_result[1000];
121 unsigned char output[1000];
122 unsigned char output_str[1000];
123 rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000124 mpi P1, Q1, H, G;
Paul Bakkereaf90d92011-07-13 14:21:52 +0000125 int hash_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200126 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000127
Paul Bakker6c591fa2011-05-05 11:49:20 +0000128 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakker33b43f12013-08-20 11:48:36 +0200129 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000130
Paul Bakker42a29bf2009-07-07 20:18:41 +0000131 memset( message_str, 0x00, 1000 );
132 memset( hash_result, 0x00, 1000 );
133 memset( output, 0x00, 1000 );
134 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200135 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000136
Paul Bakker33b43f12013-08-20 11:48:36 +0200137 ctx.len = mod / 8;
138 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
139 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
140 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
141 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000142
143 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
144 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
145 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
146 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
147 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
148 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
149 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
150 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
151
152 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
153
Paul Bakker33b43f12013-08-20 11:48:36 +0200154 unhexify( message_str, message_hex_string );
155 hash_len = unhexify( hash_result, hash_result_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000156
Paul Bakker548957d2013-08-30 10:30:02 +0200157 TEST_ASSERT( rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, POLARSSL_MD_NONE, hash_len, hash_result, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000158
159 hexify( output_str, output, ctx.len );
160
Paul Bakker33b43f12013-08-20 11:48:36 +0200161 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000162
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100163 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
164 if( padding_mode == RSA_PKCS_V15 )
165 {
166 memset( output, 0x00, 1000 );
167 memset( output_str, 0x00, 1000 );
168
169 TEST_ASSERT( rsa_rsaes_pkcs1_v15_encrypt( &ctx,
170 &rnd_pseudo_rand, &rnd_info, RSA_PRIVATE,
171 hash_len, hash_result, output ) == 0 );
172
173 hexify( output_str, output, ctx.len );
174
175 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
176 }
177
Paul Bakker6c591fa2011-05-05 11:49:20 +0000178 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100179 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000180}
Paul Bakker33b43f12013-08-20 11:48:36 +0200181/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000182
Paul Bakker33b43f12013-08-20 11:48:36 +0200183/* BEGIN_CASE */
184void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string,
185 int padding_mode, int mod, int radix_N,
186 char *input_N, int radix_E, char *input_E,
187 char *result_hex_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000188{
189 unsigned char message_str[1000];
190 unsigned char hash_result[1000];
191 unsigned char result_str[1000];
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100192 unsigned char output[1000];
Paul Bakker821fb082009-07-12 13:26:42 +0000193 rsa_context ctx;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100194 size_t hash_len, olen;
Paul Bakker821fb082009-07-12 13:26:42 +0000195
Paul Bakker33b43f12013-08-20 11:48:36 +0200196 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000197 memset( message_str, 0x00, 1000 );
198 memset( hash_result, 0x00, 1000 );
199 memset( result_str, 0x00, 1000 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100200 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000201
Paul Bakker33b43f12013-08-20 11:48:36 +0200202 ctx.len = mod / 8;
203 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
204 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000205
206 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
207
Paul Bakker33b43f12013-08-20 11:48:36 +0200208 unhexify( message_str, message_hex_string );
209 hash_len = unhexify( hash_result, hash_result_string );
210 unhexify( result_str, result_hex_str );
Paul Bakker821fb082009-07-12 13:26:42 +0000211
Paul Bakker548957d2013-08-30 10:30:02 +0200212 TEST_ASSERT( rsa_pkcs1_verify( &ctx, NULL, NULL, RSA_PUBLIC, POLARSSL_MD_NONE, hash_len, hash_result, result_str ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100213
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100214 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
215 if( padding_mode == RSA_PKCS_V15 )
216 {
217 int ok;
218
219 TEST_ASSERT( rsa_rsaes_pkcs1_v15_decrypt( &ctx,
220 NULL, NULL, RSA_PUBLIC,
221 &olen, result_str, output, sizeof( output ) ) == 0 );
222
223 ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0;
224 if( correct == 0 )
225 TEST_ASSERT( ok == 1 );
226 else
227 TEST_ASSERT( ok == 0 );
228 }
229
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100230 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000231}
Paul Bakker33b43f12013-08-20 11:48:36 +0200232/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000233
Paul Bakker33b43f12013-08-20 11:48:36 +0200234/* BEGIN_CASE */
235void rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod,
236 int radix_N, char *input_N, int radix_E, char *input_E,
237 char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000238{
239 unsigned char message_str[1000];
240 unsigned char output[1000];
241 unsigned char output_str[1000];
242 rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000243 size_t msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000244 rnd_pseudo_info rnd_info;
245
246 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000247
Paul Bakker33b43f12013-08-20 11:48:36 +0200248 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000249 memset( message_str, 0x00, 1000 );
250 memset( output, 0x00, 1000 );
251 memset( output_str, 0x00, 1000 );
252
Paul Bakker33b43f12013-08-20 11:48:36 +0200253 ctx.len = mod / 8;
254 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
255 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000256
257 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
258
Paul Bakker33b43f12013-08-20 11:48:36 +0200259 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000260
Paul Bakker33b43f12013-08-20 11:48:36 +0200261 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PUBLIC, msg_len, message_str, output ) == result );
262 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000263 {
264 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000265
Paul Bakker33b43f12013-08-20 11:48:36 +0200266 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000267 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100268
269 rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000270}
Paul Bakker33b43f12013-08-20 11:48:36 +0200271/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000272
Paul Bakker33b43f12013-08-20 11:48:36 +0200273/* BEGIN_CASE */
274void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode,
275 int mod, int radix_N, char *input_N,
276 int radix_E, char *input_E,
277 char *result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000278{
279 unsigned char message_str[1000];
280 unsigned char output[1000];
281 unsigned char output_str[1000];
282 rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000283 size_t msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000284
Paul Bakker33b43f12013-08-20 11:48:36 +0200285 rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000286 memset( message_str, 0x00, 1000 );
287 memset( output, 0x00, 1000 );
288 memset( output_str, 0x00, 1000 );
289
Paul Bakker33b43f12013-08-20 11:48:36 +0200290 ctx.len = mod / 8;
291 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
292 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000293
294 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
295
Paul Bakker33b43f12013-08-20 11:48:36 +0200296 msg_len = unhexify( message_str, message_hex_string );
Paul Bakkera6656852010-07-18 19:47:14 +0000297
Paul Bakker33b43f12013-08-20 11:48:36 +0200298 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL, RSA_PUBLIC, msg_len, message_str, output ) == result );
299 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000300 {
301 hexify( output_str, output, ctx.len );
302
Paul Bakker33b43f12013-08-20 11:48:36 +0200303 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000304 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100305
306 rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000307}
Paul Bakker33b43f12013-08-20 11:48:36 +0200308/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000309
Paul Bakker33b43f12013-08-20 11:48:36 +0200310/* BEGIN_CASE */
311void rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod,
312 int radix_P, char *input_P, int radix_Q, char *input_Q,
313 int radix_N, char *input_N, int radix_E, char *input_E,
314 int max_output, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000315{
316 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000317 unsigned char output[1000];
318 unsigned char output_str[1000];
319 rsa_context ctx;
320 mpi P1, Q1, H, G;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000321 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200322 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000323
Paul Bakker6c591fa2011-05-05 11:49:20 +0000324 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakker33b43f12013-08-20 11:48:36 +0200325 rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000326
327 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000328 memset( output, 0x00, 1000 );
329 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200330 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000331
Paul Bakker33b43f12013-08-20 11:48:36 +0200332 ctx.len = mod / 8;
333 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
334 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
335 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
336 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000337
338 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
339 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
340 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
341 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
342 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
343 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
344 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
345 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
346
347 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
348
Paul Bakker33b43f12013-08-20 11:48:36 +0200349 unhexify( message_str, message_hex_string );
Paul Bakker69998dd2009-07-11 19:15:20 +0000350 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000351
Paul Bakker548957d2013-08-30 10:30:02 +0200352 TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, &output_len, message_str, output, max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200353 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000354 {
355 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000356
Paul Bakker33b43f12013-08-20 11:48:36 +0200357 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000358 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000359
360 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100361 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000362}
Paul Bakker33b43f12013-08-20 11:48:36 +0200363/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000364
Paul Bakker33b43f12013-08-20 11:48:36 +0200365/* BEGIN_CASE */
366void rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N,
367 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000368{
369 unsigned char message_str[1000];
370 unsigned char output[1000];
371 unsigned char output_str[1000];
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100372 rsa_context ctx, ctx2; /* Also test rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000373
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000374 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100375 rsa_init( &ctx2, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000376 memset( message_str, 0x00, 1000 );
377 memset( output, 0x00, 1000 );
378 memset( output_str, 0x00, 1000 );
379
Paul Bakker33b43f12013-08-20 11:48:36 +0200380 ctx.len = mod / 8;
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 );
Paul Bakker821fb082009-07-12 13:26:42 +0000383
384 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
385
Paul Bakker33b43f12013-08-20 11:48:36 +0200386 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000387
Paul Bakker33b43f12013-08-20 11:48:36 +0200388 TEST_ASSERT( rsa_public( &ctx, message_str, output ) == result );
389 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000390 {
391 hexify( output_str, output, ctx.len );
392
Paul Bakker33b43f12013-08-20 11:48:36 +0200393 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000394 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100395
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100396 /* And now with the copy */
397 TEST_ASSERT( rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100398 rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100399
400 TEST_ASSERT( rsa_check_pubkey( &ctx2 ) == 0 );
401
402 memset( output, 0x00, 1000 );
403 memset( output_str, 0x00, 1000 );
404 TEST_ASSERT( rsa_public( &ctx2, message_str, output ) == result );
405 if( result == 0 )
406 {
407 hexify( output_str, output, ctx2.len );
408
409 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
410 }
411
412 rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000413}
Paul Bakker33b43f12013-08-20 11:48:36 +0200414/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000415
Paul Bakker33b43f12013-08-20 11:48:36 +0200416/* BEGIN_CASE */
417void rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
418 int radix_Q, char *input_Q, int radix_N, char *input_N,
419 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000420{
421 unsigned char message_str[1000];
422 unsigned char output[1000];
423 unsigned char output_str[1000];
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100424 rsa_context ctx, ctx2; /* Also test rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000425 mpi P1, Q1, H, G;
Paul Bakker548957d2013-08-30 10:30:02 +0200426 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200427 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000428
Paul Bakker6c591fa2011-05-05 11:49:20 +0000429 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000430 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100431 rsa_init( &ctx2, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000432
433 memset( message_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200434 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000435
Paul Bakker33b43f12013-08-20 11:48:36 +0200436 ctx.len = mod / 8;
437 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
438 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
439 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
440 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000441
442 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
443 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
444 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
445 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
446 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
447 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
448 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
449 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
450
451 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
452
Paul Bakker33b43f12013-08-20 11:48:36 +0200453 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000454
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200455 /* repeat three times to test updating of blinding values */
456 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000457 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200458 memset( output, 0x00, 1000 );
459 memset( output_str, 0x00, 1000 );
460 TEST_ASSERT( rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
461 message_str, output ) == result );
462 if( result == 0 )
463 {
464 hexify( output_str, output, ctx.len );
Paul Bakker821fb082009-07-12 13:26:42 +0000465
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200466 TEST_ASSERT( strcasecmp( (char *) output_str,
467 result_hex_str ) == 0 );
468 }
Paul Bakker821fb082009-07-12 13:26:42 +0000469 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000470
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100471 /* And now one more time with the copy */
472 TEST_ASSERT( rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100473 rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100474
475 TEST_ASSERT( rsa_check_privkey( &ctx2 ) == 0 );
476
477 memset( output, 0x00, 1000 );
478 memset( output_str, 0x00, 1000 );
479 TEST_ASSERT( rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
480 message_str, output ) == result );
481 if( result == 0 )
482 {
483 hexify( output_str, output, ctx2.len );
484
485 TEST_ASSERT( strcasecmp( (char *) output_str,
486 result_hex_str ) == 0 );
487 }
488
489 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
490 rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000491}
Paul Bakker33b43f12013-08-20 11:48:36 +0200492/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000493
Paul Bakker33b43f12013-08-20 11:48:36 +0200494/* BEGIN_CASE */
495void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000496{
497 rsa_context ctx;
498 memset( &ctx, 0x00, sizeof( rsa_context ) );
499
500 TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
501}
Paul Bakker33b43f12013-08-20 11:48:36 +0200502/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000503
Paul Bakker33b43f12013-08-20 11:48:36 +0200504/* BEGIN_CASE */
505void rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
506 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000507{
508 rsa_context ctx;
509
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000510 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000511
Paul Bakker33b43f12013-08-20 11:48:36 +0200512 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000513 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200514 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000515 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200516 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000517 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200518 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000519 }
520
Paul Bakker33b43f12013-08-20 11:48:36 +0200521 TEST_ASSERT( rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100522
523 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000524}
Paul Bakker33b43f12013-08-20 11:48:36 +0200525/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000526
Paul Bakker33b43f12013-08-20 11:48:36 +0200527/* BEGIN_CASE */
528void rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
529 char *input_Q, int radix_N, char *input_N,
530 int radix_E, char *input_E, int radix_D, char *input_D,
531 int radix_DP, char *input_DP, int radix_DQ,
532 char *input_DQ, int radix_QP, char *input_QP,
533 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000534{
535 rsa_context ctx;
536
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000537 rsa_init( &ctx, RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000538
Paul Bakker33b43f12013-08-20 11:48:36 +0200539 ctx.len = mod / 8;
540 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000541 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200542 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000543 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200544 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000545 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200546 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000547 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200548 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000549 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200550 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000551 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200552 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000553 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200554 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000555 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200556 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000557 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200558 TEST_ASSERT( mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000559 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200560 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000561 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200562 TEST_ASSERT( mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000563 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200564 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000565 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200566 TEST_ASSERT( mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000567 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200568 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000569 {
Paul Bakker33b43f12013-08-20 11:48:36 +0200570 TEST_ASSERT( mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000571 }
Paul Bakker821fb082009-07-12 13:26:42 +0000572
Paul Bakker33b43f12013-08-20 11:48:36 +0200573 TEST_ASSERT( rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100574
575 rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000576}
Paul Bakker33b43f12013-08-20 11:48:36 +0200577/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000578
Manuel Pégourié-Gonnarda0d758b2013-12-01 16:27:00 +0100579/* BEGIN_CASE depends_on:POLARSSL_CTR_DRBG_C:POLARSSL_ENTROPY_C */
Paul Bakker33b43f12013-08-20 11:48:36 +0200580void rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000581{
582 rsa_context ctx;
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000583 entropy_context entropy;
584 ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200585 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000586
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000587 entropy_init( &entropy );
588 TEST_ASSERT( ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200589 (const unsigned char *) pers, strlen( pers ) ) == 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000590
Paul Bakkerebcef6d2010-08-16 11:10:49 +0000591 rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000592
Paul Bakker33b43f12013-08-20 11:48:36 +0200593 TEST_ASSERT( rsa_gen_key( &ctx, ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
594 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000595 {
596 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
597 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100598
599 rsa_free( &ctx );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200600 entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000601}
Paul Bakker33b43f12013-08-20 11:48:36 +0200602/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000603
Manuel Pégourié-Gonnard20140162013-10-10 12:48:03 +0200604/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200605void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +0000606{
607 TEST_ASSERT( rsa_self_test( 0 ) == 0 );
608}
Paul Bakker33b43f12013-08-20 11:48:36 +0200609/* END_CASE */