blob: a6bd7c124c9b6ada976134da0f6f95b4ecb5e466 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/rsa.h"
3#include "mbedtls/md2.h"
4#include "mbedtls/md4.h"
5#include "mbedtls/md5.h"
6#include "mbedtls/sha1.h"
7#include "mbedtls/sha256.h"
8#include "mbedtls/sha512.h"
9#include "mbedtls/entropy.h"
10#include "mbedtls/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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +020015 * END_DEPENDENCIES
16 */
Paul Bakker5690efc2011-05-26 13:16:06 +000017
Paul Bakker33b43f12013-08-20 11:48:36 +020018/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019void mbedtls_rsa_pkcs1_sign( char *message_hex_string, int padding_mode, int digest,
Paul Bakker33b43f12013-08-20 11:48:36 +020020 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];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028 mbedtls_rsa_context ctx;
29 mbedtls_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
34 mbedtls_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;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
44 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
45 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
46 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
49 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
50 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
51 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
52 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
53 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
54 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
55 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000058
Paul Bakker33b43f12013-08-20 11:48:36 +020059 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 if( mbedtls_md_info_from_type( digest ) != NULL )
Hanno Beckerf8b56d42017-10-05 10:16:37 +010062 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ),
63 message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000064
Hanno Beckerf8b56d42017-10-05 10:16:37 +010065 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
66 MBEDTLS_RSA_PRIVATE, digest, 0,
67 hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020068 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +000069 {
70 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +000071
Paul Bakker33b43f12013-08-20 11:48:36 +020072 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +000073 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000074
Paul Bakkerbd51b262014-07-10 15:26:12 +020075exit:
Hanno Beckerf8b56d42017-10-05 10:16:37 +010076 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
77 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +000079}
Paul Bakker33b43f12013-08-20 11:48:36 +020080/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +000081
Paul Bakker33b43f12013-08-20 11:48:36 +020082/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_rsa_pkcs1_verify( char *message_hex_string, int padding_mode, int digest,
Paul Bakker33b43f12013-08-20 11:48:36 +020084 int mod, int radix_N, char *input_N, int radix_E,
85 char *input_E, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000086{
87 unsigned char message_str[1000];
88 unsigned char hash_result[1000];
89 unsigned char result_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000091 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +000092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000094 memset( message_str, 0x00, 1000 );
95 memset( hash_result, 0x00, 1000 );
96 memset( result_str, 0x00, 1000 );
97
Paul Bakker33b43f12013-08-20 11:48:36 +020098 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
100 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000103
Paul Bakker33b43f12013-08-20 11:48:36 +0200104 msg_len = unhexify( message_str, message_hex_string );
105 unhexify( result_str, result_hex_str );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 if( mbedtls_md_info_from_type( digest ) != NULL )
108 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100111
Paul Bakkerbd51b262014-07-10 15:26:12 +0200112exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000114}
Paul Bakker33b43f12013-08-20 11:48:36 +0200115/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000116
Paul Bakker821fb082009-07-12 13:26:42 +0000117
Paul Bakker33b43f12013-08-20 11:48:36 +0200118/* BEGIN_CASE */
119void rsa_pkcs1_sign_raw( char *message_hex_string, char *hash_result_string,
120 int padding_mode, int mod, int radix_P, char *input_P,
121 int radix_Q, char *input_Q, int radix_N,
122 char *input_N, int radix_E, char *input_E,
123 char *result_hex_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000124{
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100125 int res;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000126 unsigned char message_str[1000];
127 unsigned char hash_result[1000];
128 unsigned char output[1000];
129 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_rsa_context ctx;
131 mbedtls_mpi P1, Q1, H, G;
Paul Bakkereaf90d92011-07-13 14:21:52 +0000132 int hash_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200133 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
136 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000137
Paul Bakker42a29bf2009-07-07 20:18:41 +0000138 memset( message_str, 0x00, 1000 );
139 memset( hash_result, 0x00, 1000 );
140 memset( output, 0x00, 1000 );
141 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200142 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000143
Paul Bakker33b43f12013-08-20 11:48:36 +0200144 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
146 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
147 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
148 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
151 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
152 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
153 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
154 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
155 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
156 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
157 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000160
Paul Bakker33b43f12013-08-20 11:48:36 +0200161 unhexify( message_str, message_hex_string );
162 hash_len = unhexify( hash_result, hash_result_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000163
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100164 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
165 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
166 hash_len, hash_result, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000167
168 hexify( output_str, output, ctx.len );
169
Paul Bakker33b43f12013-08-20 11:48:36 +0200170 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000171
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100172 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100174 {
175 memset( output, 0x00, 1000 );
176 memset( output_str, 0x00, 1000 );
177
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100178 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100180 hash_len, hash_result, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100181
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100182#if !defined(MBEDTLS_RSA_ALT)
183 TEST_ASSERT( res == 0 );
184#else
185 TEST_ASSERT( ( res == 0 ) ||
186 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
187#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100188
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100189 if( res == 0 )
190 {
191 hexify( output_str, output, ctx.len );
192 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
193 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100194 }
195
Paul Bakkerbd51b262014-07-10 15:26:12 +0200196exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
198 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000199}
Paul Bakker33b43f12013-08-20 11:48:36 +0200200/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000201
Paul Bakker33b43f12013-08-20 11:48:36 +0200202/* BEGIN_CASE */
203void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string,
204 int padding_mode, int mod, int radix_N,
205 char *input_N, int radix_E, char *input_E,
206 char *result_hex_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000207{
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100208 int res;
Paul Bakker821fb082009-07-12 13:26:42 +0000209 unsigned char message_str[1000];
210 unsigned char hash_result[1000];
211 unsigned char result_str[1000];
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100212 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_rsa_context ctx;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100214 size_t hash_len, olen;
Paul Bakker821fb082009-07-12 13:26:42 +0000215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000217 memset( message_str, 0x00, 1000 );
218 memset( hash_result, 0x00, 1000 );
219 memset( result_str, 0x00, 1000 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100220 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000221
Paul Bakker33b43f12013-08-20 11:48:36 +0200222 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
224 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000227
Paul Bakker33b43f12013-08-20 11:48:36 +0200228 unhexify( message_str, message_hex_string );
229 hash_len = unhexify( hash_result, hash_result_string );
230 unhexify( result_str, result_hex_str );
Paul Bakker821fb082009-07-12 13:26:42 +0000231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE, hash_len, hash_result, result_str ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100233
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100234 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100236 {
237 int ok;
238
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100239 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100241 &olen, result_str, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100242
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100243#if !defined(MBEDTLS_RSA_ALT)
244 TEST_ASSERT( res == 0 );
245#else
246 TEST_ASSERT( ( res == 0 ) ||
247 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
248#endif
249
250 if( res == 0 )
251 {
252 ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0;
253 if( correct == 0 )
254 TEST_ASSERT( ok == 1 );
255 else
256 TEST_ASSERT( ok == 0 );
257 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100258 }
259
Paul Bakkerbd51b262014-07-10 15:26:12 +0200260exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000262}
Paul Bakker33b43f12013-08-20 11:48:36 +0200263/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000264
Paul Bakker33b43f12013-08-20 11:48:36 +0200265/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266void mbedtls_rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200267 int radix_N, char *input_N, int radix_E, char *input_E,
268 char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000269{
270 unsigned char message_str[1000];
271 unsigned char output[1000];
272 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000274 size_t msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000275 rnd_pseudo_info rnd_info;
276
277 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000280 memset( message_str, 0x00, 1000 );
281 memset( output, 0x00, 1000 );
282 memset( output_str, 0x00, 1000 );
283
Paul Bakker33b43f12013-08-20 11:48:36 +0200284 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
286 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000289
Paul Bakker33b43f12013-08-20 11:48:36 +0200290 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000291
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100292 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
293 MBEDTLS_RSA_PUBLIC, msg_len,
294 message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200295 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000296 {
297 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000298
Paul Bakker33b43f12013-08-20 11:48:36 +0200299 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000300 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100301
Paul Bakkerbd51b262014-07-10 15:26:12 +0200302exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000304}
Paul Bakker33b43f12013-08-20 11:48:36 +0200305/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000306
Paul Bakker33b43f12013-08-20 11:48:36 +0200307/* BEGIN_CASE */
308void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode,
309 int mod, int radix_N, char *input_N,
310 int radix_E, char *input_E,
311 char *result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000312{
313 unsigned char message_str[1000];
314 unsigned char output[1000];
315 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000317 size_t msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000320 memset( message_str, 0x00, 1000 );
321 memset( output, 0x00, 1000 );
322 memset( output_str, 0x00, 1000 );
323
Paul Bakker33b43f12013-08-20 11:48:36 +0200324 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
326 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000329
Paul Bakker33b43f12013-08-20 11:48:36 +0200330 msg_len = unhexify( message_str, message_hex_string );
Paul Bakkera6656852010-07-18 19:47:14 +0000331
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100332 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL,
333 MBEDTLS_RSA_PUBLIC, msg_len,
334 message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200335 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000336 {
337 hexify( output_str, output, ctx.len );
338
Paul Bakker33b43f12013-08-20 11:48:36 +0200339 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000340 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100341
Paul Bakkerbd51b262014-07-10 15:26:12 +0200342exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000344}
Paul Bakker33b43f12013-08-20 11:48:36 +0200345/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000346
Paul Bakker33b43f12013-08-20 11:48:36 +0200347/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348void mbedtls_rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200349 int radix_P, char *input_P, int radix_Q, char *input_Q,
350 int radix_N, char *input_N, int radix_E, char *input_E,
351 int max_output, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000352{
353 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000354 unsigned char output[1000];
355 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_rsa_context ctx;
357 mbedtls_mpi P1, Q1, H, G;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000358 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200359 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
362 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000363
364 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000365 memset( output, 0x00, 1000 );
366 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200367 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000368
Paul Bakker33b43f12013-08-20 11:48:36 +0200369 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
371 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
372 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
373 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
376 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
377 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
378 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
379 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
380 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
381 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
382 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000385
Paul Bakker33b43f12013-08-20 11:48:36 +0200386 unhexify( message_str, message_hex_string );
Paul Bakker69998dd2009-07-11 19:15:20 +0000387 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str, output, max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200390 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000391 {
392 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000393
Paul Bakker33b43f12013-08-20 11:48:36 +0200394 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000395 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000396
Paul Bakkerbd51b262014-07-10 15:26:12 +0200397exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
399 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000400}
Paul Bakker33b43f12013-08-20 11:48:36 +0200401/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000402
Paul Bakker33b43f12013-08-20 11:48:36 +0200403/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404void mbedtls_rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N,
Paul Bakker33b43f12013-08-20 11:48:36 +0200405 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000406{
407 unsigned char message_str[1000];
408 unsigned char output[1000];
409 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
413 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000414 memset( message_str, 0x00, 1000 );
415 memset( output, 0x00, 1000 );
416 memset( output_str, 0x00, 1000 );
417
Paul Bakker33b43f12013-08-20 11:48:36 +0200418 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
420 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000423
Paul Bakker33b43f12013-08-20 11:48:36 +0200424 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200427 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000428 {
429 hexify( output_str, output, ctx.len );
430
Paul Bakker33b43f12013-08-20 11:48:36 +0200431 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000432 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100433
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100434 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200436 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100440
441 memset( output, 0x00, 1000 );
442 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100444 if( result == 0 )
445 {
446 hexify( output_str, output, ctx2.len );
447
448 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
449 }
450
Paul Bakkerbd51b262014-07-10 15:26:12 +0200451exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_rsa_free( &ctx );
453 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000454}
Paul Bakker33b43f12013-08-20 11:48:36 +0200455/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000456
Paul Bakker33b43f12013-08-20 11:48:36 +0200457/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458void mbedtls_rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
Paul Bakker33b43f12013-08-20 11:48:36 +0200459 int radix_Q, char *input_Q, int radix_N, char *input_N,
460 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000461{
462 unsigned char message_str[1000];
463 unsigned char output[1000];
464 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
466 mbedtls_mpi P1, Q1, H, G;
Paul Bakker548957d2013-08-30 10:30:02 +0200467 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200468 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
471 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
472 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000473
474 memset( message_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200475 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000476
Paul Bakker33b43f12013-08-20 11:48:36 +0200477 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
479 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
480 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
481 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
484 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
485 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
486 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
487 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
488 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
489 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
490 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000493
Paul Bakker33b43f12013-08-20 11:48:36 +0200494 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000495
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200496 /* repeat three times to test updating of blinding values */
497 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000498 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200499 memset( output, 0x00, 1000 );
500 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200502 message_str, output ) == result );
503 if( result == 0 )
504 {
505 hexify( output_str, output, ctx.len );
Paul Bakker821fb082009-07-12 13:26:42 +0000506
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200507 TEST_ASSERT( strcasecmp( (char *) output_str,
508 result_hex_str ) == 0 );
509 }
Paul Bakker821fb082009-07-12 13:26:42 +0000510 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000511
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100512 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200514 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100518
519 memset( output, 0x00, 1000 );
520 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100522 message_str, output ) == result );
523 if( result == 0 )
524 {
525 hexify( output_str, output, ctx2.len );
526
527 TEST_ASSERT( strcasecmp( (char *) output_str,
528 result_hex_str ) == 0 );
529 }
530
Paul Bakkerbd51b262014-07-10 15:26:12 +0200531exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
533 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000534}
Paul Bakker33b43f12013-08-20 11:48:36 +0200535/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000536
Paul Bakker33b43f12013-08-20 11:48:36 +0200537/* BEGIN_CASE */
538void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000539{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_rsa_context ctx;
541 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000544}
Paul Bakker33b43f12013-08-20 11:48:36 +0200545/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000546
Paul Bakker33b43f12013-08-20 11:48:36 +0200547/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548void mbedtls_rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
Paul Bakker33b43f12013-08-20 11:48:36 +0200549 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000554
Paul Bakker33b43f12013-08-20 11:48:36 +0200555 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000558 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200559 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000562 }
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100565
Paul Bakkerbd51b262014-07-10 15:26:12 +0200566exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000568}
Paul Bakker33b43f12013-08-20 11:48:36 +0200569/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000570
Paul Bakker33b43f12013-08-20 11:48:36 +0200571/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572void mbedtls_rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
Paul Bakker33b43f12013-08-20 11:48:36 +0200573 char *input_Q, int radix_N, char *input_N,
574 int radix_E, char *input_E, int radix_D, char *input_D,
575 int radix_DP, char *input_DP, int radix_DQ,
576 char *input_DQ, int radix_QP, char *input_QP,
577 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000578{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000582
Paul Bakker33b43f12013-08-20 11:48:36 +0200583 ctx.len = mod / 8;
584 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000587 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200588 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000591 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200592 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000595 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200596 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000599 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200600 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000603 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200604 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000607 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200608 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000611 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200612 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000615 }
Paul Bakker821fb082009-07-12 13:26:42 +0000616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100618
Paul Bakkerbd51b262014-07-10 15:26:12 +0200619exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000621}
Paul Bakker33b43f12013-08-20 11:48:36 +0200622/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000623
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100624/* BEGIN_CASE */
625void rsa_check_pubpriv( int mod, int radix_Npub, char *input_Npub,
626 int radix_Epub, char *input_Epub,
627 int radix_P, char *input_P, int radix_Q,
628 char *input_Q, int radix_N, char *input_N,
629 int radix_E, char *input_E, int radix_D, char *input_D,
630 int radix_DP, char *input_DP, int radix_DQ,
631 char *input_DQ, int radix_QP, char *input_QP,
632 int result )
633{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
637 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100638
639 pub.len = mod / 8;
640 prv.len = mod / 8;
641
642 if( strlen( input_Npub ) )
643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100645 }
646 if( strlen( input_Epub ) )
647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100649 }
650
651 if( strlen( input_P ) )
652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100654 }
655 if( strlen( input_Q ) )
656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100658 }
659 if( strlen( input_N ) )
660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100662 }
663 if( strlen( input_E ) )
664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100666 }
667 if( strlen( input_D ) )
668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100670 }
671 if( strlen( input_DP ) )
672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100674 }
675 if( strlen( input_DQ ) )
676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100678 }
679 if( strlen( input_QP ) )
680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100682 }
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100685
686exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_rsa_free( &pub );
688 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100689}
690/* END_CASE */
691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
693void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000694{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_rsa_context ctx;
696 mbedtls_entropy_context entropy;
697 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200698 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000699
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200700 mbedtls_ctr_drbg_init( &ctr_drbg );
701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200703 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200704 (const unsigned char *) pers, strlen( pers ) ) == 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200709 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100712 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000713 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100714
Paul Bakkerbd51b262014-07-10 15:26:12 +0200715exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_rsa_free( &ctx );
717 mbedtls_ctr_drbg_free( &ctr_drbg );
718 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000719}
Paul Bakker33b43f12013-08-20 11:48:36 +0200720/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200723void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +0000724{
Andres AG93012e82016-09-09 09:10:28 +0100725 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000726}
Paul Bakker33b43f12013-08-20 11:48:36 +0200727/* END_CASE */