blob: 953c6338f9eb15cece079c5f757d2710ee0f7968 [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"
Hanno Beckera565f542017-10-11 11:00:19 +01003#include "mbedtls/rsa_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/md2.h"
5#include "mbedtls/md4.h"
6#include "mbedtls/md5.h"
7#include "mbedtls/sha1.h"
8#include "mbedtls/sha256.h"
9#include "mbedtls/sha512.h"
10#include "mbedtls/entropy.h"
11#include "mbedtls/ctr_drbg.h"
Hanno Becker47deec42017-07-24 12:27:09 +010012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +000014
Paul Bakker33b43f12013-08-20 11:48:36 +020015/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +020017 * END_DEPENDENCIES
18 */
Paul Bakker5690efc2011-05-26 13:16:06 +000019
Paul Bakker33b43f12013-08-20 11:48:36 +020020/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021void mbedtls_rsa_pkcs1_sign( char *message_hex_string, int padding_mode, int digest,
Paul Bakker33b43f12013-08-20 11:48:36 +020022 int mod, int radix_P, char *input_P, int radix_Q,
23 char *input_Q, int radix_N, char *input_N, int radix_E,
24 char *input_E, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000025{
26 unsigned char message_str[1000];
27 unsigned char hash_result[1000];
28 unsigned char output[1000];
29 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010031 mbedtls_mpi N, P, Q, E;
Paul Bakker69998dd2009-07-11 19:15:20 +000032 int msg_len;
Paul Bakker548957d2013-08-30 10:30:02 +020033 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +000034
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010035 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
36 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000038
39 memset( message_str, 0x00, 1000 );
40 memset( hash_result, 0x00, 1000 );
41 memset( output, 0x00, 1000 );
42 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +020043 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +000044
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010045 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
46 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
47 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
48 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000049
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010050 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
51 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +010052 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000054
Paul Bakker33b43f12013-08-20 11:48:36 +020055 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 if( mbedtls_md_info_from_type( digest ) != NULL )
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010058 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ),
59 message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000060
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010061 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
62 MBEDTLS_RSA_PRIVATE, digest, 0,
63 hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020064 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +000065 {
66 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +000067
Paul Bakker33b43f12013-08-20 11:48:36 +020068 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +000069 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000070
Paul Bakkerbd51b262014-07-10 15:26:12 +020071exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010072 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
73 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +000075}
Paul Bakker33b43f12013-08-20 11:48:36 +020076/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +000077
Paul Bakker33b43f12013-08-20 11:48:36 +020078/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079void mbedtls_rsa_pkcs1_verify( char *message_hex_string, int padding_mode, int digest,
Paul Bakker33b43f12013-08-20 11:48:36 +020080 int mod, int radix_N, char *input_N, int radix_E,
81 char *input_E, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000082{
83 unsigned char message_str[1000];
84 unsigned char hash_result[1000];
85 unsigned char result_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000087 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +000088
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010089 mbedtls_mpi N, E;
90
91 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000093 memset( message_str, 0x00, 1000 );
94 memset( hash_result, 0x00, 1000 );
95 memset( result_str, 0x00, 1000 );
96
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010097 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
98 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
99 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
100 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000102
Paul Bakker33b43f12013-08-20 11:48:36 +0200103 msg_len = unhexify( message_str, message_hex_string );
104 unhexify( result_str, result_hex_str );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 if( mbedtls_md_info_from_type( digest ) != NULL )
107 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 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 +0100110
Paul Bakkerbd51b262014-07-10 15:26:12 +0200111exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100112 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
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;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100131 mbedtls_mpi N, P, Q, E;
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_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100136 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
137 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000138
Paul Bakker42a29bf2009-07-07 20:18:41 +0000139 memset( message_str, 0x00, 1000 );
140 memset( hash_result, 0x00, 1000 );
141 memset( output, 0x00, 1000 );
142 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200143 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000144
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100145 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
146 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
147 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
148 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000149
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100150 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
151 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100152 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000154
Paul Bakker33b43f12013-08-20 11:48:36 +0200155 unhexify( message_str, message_hex_string );
156 hash_len = unhexify( hash_result, hash_result_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000157
Hanno Becker8fd55482017-08-23 14:07:48 +0100158 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
159 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
160 hash_len, hash_result, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000161
162 hexify( output_str, output, ctx.len );
163
Paul Bakker33b43f12013-08-20 11:48:36 +0200164 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000165
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200166#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100167 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100169 {
170 memset( output, 0x00, 1000 );
171 memset( output_str, 0x00, 1000 );
172
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100173 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100175 hash_len, hash_result, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100176
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100177#if !defined(MBEDTLS_RSA_ALT)
178 TEST_ASSERT( res == 0 );
179#else
180 TEST_ASSERT( ( res == 0 ) ||
181 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
182#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100183
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100184 if( res == 0 )
185 {
186 hexify( output_str, output, ctx.len );
187 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
188 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100189 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200190#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100191
Paul Bakkerbd51b262014-07-10 15:26:12 +0200192exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100193 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
194 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000197}
Paul Bakker33b43f12013-08-20 11:48:36 +0200198/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000199
Paul Bakker33b43f12013-08-20 11:48:36 +0200200/* BEGIN_CASE */
201void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string,
202 int padding_mode, int mod, int radix_N,
203 char *input_N, int radix_E, char *input_E,
204 char *result_hex_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000205{
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100206 int res;
Paul Bakker821fb082009-07-12 13:26:42 +0000207 unsigned char message_str[1000];
208 unsigned char hash_result[1000];
209 unsigned char result_str[1000];
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100210 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_rsa_context ctx;
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200212 size_t hash_len;
Paul Bakker821fb082009-07-12 13:26:42 +0000213
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100214 mbedtls_mpi N, E;
215 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000218 memset( message_str, 0x00, 1000 );
219 memset( hash_result, 0x00, 1000 );
220 memset( result_str, 0x00, 1000 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100221 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000222
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100223 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
224 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000225
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100226 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
227 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000229
Paul Bakker33b43f12013-08-20 11:48:36 +0200230 unhexify( message_str, message_hex_string );
231 hash_len = unhexify( hash_result, hash_result_string );
232 unhexify( result_str, result_hex_str );
Paul Bakker821fb082009-07-12 13:26:42 +0000233
Hanno Becker8fd55482017-08-23 14:07:48 +0100234 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
235 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE,
236 hash_len, hash_result,
237 result_str ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100238
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200239#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100240 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100242 {
243 int ok;
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200244 size_t olen;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100245
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100246 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100248 &olen, result_str, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100249
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100250#if !defined(MBEDTLS_RSA_ALT)
251 TEST_ASSERT( res == 0 );
252#else
253 TEST_ASSERT( ( res == 0 ) ||
254 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
255#endif
256
257 if( res == 0 )
258 {
259 ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0;
260 if( correct == 0 )
261 TEST_ASSERT( ok == 1 );
262 else
263 TEST_ASSERT( ok == 0 );
264 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100265 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200266#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100267
Paul Bakkerbd51b262014-07-10 15:26:12 +0200268exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100269 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000271}
Paul Bakker33b43f12013-08-20 11:48:36 +0200272/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000273
Paul Bakker33b43f12013-08-20 11:48:36 +0200274/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275void mbedtls_rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200276 int radix_N, char *input_N, int radix_E, char *input_E,
277 char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000278{
279 unsigned char message_str[1000];
280 unsigned char output[1000];
281 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000283 size_t msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000284 rnd_pseudo_info rnd_info;
285
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100286 mbedtls_mpi N, E;
287 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
288
Paul Bakker997bbd12011-03-13 15:45:42 +0000289 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000292 memset( message_str, 0x00, 1000 );
293 memset( output, 0x00, 1000 );
294 memset( output_str, 0x00, 1000 );
295
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100296 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
297 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000298
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100299 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
300 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000302
Paul Bakker33b43f12013-08-20 11:48:36 +0200303 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000304
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100305 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
306 MBEDTLS_RSA_PUBLIC, msg_len,
307 message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200308 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000309 {
310 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000311
Paul Bakker33b43f12013-08-20 11:48:36 +0200312 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000313 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100314
Paul Bakkerbd51b262014-07-10 15:26:12 +0200315exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100316 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000318}
Paul Bakker33b43f12013-08-20 11:48:36 +0200319/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000320
Paul Bakker33b43f12013-08-20 11:48:36 +0200321/* BEGIN_CASE */
322void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode,
323 int mod, int radix_N, char *input_N,
324 int radix_E, char *input_E,
325 char *result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000326{
327 unsigned char message_str[1000];
328 unsigned char output[1000];
329 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000331 size_t msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000332
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100333 mbedtls_mpi N, E;
334
335 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000337 memset( message_str, 0x00, 1000 );
338 memset( output, 0x00, 1000 );
339 memset( output_str, 0x00, 1000 );
340
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100341 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
342 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000343
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100344 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
345 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000347
Paul Bakker33b43f12013-08-20 11:48:36 +0200348 msg_len = unhexify( message_str, message_hex_string );
Paul Bakkera6656852010-07-18 19:47:14 +0000349
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100350 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL,
351 MBEDTLS_RSA_PUBLIC, msg_len,
352 message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200353 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000354 {
355 hexify( output_str, output, ctx.len );
356
Paul Bakker33b43f12013-08-20 11:48:36 +0200357 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000358 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100359
Paul Bakkerbd51b262014-07-10 15:26:12 +0200360exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100361 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000363}
Paul Bakker33b43f12013-08-20 11:48:36 +0200364/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000365
Paul Bakker33b43f12013-08-20 11:48:36 +0200366/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367void mbedtls_rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200368 int radix_P, char *input_P, int radix_Q, char *input_Q,
369 int radix_N, char *input_N, int radix_E, char *input_E,
370 int max_output, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000371{
372 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000373 unsigned char output[1000];
374 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000376 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200377 rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100378 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000379
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100380 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
381 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000384
385 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000386 memset( output, 0x00, 1000 );
387 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200388 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000389
Paul Bakker42a29bf2009-07-07 20:18:41 +0000390
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100391 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
392 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
393 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
394 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000395
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100396 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
397 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100398 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000400
Paul Bakker33b43f12013-08-20 11:48:36 +0200401 unhexify( message_str, message_hex_string );
Paul Bakker69998dd2009-07-11 19:15:20 +0000402 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 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 +0200405 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000406 {
407 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000408
Paul Bakker33b43f12013-08-20 11:48:36 +0200409 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000410 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000411
Paul Bakkerbd51b262014-07-10 15:26:12 +0200412exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100413 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
414 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000416}
Paul Bakker33b43f12013-08-20 11:48:36 +0200417/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000418
Paul Bakker33b43f12013-08-20 11:48:36 +0200419/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420void mbedtls_rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N,
Paul Bakker33b43f12013-08-20 11:48:36 +0200421 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000422{
423 unsigned char message_str[1000];
424 unsigned char output[1000];
425 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000427
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100428 mbedtls_mpi N, E;
429
430 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
432 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000433 memset( message_str, 0x00, 1000 );
434 memset( output, 0x00, 1000 );
435 memset( output_str, 0x00, 1000 );
436
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100437 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
438 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000439
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100440 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
441 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000443
Paul Bakker33b43f12013-08-20 11:48:36 +0200444 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200447 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000448 {
449 hexify( output_str, output, ctx.len );
450
Paul Bakker33b43f12013-08-20 11:48:36 +0200451 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000452 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100453
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100454 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200456 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100460
461 memset( output, 0x00, 1000 );
462 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100464 if( result == 0 )
465 {
466 hexify( output_str, output, ctx2.len );
467
468 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
469 }
470
Paul Bakkerbd51b262014-07-10 15:26:12 +0200471exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100472 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_rsa_free( &ctx );
474 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000475}
Paul Bakker33b43f12013-08-20 11:48:36 +0200476/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000477
Paul Bakker33b43f12013-08-20 11:48:36 +0200478/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479void mbedtls_rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
Paul Bakker33b43f12013-08-20 11:48:36 +0200480 int radix_Q, char *input_Q, int radix_N, char *input_N,
481 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000482{
483 unsigned char message_str[1000];
484 unsigned char output[1000];
485 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100487 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200488 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200489 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000490
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100491 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
492 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
494 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000495
496 memset( message_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200497 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000498
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100499 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
500 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
501 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
502 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000503
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100504 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
505 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100506 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000508
Paul Bakker33b43f12013-08-20 11:48:36 +0200509 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000510
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200511 /* repeat three times to test updating of blinding values */
512 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000513 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200514 memset( output, 0x00, 1000 );
515 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200517 message_str, output ) == result );
518 if( result == 0 )
519 {
520 hexify( output_str, output, ctx.len );
Paul Bakker821fb082009-07-12 13:26:42 +0000521
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200522 TEST_ASSERT( strcasecmp( (char *) output_str,
523 result_hex_str ) == 0 );
524 }
Paul Bakker821fb082009-07-12 13:26:42 +0000525 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000526
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100527 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200529 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100533
534 memset( output, 0x00, 1000 );
535 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100537 message_str, output ) == result );
538 if( result == 0 )
539 {
540 hexify( output_str, output, ctx2.len );
541
542 TEST_ASSERT( strcasecmp( (char *) output_str,
543 result_hex_str ) == 0 );
544 }
545
Paul Bakkerbd51b262014-07-10 15:26:12 +0200546exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100547 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
548 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000551}
Paul Bakker33b43f12013-08-20 11:48:36 +0200552/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000553
Paul Bakker33b43f12013-08-20 11:48:36 +0200554/* BEGIN_CASE */
555void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000556{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_rsa_context ctx;
558 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000561}
Paul Bakker33b43f12013-08-20 11:48:36 +0200562/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000563
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565void mbedtls_rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
Paul Bakker33b43f12013-08-20 11:48:36 +0200566 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100569 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000570
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100571 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000573
Paul Bakker33b43f12013-08-20 11:48:36 +0200574 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000575 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100576 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000577 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200578 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000579 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100580 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000581 }
582
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100583 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100585
Paul Bakkerbd51b262014-07-10 15:26:12 +0200586exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100587 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000589}
Paul Bakker33b43f12013-08-20 11:48:36 +0200590/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000591
Paul Bakker33b43f12013-08-20 11:48:36 +0200592/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593void mbedtls_rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
Paul Bakker33b43f12013-08-20 11:48:36 +0200594 char *input_Q, int radix_N, char *input_N,
595 int radix_E, char *input_E, int radix_D, char *input_D,
596 int radix_DP, char *input_DP, int radix_DQ,
597 char *input_DQ, int radix_QP, char *input_QP,
598 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000603
Paul Bakker33b43f12013-08-20 11:48:36 +0200604 ctx.len = mod / 8;
605 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000608 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200609 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000612 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200613 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000616 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200617 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000620 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200621 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000624 }
Hanno Becker131134f2017-08-23 08:31:07 +0100625#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200626 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000629 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200630 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000633 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200634 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000637 }
Hanno Becker131134f2017-08-23 08:31:07 +0100638#else
639 ((void) radix_DP); ((void) input_DP);
640 ((void) radix_DQ); ((void) input_DQ);
641 ((void) radix_QP); ((void) input_QP);
642#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100645
Paul Bakkerbd51b262014-07-10 15:26:12 +0200646exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000648}
Paul Bakker33b43f12013-08-20 11:48:36 +0200649/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000650
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100651/* BEGIN_CASE */
652void rsa_check_pubpriv( int mod, int radix_Npub, char *input_Npub,
653 int radix_Epub, char *input_Epub,
654 int radix_P, char *input_P, int radix_Q,
655 char *input_Q, int radix_N, char *input_N,
656 int radix_E, char *input_E, int radix_D, char *input_D,
657 int radix_DP, char *input_DP, int radix_DQ,
658 char *input_DQ, int radix_QP, char *input_QP,
659 int result )
660{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
664 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100665
666 pub.len = mod / 8;
667 prv.len = mod / 8;
668
669 if( strlen( input_Npub ) )
670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100672 }
673 if( strlen( input_Epub ) )
674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100676 }
677
678 if( strlen( input_P ) )
679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100681 }
682 if( strlen( input_Q ) )
683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100685 }
686 if( strlen( input_N ) )
687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100689 }
690 if( strlen( input_E ) )
691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100693 }
694 if( strlen( input_D ) )
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100697 }
Hanno Becker131134f2017-08-23 08:31:07 +0100698#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100699 if( strlen( input_DP ) )
700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100702 }
703 if( strlen( input_DQ ) )
704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100706 }
707 if( strlen( input_QP ) )
708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100710 }
Hanno Becker131134f2017-08-23 08:31:07 +0100711#else
712 ((void) radix_DP); ((void) input_DP);
713 ((void) radix_DQ); ((void) input_DQ);
714 ((void) radix_QP); ((void) input_QP);
715#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100718
719exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_rsa_free( &pub );
721 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100722}
723/* END_CASE */
724
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100725/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000727{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_rsa_context ctx;
729 mbedtls_entropy_context entropy;
730 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200731 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000732
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200733 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +0100735 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000736
Hanno Beckera47023e2017-12-22 17:08:03 +0000737 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
738 &entropy, (const unsigned char *) pers,
739 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200742 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100745 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000746 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100747
Paul Bakkerbd51b262014-07-10 15:26:12 +0200748exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_rsa_free( &ctx );
750 mbedtls_ctr_drbg_free( &ctr_drbg );
751 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000752}
Paul Bakker33b43f12013-08-20 11:48:36 +0200753/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000754
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100755/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +0100756void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100757 int radix_D, char *input_D,
758 int radix_E, char *input_E,
759 int radix_P, char *output_P,
760 int radix_Q, char *output_Q,
761 int corrupt, int result )
762{
763 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
764
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100765 mbedtls_mpi_init( &N );
766 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
767 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
768 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
769
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100770 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
771 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
772 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
773 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
774 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
775
776 if( corrupt )
777 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
778
779 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100780 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100781
782 if( !corrupt )
783 {
784 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
785 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
786 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
787 }
788
789exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100790 mbedtls_mpi_free( &N );
791 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
792 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
793 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100794}
795/* END_CASE */
796
Hanno Becker6b4ce492017-08-23 11:00:21 +0100797/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100798void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
799 int radix_Q, char *input_Q,
800 int radix_E, char *input_E,
801 int radix_D, char *output_D,
802 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +0100803{
804 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
805
806 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
807 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
808 mbedtls_mpi_init( &E );
809 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
810
811 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
812 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
813 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
814 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
815
816 if( corrupt )
817 {
818 /* Make E even */
819 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
820 }
821
822 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100823 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
824 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100825
826 if( !corrupt )
827 {
828 /*
829 * Check that D and Dp agree modulo LCM(P-1, Q-1).
830 */
831
832 /* Replace P,Q by P-1, Q-1 */
833 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
834 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
835
836 /* Check D == Dp modulo P-1 */
837 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
838 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
839 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
840
841 /* Check D == Dp modulo Q-1 */
842 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
843 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
844 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
845 }
846
847exit:
848
849 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
850 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
851 mbedtls_mpi_free( &E );
852 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
853}
854/* END_CASE */
855
Hanno Beckerf40cdf92017-12-22 11:03:27 +0000856/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +0100857void mbedtls_rsa_import( int radix_N, char *input_N,
858 int radix_P, char *input_P,
859 int radix_Q, char *input_Q,
860 int radix_D, char *input_D,
861 int radix_E, char *input_E,
862 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +0100863 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +0100864 int res_check,
865 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100866{
867 mbedtls_mpi N, P, Q, D, E;
868 mbedtls_rsa_context ctx;
869
Hanno Beckere1582a82017-09-29 11:51:05 +0100870 /* Buffers used for encryption-decryption test */
871 unsigned char *buf_orig = NULL;
872 unsigned char *buf_enc = NULL;
873 unsigned char *buf_dec = NULL;
874
Hanno Beckerc77ab892017-08-23 11:01:06 +0100875 mbedtls_entropy_context entropy;
876 mbedtls_ctr_drbg_context ctr_drbg;
877 const char *pers = "test_suite_rsa";
878
Hanno Becker4d6e8342017-09-29 11:50:18 +0100879 const int have_N = ( strlen( input_N ) > 0 );
880 const int have_P = ( strlen( input_P ) > 0 );
881 const int have_Q = ( strlen( input_Q ) > 0 );
882 const int have_D = ( strlen( input_D ) > 0 );
883 const int have_E = ( strlen( input_E ) > 0 );
884
Hanno Beckerc77ab892017-08-23 11:01:06 +0100885 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100886 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100887 mbedtls_rsa_init( &ctx, 0, 0 );
888
889 mbedtls_mpi_init( &N );
890 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
891 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
892
Hanno Beckerd4d60572018-01-10 07:12:01 +0000893 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
894 (const unsigned char *) pers, strlen( pers ) ) == 0 );
895
Hanno Becker4d6e8342017-09-29 11:50:18 +0100896 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100897 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
898
Hanno Becker4d6e8342017-09-29 11:50:18 +0100899 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100900 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
901
Hanno Becker4d6e8342017-09-29 11:50:18 +0100902 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100903 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
904
Hanno Becker4d6e8342017-09-29 11:50:18 +0100905 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100906 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
907
Hanno Becker4d6e8342017-09-29 11:50:18 +0100908 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100909 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
910
911 if( !successive )
912 {
913 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100914 have_N ? &N : NULL,
915 have_P ? &P : NULL,
916 have_Q ? &Q : NULL,
917 have_D ? &D : NULL,
918 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100919 }
920 else
921 {
922 /* Import N, P, Q, D, E separately.
923 * This should make no functional difference. */
924
925 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100926 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100927 NULL, NULL, NULL, NULL ) == 0 );
928
929 TEST_ASSERT( mbedtls_rsa_import( &ctx,
930 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100931 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100932 NULL, NULL, NULL ) == 0 );
933
934 TEST_ASSERT( mbedtls_rsa_import( &ctx,
935 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100936 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100937 NULL, NULL ) == 0 );
938
939 TEST_ASSERT( mbedtls_rsa_import( &ctx,
940 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100941 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100942 NULL ) == 0 );
943
944 TEST_ASSERT( mbedtls_rsa_import( &ctx,
945 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100946 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100947 }
948
Hanno Becker04877a42017-10-11 10:01:33 +0100949 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100950
Hanno Beckere1582a82017-09-29 11:51:05 +0100951 /* On expected success, perform some public and private
952 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +0100953 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +0100954 {
Hanno Beckere1582a82017-09-29 11:51:05 +0100955 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +0100956 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
957 else
958 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
959
960 if( res_check != 0 )
961 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +0100962
963 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
964 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
965 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
966 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
967 goto exit;
968
969 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
970 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
971
972 /* Make sure the number we're generating is smaller than the modulus */
973 buf_orig[0] = 0x00;
974
975 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
976
977 if( is_priv )
978 {
979 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
980 &ctr_drbg, buf_enc,
981 buf_dec ) == 0 );
982
983 TEST_ASSERT( memcmp( buf_orig, buf_dec,
984 mbedtls_rsa_get_len( &ctx ) ) == 0 );
985 }
986 }
987
Hanno Beckerc77ab892017-08-23 11:01:06 +0100988exit:
989
Hanno Beckere1582a82017-09-29 11:51:05 +0100990 mbedtls_free( buf_orig );
991 mbedtls_free( buf_enc );
992 mbedtls_free( buf_dec );
993
Hanno Beckerc77ab892017-08-23 11:01:06 +0100994 mbedtls_rsa_free( &ctx );
995
996 mbedtls_ctr_drbg_free( &ctr_drbg );
997 mbedtls_entropy_free( &entropy );
998
999 mbedtls_mpi_free( &N );
1000 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1001 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1002}
1003/* END_CASE */
1004
Hanno Becker417f2d62017-08-23 11:44:51 +01001005/* BEGIN_CASE */
1006void mbedtls_rsa_export( int radix_N, char *input_N,
1007 int radix_P, char *input_P,
1008 int radix_Q, char *input_Q,
1009 int radix_D, char *input_D,
1010 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001011 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001012 int successive )
1013{
1014 /* Original MPI's with which we set up the RSA context */
1015 mbedtls_mpi N, P, Q, D, E;
1016
1017 /* Exported MPI's */
1018 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1019
1020 const int have_N = ( strlen( input_N ) > 0 );
1021 const int have_P = ( strlen( input_P ) > 0 );
1022 const int have_Q = ( strlen( input_Q ) > 0 );
1023 const int have_D = ( strlen( input_D ) > 0 );
1024 const int have_E = ( strlen( input_E ) > 0 );
1025
Hanno Becker417f2d62017-08-23 11:44:51 +01001026 mbedtls_rsa_context ctx;
1027
1028 mbedtls_rsa_init( &ctx, 0, 0 );
1029
1030 mbedtls_mpi_init( &N );
1031 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1032 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1033
1034 mbedtls_mpi_init( &Ne );
1035 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1036 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1037
1038 /* Setup RSA context */
1039
1040 if( have_N )
1041 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1042
1043 if( have_P )
1044 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1045
1046 if( have_Q )
1047 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1048
1049 if( have_D )
1050 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1051
1052 if( have_E )
1053 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1054
1055 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1056 strlen( input_N ) ? &N : NULL,
1057 strlen( input_P ) ? &P : NULL,
1058 strlen( input_Q ) ? &Q : NULL,
1059 strlen( input_D ) ? &D : NULL,
1060 strlen( input_E ) ? &E : NULL ) == 0 );
1061
Hanno Becker7f25f852017-10-10 16:56:22 +01001062 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001063
1064 /*
1065 * Export parameters and compare to original ones.
1066 */
1067
1068 /* N and E must always be present. */
1069 if( !successive )
1070 {
1071 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1072 }
1073 else
1074 {
1075 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1076 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1077 }
1078 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1079 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1080
1081 /* If we were providing enough information to setup a complete private context,
1082 * we expect to be able to export all core parameters. */
1083
1084 if( is_priv )
1085 {
1086 if( !successive )
1087 {
1088 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1089 &De, NULL ) == 0 );
1090 }
1091 else
1092 {
1093 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1094 NULL, NULL ) == 0 );
1095 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1096 NULL, NULL ) == 0 );
1097 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1098 &De, NULL ) == 0 );
1099 }
1100
1101 if( have_P )
1102 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1103
1104 if( have_Q )
1105 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1106
1107 if( have_D )
1108 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1109
1110 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001111 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1112 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001113 }
1114
1115exit:
1116
1117 mbedtls_rsa_free( &ctx );
1118
1119 mbedtls_mpi_free( &N );
1120 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1121 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1122
1123 mbedtls_mpi_free( &Ne );
1124 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1125 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1126}
1127/* END_CASE */
1128
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001129/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Becker750e8b42017-08-25 07:54:27 +01001130void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1131 int radix_P, char *input_P,
1132 int radix_Q, char *input_Q,
1133 int radix_D, char *input_D,
1134 int radix_E, char *input_E,
1135 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001136{
1137 /* Original MPI's with which we set up the RSA context */
1138 mbedtls_mpi N, P, Q, D, E;
1139
1140 const int have_N = ( strlen( input_N ) > 0 );
1141 const int have_P = ( strlen( input_P ) > 0 );
1142 const int have_Q = ( strlen( input_Q ) > 0 );
1143 const int have_D = ( strlen( input_D ) > 0 );
1144 const int have_E = ( strlen( input_E ) > 0 );
1145
1146 mbedtls_entropy_context entropy;
1147 mbedtls_ctr_drbg_context ctr_drbg;
1148 const char *pers = "test_suite_rsa";
1149
1150 mbedtls_mpi_init( &N );
1151 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1152 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1153
1154 mbedtls_ctr_drbg_init( &ctr_drbg );
1155 mbedtls_entropy_init( &entropy );
1156 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1157 &entropy, (const unsigned char *) pers,
1158 strlen( pers ) ) == 0 );
1159
1160 if( have_N )
1161 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1162
1163 if( have_P )
1164 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1165
1166 if( have_Q )
1167 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1168
1169 if( have_D )
1170 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1171
1172 if( have_E )
1173 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1174
Hanno Becker750e8b42017-08-25 07:54:27 +01001175 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1176 have_P ? &P : NULL,
1177 have_Q ? &Q : NULL,
1178 have_D ? &D : NULL,
1179 have_E ? &E : NULL,
1180 prng ? mbedtls_ctr_drbg_random : NULL,
1181 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001182exit:
1183
1184 mbedtls_ctr_drbg_free( &ctr_drbg );
1185 mbedtls_entropy_free( &entropy );
1186
1187 mbedtls_mpi_free( &N );
1188 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1189 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1190}
1191/* END_CASE */
1192
Hanno Beckerc77ab892017-08-23 11:01:06 +01001193/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001194void mbedtls_rsa_export_raw( char *input_N, char *input_P,
1195 char *input_Q, char *input_D,
Hanno Beckere1582a82017-09-29 11:51:05 +01001196 char *input_E, int is_priv,
1197 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001198{
1199 /* Original raw buffers with which we set up the RSA context */
1200 unsigned char bufN[1000];
1201 unsigned char bufP[1000];
1202 unsigned char bufQ[1000];
1203 unsigned char bufD[1000];
1204 unsigned char bufE[1000];
1205
1206 size_t lenN = 0;
1207 size_t lenP = 0;
1208 size_t lenQ = 0;
1209 size_t lenD = 0;
1210 size_t lenE = 0;
1211
1212 /* Exported buffers */
1213 unsigned char bufNe[ sizeof( bufN ) ];
1214 unsigned char bufPe[ sizeof( bufP ) ];
1215 unsigned char bufQe[ sizeof( bufQ ) ];
1216 unsigned char bufDe[ sizeof( bufD ) ];
1217 unsigned char bufEe[ sizeof( bufE ) ];
1218
1219 const int have_N = ( strlen( input_N ) > 0 );
1220 const int have_P = ( strlen( input_P ) > 0 );
1221 const int have_Q = ( strlen( input_Q ) > 0 );
1222 const int have_D = ( strlen( input_D ) > 0 );
1223 const int have_E = ( strlen( input_E ) > 0 );
1224
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001225 mbedtls_rsa_context ctx;
1226
1227 mbedtls_rsa_init( &ctx, 0, 0 );
1228
1229 /* Setup RSA context */
1230
1231 if( have_N )
1232 lenN = unhexify( bufN, input_N );
1233
1234 if( have_P )
1235 lenP = unhexify( bufP, input_P );
1236
1237 if( have_Q )
1238 lenQ = unhexify( bufQ, input_Q );
1239
1240 if( have_D )
1241 lenD = unhexify( bufD, input_D );
1242
1243 if( have_E )
1244 lenE = unhexify( bufE, input_E );
1245
1246 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1247 have_N ? bufN : NULL, lenN,
1248 have_P ? bufP : NULL, lenP,
1249 have_Q ? bufQ : NULL, lenQ,
1250 have_D ? bufD : NULL, lenD,
1251 have_E ? bufE : NULL, lenE ) == 0 );
1252
Hanno Becker7f25f852017-10-10 16:56:22 +01001253 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001254
1255 /*
1256 * Export parameters and compare to original ones.
1257 */
1258
1259 /* N and E must always be present. */
1260 if( !successive )
1261 {
1262 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, lenN,
1263 NULL, 0, NULL, 0, NULL, 0,
1264 bufEe, lenE ) == 0 );
1265 }
1266 else
1267 {
1268 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, lenN,
1269 NULL, 0, NULL, 0, NULL, 0,
1270 NULL, 0 ) == 0 );
1271 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1272 NULL, 0, NULL, 0, NULL, 0,
1273 bufEe, lenE ) == 0 );
1274 }
1275 TEST_ASSERT( memcmp( bufN, bufNe, lenN ) == 0 );
1276 TEST_ASSERT( memcmp( bufE, bufEe, lenE ) == 0 );
1277
1278 /* If we were providing enough information to setup a complete private context,
1279 * we expect to be able to export all core parameters. */
1280
1281 if( is_priv )
1282 {
1283 if( !successive )
1284 {
1285 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1286 bufPe, lenP ? lenP : sizeof( bufPe ),
1287 bufQe, lenQ ? lenQ : sizeof( bufQe ),
1288 bufDe, lenD ? lenD : sizeof( bufDe ),
1289 NULL, 0 ) == 0 );
1290 }
1291 else
1292 {
1293 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1294 bufPe, lenP ? lenP : sizeof( bufPe ),
1295 NULL, 0, NULL, 0,
1296 NULL, 0 ) == 0 );
1297
1298 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
1299 bufQe, lenQ ? lenQ : sizeof( bufQe ),
1300 NULL, 0, NULL, 0 ) == 0 );
1301
1302 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
1303 NULL, 0, bufDe, lenD ? lenD : sizeof( bufDe ),
1304 NULL, 0 ) == 0 );
1305 }
1306
1307 if( have_P )
1308 TEST_ASSERT( memcmp( bufP, bufPe, lenP ) == 0 );
1309
1310 if( have_Q )
1311 TEST_ASSERT( memcmp( bufQ, bufQe, lenQ ) == 0 );
1312
1313 if( have_D )
1314 TEST_ASSERT( memcmp( bufD, bufDe, lenD ) == 0 );
1315
1316 }
1317
1318exit:
1319 mbedtls_rsa_free( &ctx );
1320}
1321/* END_CASE */
1322
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001323/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001324void mbedtls_rsa_import_raw( char *input_N,
1325 char *input_P, char *input_Q,
1326 char *input_D, char *input_E,
1327 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001328 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001329 int res_check,
1330 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001331{
1332 unsigned char bufN[1000];
1333 unsigned char bufP[1000];
1334 unsigned char bufQ[1000];
1335 unsigned char bufD[1000];
1336 unsigned char bufE[1000];
1337
Hanno Beckere1582a82017-09-29 11:51:05 +01001338 /* Buffers used for encryption-decryption test */
1339 unsigned char *buf_orig = NULL;
1340 unsigned char *buf_enc = NULL;
1341 unsigned char *buf_dec = NULL;
1342
Hanno Beckerc77ab892017-08-23 11:01:06 +01001343 size_t lenN = 0;
1344 size_t lenP = 0;
1345 size_t lenQ = 0;
1346 size_t lenD = 0;
1347 size_t lenE = 0;
1348
1349 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001350 mbedtls_entropy_context entropy;
1351 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001352
Hanno Beckerc77ab892017-08-23 11:01:06 +01001353 const char *pers = "test_suite_rsa";
1354
1355 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001356 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001357 mbedtls_rsa_init( &ctx, 0, 0 );
1358
Hanno Beckerc77ab892017-08-23 11:01:06 +01001359 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1360 &entropy, (const unsigned char *) pers,
1361 strlen( pers ) ) == 0 );
1362
Hanno Beckerc77ab892017-08-23 11:01:06 +01001363 if( strlen( input_N ) )
1364 lenN = unhexify( bufN, input_N );
1365
1366 if( strlen( input_P ) )
1367 lenP = unhexify( bufP, input_P );
1368
1369 if( strlen( input_Q ) )
1370 lenQ = unhexify( bufQ, input_Q );
1371
1372 if( strlen( input_D ) )
1373 lenD = unhexify( bufD, input_D );
1374
1375 if( strlen( input_E ) )
1376 lenE = unhexify( bufE, input_E );
1377
1378 if( !successive )
1379 {
1380 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1381 ( lenN > 0 ) ? bufN : NULL, lenN,
1382 ( lenP > 0 ) ? bufP : NULL, lenP,
1383 ( lenQ > 0 ) ? bufQ : NULL, lenQ,
1384 ( lenD > 0 ) ? bufD : NULL, lenD,
1385 ( lenE > 0 ) ? bufE : NULL, lenE ) == 0 );
1386 }
1387 else
1388 {
1389 /* Import N, P, Q, D, E separately.
1390 * This should make no functional difference. */
1391
1392 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1393 ( lenN > 0 ) ? bufN : NULL, lenN,
1394 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1395
1396 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1397 NULL, 0,
1398 ( lenP > 0 ) ? bufP : NULL, lenP,
1399 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1400
1401 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1402 NULL, 0, NULL, 0,
1403 ( lenQ > 0 ) ? bufQ : NULL, lenQ,
1404 NULL, 0, NULL, 0 ) == 0 );
1405
1406 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1407 NULL, 0, NULL, 0, NULL, 0,
1408 ( lenD > 0 ) ? bufD : NULL, lenD,
1409 NULL, 0 ) == 0 );
1410
1411 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1412 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1413 ( lenE > 0 ) ? bufE : NULL, lenE ) == 0 );
1414 }
1415
Hanno Becker04877a42017-10-11 10:01:33 +01001416 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001417
Hanno Beckere1582a82017-09-29 11:51:05 +01001418 /* On expected success, perform some public and private
1419 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001420 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001421 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001422 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001423 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1424 else
1425 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1426
1427 if( res_check != 0 )
1428 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001429
1430 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1431 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1432 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1433 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1434 goto exit;
1435
1436 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1437 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1438
1439 /* Make sure the number we're generating is smaller than the modulus */
1440 buf_orig[0] = 0x00;
1441
1442 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1443
1444 if( is_priv )
1445 {
1446 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1447 &ctr_drbg, buf_enc,
1448 buf_dec ) == 0 );
1449
1450 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1451 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1452 }
1453 }
1454
Hanno Beckerc77ab892017-08-23 11:01:06 +01001455exit:
1456
Hanno Becker3f3ae852017-10-02 10:08:39 +01001457 mbedtls_free( buf_orig );
1458 mbedtls_free( buf_enc );
1459 mbedtls_free( buf_dec );
1460
Hanno Beckerc77ab892017-08-23 11:01:06 +01001461 mbedtls_rsa_free( &ctx );
1462
1463 mbedtls_ctr_drbg_free( &ctr_drbg );
1464 mbedtls_entropy_free( &entropy );
1465
1466}
1467/* END_CASE */
1468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +02001470void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +00001471{
Andres AG93012e82016-09-09 09:10:28 +01001472 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001473}
Paul Bakker33b43f12013-08-20 11:48:36 +02001474/* END_CASE */