blob: 87d15a85951422e35bf54377f394d878a45db5c2 [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;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010029 mbedtls_mpi N, P, Q, E;
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
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010033 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
34 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000036
37 memset( message_str, 0x00, 1000 );
38 memset( hash_result, 0x00, 1000 );
39 memset( output, 0x00, 1000 );
40 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +020041 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +000042
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010043 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
44 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
45 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
46 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000047
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010048 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
49 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +010050 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000052
Paul Bakker33b43f12013-08-20 11:48:36 +020053 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 if( mbedtls_md_info_from_type( digest ) != NULL )
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010056 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ),
57 message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000058
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010059 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
60 MBEDTLS_RSA_PRIVATE, digest, 0,
61 hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020062 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +000063 {
64 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +000065
Paul Bakker33b43f12013-08-20 11:48:36 +020066 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +000067 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000068
Paul Bakkerbd51b262014-07-10 15:26:12 +020069exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010070 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
71 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +000073}
Paul Bakker33b43f12013-08-20 11:48:36 +020074/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +000075
Paul Bakker33b43f12013-08-20 11:48:36 +020076/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077void mbedtls_rsa_pkcs1_verify( char *message_hex_string, int padding_mode, int digest,
Paul Bakker33b43f12013-08-20 11:48:36 +020078 int mod, int radix_N, char *input_N, int radix_E,
79 char *input_E, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000080{
81 unsigned char message_str[1000];
82 unsigned char hash_result[1000];
83 unsigned char result_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000085 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +000086
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010087 mbedtls_mpi N, E;
88
89 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000091 memset( message_str, 0x00, 1000 );
92 memset( hash_result, 0x00, 1000 );
93 memset( result_str, 0x00, 1000 );
94
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010095 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
96 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
97 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
98 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000100
Paul Bakker33b43f12013-08-20 11:48:36 +0200101 msg_len = unhexify( message_str, message_hex_string );
102 unhexify( result_str, result_hex_str );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 if( mbedtls_md_info_from_type( digest ) != NULL )
105 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 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 +0100108
Paul Bakkerbd51b262014-07-10 15:26:12 +0200109exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100110 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000112}
Paul Bakker33b43f12013-08-20 11:48:36 +0200113/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000114
Paul Bakker821fb082009-07-12 13:26:42 +0000115
Paul Bakker33b43f12013-08-20 11:48:36 +0200116/* BEGIN_CASE */
117void rsa_pkcs1_sign_raw( char *message_hex_string, char *hash_result_string,
118 int padding_mode, int mod, int radix_P, char *input_P,
119 int radix_Q, char *input_Q, int radix_N,
120 char *input_N, int radix_E, char *input_E,
121 char *result_hex_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000122{
123 unsigned char message_str[1000];
124 unsigned char hash_result[1000];
125 unsigned char output[1000];
126 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100128 mbedtls_mpi N, P, Q, E;
Paul Bakkereaf90d92011-07-13 14:21:52 +0000129 int hash_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200130 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100133 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
134 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000135
Paul Bakker42a29bf2009-07-07 20:18:41 +0000136 memset( message_str, 0x00, 1000 );
137 memset( hash_result, 0x00, 1000 );
138 memset( output, 0x00, 1000 );
139 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200140 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000141
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100142 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
143 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
144 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
145 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000146
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100147 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
148 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100149 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000151
Paul Bakker33b43f12013-08-20 11:48:36 +0200152 unhexify( message_str, message_hex_string );
153 hash_len = unhexify( hash_result, hash_result_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000154
Hanno Becker8fd55482017-08-23 14:07:48 +0100155 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
156 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
157 hash_len, hash_result, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000158
159 hexify( output_str, output, ctx.len );
160
Paul Bakker33b43f12013-08-20 11:48:36 +0200161 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000162
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100163 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100165 {
166 memset( output, 0x00, 1000 );
167 memset( output_str, 0x00, 1000 );
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 TEST_ASSERT( mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
170 &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100171 hash_len, hash_result, output ) == 0 );
172
173 hexify( output_str, output, ctx.len );
174
175 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
176 }
177
Paul Bakkerbd51b262014-07-10 15:26:12 +0200178exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100179 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
180 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000183}
Paul Bakker33b43f12013-08-20 11:48:36 +0200184/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000185
Paul Bakker33b43f12013-08-20 11:48:36 +0200186/* BEGIN_CASE */
187void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string,
188 int padding_mode, int mod, int radix_N,
189 char *input_N, int radix_E, char *input_E,
190 char *result_hex_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000191{
192 unsigned char message_str[1000];
193 unsigned char hash_result[1000];
194 unsigned char result_str[1000];
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100195 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_rsa_context ctx;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100197 size_t hash_len, olen;
Paul Bakker821fb082009-07-12 13:26:42 +0000198
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100199 mbedtls_mpi N, E;
200 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000203 memset( message_str, 0x00, 1000 );
204 memset( hash_result, 0x00, 1000 );
205 memset( result_str, 0x00, 1000 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100206 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000207
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100208 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
209 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000210
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100211 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
212 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000214
Paul Bakker33b43f12013-08-20 11:48:36 +0200215 unhexify( message_str, message_hex_string );
216 hash_len = unhexify( hash_result, hash_result_string );
217 unhexify( result_str, result_hex_str );
Paul Bakker821fb082009-07-12 13:26:42 +0000218
Hanno Becker8fd55482017-08-23 14:07:48 +0100219 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
220 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE,
221 hash_len, hash_result,
222 result_str ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100223
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100224 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100226 {
227 int ok;
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 TEST_ASSERT( mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
230 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100231 &olen, result_str, output, sizeof( output ) ) == 0 );
232
233 ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0;
234 if( correct == 0 )
235 TEST_ASSERT( ok == 1 );
236 else
237 TEST_ASSERT( ok == 0 );
238 }
239
Paul Bakkerbd51b262014-07-10 15:26:12 +0200240exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100241 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000243}
Paul Bakker33b43f12013-08-20 11:48:36 +0200244/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000245
Paul Bakker33b43f12013-08-20 11:48:36 +0200246/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247void mbedtls_rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200248 int radix_N, char *input_N, int radix_E, char *input_E,
249 char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000250{
251 unsigned char message_str[1000];
252 unsigned char output[1000];
253 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000255 size_t msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000256 rnd_pseudo_info rnd_info;
257
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100258 mbedtls_mpi N, E;
259 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
260
Paul Bakker997bbd12011-03-13 15:45:42 +0000261 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000264 memset( message_str, 0x00, 1000 );
265 memset( output, 0x00, 1000 );
266 memset( output_str, 0x00, 1000 );
267
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100268 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
269 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000270
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100271 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
272 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000274
Paul Bakker33b43f12013-08-20 11:48:36 +0200275 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PUBLIC, msg_len, message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200278 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000279 {
280 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000281
Paul Bakker33b43f12013-08-20 11:48:36 +0200282 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000283 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100284
Paul Bakkerbd51b262014-07-10 15:26:12 +0200285exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100286 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000288}
Paul Bakker33b43f12013-08-20 11:48:36 +0200289/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000290
Paul Bakker33b43f12013-08-20 11:48:36 +0200291/* BEGIN_CASE */
292void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode,
293 int mod, int radix_N, char *input_N,
294 int radix_E, char *input_E,
295 char *result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000296{
297 unsigned char message_str[1000];
298 unsigned char output[1000];
299 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000301 size_t msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000302
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100303 mbedtls_mpi N, E;
304
305 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000307 memset( message_str, 0x00, 1000 );
308 memset( output, 0x00, 1000 );
309 memset( output_str, 0x00, 1000 );
310
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100311 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
312 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000313
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100314 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
315 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000317
Paul Bakker33b43f12013-08-20 11:48:36 +0200318 msg_len = unhexify( message_str, message_hex_string );
Paul Bakkera6656852010-07-18 19:47:14 +0000319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL, MBEDTLS_RSA_PUBLIC, msg_len, message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200321 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000322 {
323 hexify( output_str, output, ctx.len );
324
Paul Bakker33b43f12013-08-20 11:48:36 +0200325 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000326 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100327
Paul Bakkerbd51b262014-07-10 15:26:12 +0200328exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100329 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000331}
Paul Bakker33b43f12013-08-20 11:48:36 +0200332/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000333
Paul Bakker33b43f12013-08-20 11:48:36 +0200334/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335void mbedtls_rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200336 int radix_P, char *input_P, int radix_Q, char *input_Q,
337 int radix_N, char *input_N, int radix_E, char *input_E,
338 int max_output, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000339{
340 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000341 unsigned char output[1000];
342 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000344 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200345 rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100346 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000347
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100348 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
349 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000352
353 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000354 memset( output, 0x00, 1000 );
355 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200356 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000357
Paul Bakker42a29bf2009-07-07 20:18:41 +0000358
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100359 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
360 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
361 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
362 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000363
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100364 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
365 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100366 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000368
Paul Bakker33b43f12013-08-20 11:48:36 +0200369 unhexify( message_str, message_hex_string );
Paul Bakker69998dd2009-07-11 19:15:20 +0000370 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 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 +0200373 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000374 {
375 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000376
Paul Bakker33b43f12013-08-20 11:48:36 +0200377 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000378 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000379
Paul Bakkerbd51b262014-07-10 15:26:12 +0200380exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100381 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
382 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000384}
Paul Bakker33b43f12013-08-20 11:48:36 +0200385/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000386
Paul Bakker33b43f12013-08-20 11:48:36 +0200387/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388void mbedtls_rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N,
Paul Bakker33b43f12013-08-20 11:48:36 +0200389 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000390{
391 unsigned char message_str[1000];
392 unsigned char output[1000];
393 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000395
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100396 mbedtls_mpi N, E;
397
398 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
400 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000401 memset( message_str, 0x00, 1000 );
402 memset( output, 0x00, 1000 );
403 memset( output_str, 0x00, 1000 );
404
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100405 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
406 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000407
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100408 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
409 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000411
Paul Bakker33b43f12013-08-20 11:48:36 +0200412 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200415 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000416 {
417 hexify( output_str, output, ctx.len );
418
Paul Bakker33b43f12013-08-20 11:48:36 +0200419 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000420 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100421
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100422 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200424 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100428
429 memset( output, 0x00, 1000 );
430 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100432 if( result == 0 )
433 {
434 hexify( output_str, output, ctx2.len );
435
436 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
437 }
438
Paul Bakkerbd51b262014-07-10 15:26:12 +0200439exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100440 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_rsa_free( &ctx );
442 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000443}
Paul Bakker33b43f12013-08-20 11:48:36 +0200444/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000445
Paul Bakker33b43f12013-08-20 11:48:36 +0200446/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447void mbedtls_rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
Paul Bakker33b43f12013-08-20 11:48:36 +0200448 int radix_Q, char *input_Q, int radix_N, char *input_N,
449 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000450{
451 unsigned char message_str[1000];
452 unsigned char output[1000];
453 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100455 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200456 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200457 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000458
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100459 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
460 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
462 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000463
464 memset( message_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200465 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000466
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100467 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
468 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
469 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
470 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000471
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100472 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
473 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100474 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000476
Paul Bakker33b43f12013-08-20 11:48:36 +0200477 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000478
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200479 /* repeat three times to test updating of blinding values */
480 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000481 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200482 memset( output, 0x00, 1000 );
483 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200485 message_str, output ) == result );
486 if( result == 0 )
487 {
488 hexify( output_str, output, ctx.len );
Paul Bakker821fb082009-07-12 13:26:42 +0000489
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200490 TEST_ASSERT( strcasecmp( (char *) output_str,
491 result_hex_str ) == 0 );
492 }
Paul Bakker821fb082009-07-12 13:26:42 +0000493 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000494
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100495 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200497 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100501
502 memset( output, 0x00, 1000 );
503 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100505 message_str, output ) == result );
506 if( result == 0 )
507 {
508 hexify( output_str, output, ctx2.len );
509
510 TEST_ASSERT( strcasecmp( (char *) output_str,
511 result_hex_str ) == 0 );
512 }
513
Paul Bakkerbd51b262014-07-10 15:26:12 +0200514exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100515 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
516 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000519}
Paul Bakker33b43f12013-08-20 11:48:36 +0200520/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000521
Paul Bakker33b43f12013-08-20 11:48:36 +0200522/* BEGIN_CASE */
523void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000524{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_rsa_context ctx;
526 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000529}
Paul Bakker33b43f12013-08-20 11:48:36 +0200530/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000531
Paul Bakker33b43f12013-08-20 11:48:36 +0200532/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533void mbedtls_rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
Paul Bakker33b43f12013-08-20 11:48:36 +0200534 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000535{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100537 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000538
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100539 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000541
Paul Bakker33b43f12013-08-20 11:48:36 +0200542 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000543 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100544 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000545 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200546 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000547 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100548 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000549 }
550
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100551 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100553
Paul Bakkerbd51b262014-07-10 15:26:12 +0200554exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100555 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000557}
Paul Bakker33b43f12013-08-20 11:48:36 +0200558/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000559
Paul Bakker33b43f12013-08-20 11:48:36 +0200560/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561void mbedtls_rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
Paul Bakker33b43f12013-08-20 11:48:36 +0200562 char *input_Q, int radix_N, char *input_N,
563 int radix_E, char *input_E, int radix_D, char *input_D,
564 int radix_DP, char *input_DP, int radix_DQ,
565 char *input_DQ, int radix_QP, char *input_QP,
566 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000571
Paul Bakker33b43f12013-08-20 11:48:36 +0200572 ctx.len = mod / 8;
573 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000576 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200577 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000580 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200581 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000584 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200585 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000588 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200589 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000592 }
Hanno Becker131134f2017-08-23 08:31:07 +0100593#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200594 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000597 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200598 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000601 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200602 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000605 }
Hanno Becker131134f2017-08-23 08:31:07 +0100606#else
607 ((void) radix_DP); ((void) input_DP);
608 ((void) radix_DQ); ((void) input_DQ);
609 ((void) radix_QP); ((void) input_QP);
610#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100613
Paul Bakkerbd51b262014-07-10 15:26:12 +0200614exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000616}
Paul Bakker33b43f12013-08-20 11:48:36 +0200617/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000618
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100619/* BEGIN_CASE */
620void rsa_check_pubpriv( int mod, int radix_Npub, char *input_Npub,
621 int radix_Epub, char *input_Epub,
622 int radix_P, char *input_P, int radix_Q,
623 char *input_Q, int radix_N, char *input_N,
624 int radix_E, char *input_E, int radix_D, char *input_D,
625 int radix_DP, char *input_DP, int radix_DQ,
626 char *input_DQ, int radix_QP, char *input_QP,
627 int result )
628{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
632 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100633
634 pub.len = mod / 8;
635 prv.len = mod / 8;
636
637 if( strlen( input_Npub ) )
638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100640 }
641 if( strlen( input_Epub ) )
642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100644 }
645
646 if( strlen( input_P ) )
647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100649 }
650 if( strlen( input_Q ) )
651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100653 }
654 if( strlen( input_N ) )
655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100657 }
658 if( strlen( input_E ) )
659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100661 }
662 if( strlen( input_D ) )
663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100665 }
Hanno Becker131134f2017-08-23 08:31:07 +0100666#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100667 if( strlen( input_DP ) )
668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100670 }
671 if( strlen( input_DQ ) )
672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100674 }
675 if( strlen( input_QP ) )
676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100678 }
Hanno Becker131134f2017-08-23 08:31:07 +0100679#else
680 ((void) radix_DP); ((void) input_DP);
681 ((void) radix_DQ); ((void) input_DQ);
682 ((void) radix_QP); ((void) input_QP);
683#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100686
687exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_rsa_free( &pub );
689 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100690}
691/* END_CASE */
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
694void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000695{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_rsa_context ctx;
697 mbedtls_entropy_context entropy;
698 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200699 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000700
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200701 mbedtls_ctr_drbg_init( &ctr_drbg );
702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_entropy_init( &entropy );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100704 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
705 &entropy, (const unsigned char *) pers,
706 strlen( pers ) ) == 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000709
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100710 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random,
711 &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200712 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100715 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000716 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100717
Paul Bakkerbd51b262014-07-10 15:26:12 +0200718exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_rsa_free( &ctx );
720 mbedtls_ctr_drbg_free( &ctr_drbg );
721 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000722}
Paul Bakker33b43f12013-08-20 11:48:36 +0200723/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000724
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100725/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +0100726void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100727 int radix_D, char *input_D,
728 int radix_E, char *input_E,
729 int radix_P, char *output_P,
730 int radix_Q, char *output_Q,
731 int corrupt, int result )
732{
733 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
734
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100735 mbedtls_mpi_init( &N );
736 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
737 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
738 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
739
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100740 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
741 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
742 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
743 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
744 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
745
746 if( corrupt )
747 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
748
749 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100750 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100751
752 if( !corrupt )
753 {
754 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
755 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
756 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
757 }
758
759exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100760 mbedtls_mpi_free( &N );
761 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
762 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
763 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100764}
765/* END_CASE */
766
Hanno Becker6b4ce492017-08-23 11:00:21 +0100767/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100768void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
769 int radix_Q, char *input_Q,
770 int radix_E, char *input_E,
771 int radix_D, char *output_D,
772 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +0100773{
774 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
775
776 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
777 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
778 mbedtls_mpi_init( &E );
779 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
780
781 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
782 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
783 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
784 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
785
786 if( corrupt )
787 {
788 /* Make E even */
789 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
790 }
791
792 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100793 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
794 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100795
796 if( !corrupt )
797 {
798 /*
799 * Check that D and Dp agree modulo LCM(P-1, Q-1).
800 */
801
802 /* Replace P,Q by P-1, Q-1 */
803 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
804 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
805
806 /* Check D == Dp modulo P-1 */
807 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
808 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
809 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
810
811 /* Check D == Dp modulo Q-1 */
812 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
813 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
814 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
815 }
816
817exit:
818
819 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
820 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
821 mbedtls_mpi_free( &E );
822 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
823}
824/* END_CASE */
825
Hanno Beckerc77ab892017-08-23 11:01:06 +0100826/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
827void mbedtls_rsa_import( int radix_N, char *input_N,
828 int radix_P, char *input_P,
829 int radix_Q, char *input_Q,
830 int radix_D, char *input_D,
831 int radix_E, char *input_E,
832 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +0100833 int is_priv,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100834 int result )
835{
836 mbedtls_mpi N, P, Q, D, E;
837 mbedtls_rsa_context ctx;
838
Hanno Beckere1582a82017-09-29 11:51:05 +0100839 /* Buffers used for encryption-decryption test */
840 unsigned char *buf_orig = NULL;
841 unsigned char *buf_enc = NULL;
842 unsigned char *buf_dec = NULL;
843
Hanno Beckerc77ab892017-08-23 11:01:06 +0100844 mbedtls_entropy_context entropy;
845 mbedtls_ctr_drbg_context ctr_drbg;
846 const char *pers = "test_suite_rsa";
847
Hanno Becker4d6e8342017-09-29 11:50:18 +0100848 const int have_N = ( strlen( input_N ) > 0 );
849 const int have_P = ( strlen( input_P ) > 0 );
850 const int have_Q = ( strlen( input_Q ) > 0 );
851 const int have_D = ( strlen( input_D ) > 0 );
852 const int have_E = ( strlen( input_E ) > 0 );
853
Hanno Beckerc77ab892017-08-23 11:01:06 +0100854 mbedtls_ctr_drbg_init( &ctr_drbg );
855
856 mbedtls_entropy_init( &entropy );
857 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
858 (const unsigned char *) pers, strlen( pers ) ) == 0 );
859
860 mbedtls_rsa_init( &ctx, 0, 0 );
861
862 mbedtls_mpi_init( &N );
863 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
864 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
865
Hanno Becker4d6e8342017-09-29 11:50:18 +0100866 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100867 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
868
Hanno Becker4d6e8342017-09-29 11:50:18 +0100869 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100870 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
871
Hanno Becker4d6e8342017-09-29 11:50:18 +0100872 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100873 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
874
Hanno Becker4d6e8342017-09-29 11:50:18 +0100875 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100876 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
877
Hanno Becker4d6e8342017-09-29 11:50:18 +0100878 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100879 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
880
881 if( !successive )
882 {
883 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100884 have_N ? &N : NULL,
885 have_P ? &P : NULL,
886 have_Q ? &Q : NULL,
887 have_D ? &D : NULL,
888 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100889 }
890 else
891 {
892 /* Import N, P, Q, D, E separately.
893 * This should make no functional difference. */
894
895 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100896 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100897 NULL, NULL, NULL, NULL ) == 0 );
898
899 TEST_ASSERT( mbedtls_rsa_import( &ctx,
900 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100901 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100902 NULL, NULL, NULL ) == 0 );
903
904 TEST_ASSERT( mbedtls_rsa_import( &ctx,
905 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100906 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100907 NULL, NULL ) == 0 );
908
909 TEST_ASSERT( mbedtls_rsa_import( &ctx,
910 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100911 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100912 NULL ) == 0 );
913
914 TEST_ASSERT( mbedtls_rsa_import( &ctx,
915 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100916 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100917 }
918
Hanno Becker7f25f852017-10-10 16:56:22 +0100919 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == result );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100920
Hanno Beckere1582a82017-09-29 11:51:05 +0100921 /* On expected success, perform some public and private
922 * key operations to check if the key is working properly. */
923 if( result == 0 )
924 {
925 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
926
927 /* Did we expect a full private key to be setup? */
928 if( is_priv )
929 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
930
931 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
932 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
933 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
934 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
935 goto exit;
936
937 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
938 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
939
940 /* Make sure the number we're generating is smaller than the modulus */
941 buf_orig[0] = 0x00;
942
943 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
944
945 if( is_priv )
946 {
947 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
948 &ctr_drbg, buf_enc,
949 buf_dec ) == 0 );
950
951 TEST_ASSERT( memcmp( buf_orig, buf_dec,
952 mbedtls_rsa_get_len( &ctx ) ) == 0 );
953 }
954 }
955
Hanno Beckerc77ab892017-08-23 11:01:06 +0100956exit:
957
Hanno Beckere1582a82017-09-29 11:51:05 +0100958 mbedtls_free( buf_orig );
959 mbedtls_free( buf_enc );
960 mbedtls_free( buf_dec );
961
Hanno Beckerc77ab892017-08-23 11:01:06 +0100962 mbedtls_rsa_free( &ctx );
963
964 mbedtls_ctr_drbg_free( &ctr_drbg );
965 mbedtls_entropy_free( &entropy );
966
967 mbedtls_mpi_free( &N );
968 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
969 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
970}
971/* END_CASE */
972
Hanno Becker417f2d62017-08-23 11:44:51 +0100973/* BEGIN_CASE */
974void mbedtls_rsa_export( int radix_N, char *input_N,
975 int radix_P, char *input_P,
976 int radix_Q, char *input_Q,
977 int radix_D, char *input_D,
978 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +0100979 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +0100980 int successive )
981{
982 /* Original MPI's with which we set up the RSA context */
983 mbedtls_mpi N, P, Q, D, E;
984
985 /* Exported MPI's */
986 mbedtls_mpi Ne, Pe, Qe, De, Ee;
987
988 const int have_N = ( strlen( input_N ) > 0 );
989 const int have_P = ( strlen( input_P ) > 0 );
990 const int have_Q = ( strlen( input_Q ) > 0 );
991 const int have_D = ( strlen( input_D ) > 0 );
992 const int have_E = ( strlen( input_E ) > 0 );
993
Hanno Becker417f2d62017-08-23 11:44:51 +0100994 mbedtls_rsa_context ctx;
995
996 mbedtls_rsa_init( &ctx, 0, 0 );
997
998 mbedtls_mpi_init( &N );
999 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1000 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1001
1002 mbedtls_mpi_init( &Ne );
1003 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1004 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1005
1006 /* Setup RSA context */
1007
1008 if( have_N )
1009 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1010
1011 if( have_P )
1012 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1013
1014 if( have_Q )
1015 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1016
1017 if( have_D )
1018 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1019
1020 if( have_E )
1021 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1022
1023 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1024 strlen( input_N ) ? &N : NULL,
1025 strlen( input_P ) ? &P : NULL,
1026 strlen( input_Q ) ? &Q : NULL,
1027 strlen( input_D ) ? &D : NULL,
1028 strlen( input_E ) ? &E : NULL ) == 0 );
1029
Hanno Becker7f25f852017-10-10 16:56:22 +01001030 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001031
1032 /*
1033 * Export parameters and compare to original ones.
1034 */
1035
1036 /* N and E must always be present. */
1037 if( !successive )
1038 {
1039 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1040 }
1041 else
1042 {
1043 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1044 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1045 }
1046 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1047 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1048
1049 /* If we were providing enough information to setup a complete private context,
1050 * we expect to be able to export all core parameters. */
1051
1052 if( is_priv )
1053 {
1054 if( !successive )
1055 {
1056 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1057 &De, NULL ) == 0 );
1058 }
1059 else
1060 {
1061 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1062 NULL, NULL ) == 0 );
1063 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1064 NULL, NULL ) == 0 );
1065 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1066 &De, NULL ) == 0 );
1067 }
1068
1069 if( have_P )
1070 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1071
1072 if( have_Q )
1073 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1074
1075 if( have_D )
1076 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1077
1078 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001079 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1080 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001081 }
1082
1083exit:
1084
1085 mbedtls_rsa_free( &ctx );
1086
1087 mbedtls_mpi_free( &N );
1088 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1089 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1090
1091 mbedtls_mpi_free( &Ne );
1092 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1093 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1094}
1095/* END_CASE */
1096
Hanno Beckerce002632017-08-23 13:22:36 +01001097/* BEGIN_CASE */
Hanno Becker750e8b42017-08-25 07:54:27 +01001098void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1099 int radix_P, char *input_P,
1100 int radix_Q, char *input_Q,
1101 int radix_D, char *input_D,
1102 int radix_E, char *input_E,
1103 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001104{
1105 /* Original MPI's with which we set up the RSA context */
1106 mbedtls_mpi N, P, Q, D, E;
1107
1108 const int have_N = ( strlen( input_N ) > 0 );
1109 const int have_P = ( strlen( input_P ) > 0 );
1110 const int have_Q = ( strlen( input_Q ) > 0 );
1111 const int have_D = ( strlen( input_D ) > 0 );
1112 const int have_E = ( strlen( input_E ) > 0 );
1113
1114 mbedtls_entropy_context entropy;
1115 mbedtls_ctr_drbg_context ctr_drbg;
1116 const char *pers = "test_suite_rsa";
1117
1118 mbedtls_mpi_init( &N );
1119 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1120 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1121
1122 mbedtls_ctr_drbg_init( &ctr_drbg );
1123 mbedtls_entropy_init( &entropy );
1124 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1125 &entropy, (const unsigned char *) pers,
1126 strlen( pers ) ) == 0 );
1127
1128 if( have_N )
1129 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1130
1131 if( have_P )
1132 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1133
1134 if( have_Q )
1135 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1136
1137 if( have_D )
1138 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1139
1140 if( have_E )
1141 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1142
Hanno Becker750e8b42017-08-25 07:54:27 +01001143 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1144 have_P ? &P : NULL,
1145 have_Q ? &Q : NULL,
1146 have_D ? &D : NULL,
1147 have_E ? &E : NULL,
1148 prng ? mbedtls_ctr_drbg_random : NULL,
1149 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001150exit:
1151
1152 mbedtls_ctr_drbg_free( &ctr_drbg );
1153 mbedtls_entropy_free( &entropy );
1154
1155 mbedtls_mpi_free( &N );
1156 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1157 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1158}
1159/* END_CASE */
1160
Hanno Beckerc77ab892017-08-23 11:01:06 +01001161/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001162void mbedtls_rsa_export_raw( char *input_N, char *input_P,
1163 char *input_Q, char *input_D,
Hanno Beckere1582a82017-09-29 11:51:05 +01001164 char *input_E, int is_priv,
1165 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001166{
1167 /* Original raw buffers with which we set up the RSA context */
1168 unsigned char bufN[1000];
1169 unsigned char bufP[1000];
1170 unsigned char bufQ[1000];
1171 unsigned char bufD[1000];
1172 unsigned char bufE[1000];
1173
1174 size_t lenN = 0;
1175 size_t lenP = 0;
1176 size_t lenQ = 0;
1177 size_t lenD = 0;
1178 size_t lenE = 0;
1179
1180 /* Exported buffers */
1181 unsigned char bufNe[ sizeof( bufN ) ];
1182 unsigned char bufPe[ sizeof( bufP ) ];
1183 unsigned char bufQe[ sizeof( bufQ ) ];
1184 unsigned char bufDe[ sizeof( bufD ) ];
1185 unsigned char bufEe[ sizeof( bufE ) ];
1186
1187 const int have_N = ( strlen( input_N ) > 0 );
1188 const int have_P = ( strlen( input_P ) > 0 );
1189 const int have_Q = ( strlen( input_Q ) > 0 );
1190 const int have_D = ( strlen( input_D ) > 0 );
1191 const int have_E = ( strlen( input_E ) > 0 );
1192
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001193 mbedtls_rsa_context ctx;
1194
1195 mbedtls_rsa_init( &ctx, 0, 0 );
1196
1197 /* Setup RSA context */
1198
1199 if( have_N )
1200 lenN = unhexify( bufN, input_N );
1201
1202 if( have_P )
1203 lenP = unhexify( bufP, input_P );
1204
1205 if( have_Q )
1206 lenQ = unhexify( bufQ, input_Q );
1207
1208 if( have_D )
1209 lenD = unhexify( bufD, input_D );
1210
1211 if( have_E )
1212 lenE = unhexify( bufE, input_E );
1213
1214 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1215 have_N ? bufN : NULL, lenN,
1216 have_P ? bufP : NULL, lenP,
1217 have_Q ? bufQ : NULL, lenQ,
1218 have_D ? bufD : NULL, lenD,
1219 have_E ? bufE : NULL, lenE ) == 0 );
1220
Hanno Becker7f25f852017-10-10 16:56:22 +01001221 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001222
1223 /*
1224 * Export parameters and compare to original ones.
1225 */
1226
1227 /* N and E must always be present. */
1228 if( !successive )
1229 {
1230 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, lenN,
1231 NULL, 0, NULL, 0, NULL, 0,
1232 bufEe, lenE ) == 0 );
1233 }
1234 else
1235 {
1236 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, lenN,
1237 NULL, 0, NULL, 0, NULL, 0,
1238 NULL, 0 ) == 0 );
1239 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1240 NULL, 0, NULL, 0, NULL, 0,
1241 bufEe, lenE ) == 0 );
1242 }
1243 TEST_ASSERT( memcmp( bufN, bufNe, lenN ) == 0 );
1244 TEST_ASSERT( memcmp( bufE, bufEe, lenE ) == 0 );
1245
1246 /* If we were providing enough information to setup a complete private context,
1247 * we expect to be able to export all core parameters. */
1248
1249 if( is_priv )
1250 {
1251 if( !successive )
1252 {
1253 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1254 bufPe, lenP ? lenP : sizeof( bufPe ),
1255 bufQe, lenQ ? lenQ : sizeof( bufQe ),
1256 bufDe, lenD ? lenD : sizeof( bufDe ),
1257 NULL, 0 ) == 0 );
1258 }
1259 else
1260 {
1261 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1262 bufPe, lenP ? lenP : sizeof( bufPe ),
1263 NULL, 0, NULL, 0,
1264 NULL, 0 ) == 0 );
1265
1266 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
1267 bufQe, lenQ ? lenQ : sizeof( bufQe ),
1268 NULL, 0, NULL, 0 ) == 0 );
1269
1270 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
1271 NULL, 0, bufDe, lenD ? lenD : sizeof( bufDe ),
1272 NULL, 0 ) == 0 );
1273 }
1274
1275 if( have_P )
1276 TEST_ASSERT( memcmp( bufP, bufPe, lenP ) == 0 );
1277
1278 if( have_Q )
1279 TEST_ASSERT( memcmp( bufQ, bufQe, lenQ ) == 0 );
1280
1281 if( have_D )
1282 TEST_ASSERT( memcmp( bufD, bufDe, lenD ) == 0 );
1283
1284 }
1285
1286exit:
1287 mbedtls_rsa_free( &ctx );
1288}
1289/* END_CASE */
1290
1291/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001292void mbedtls_rsa_import_raw( char *input_N,
1293 char *input_P, char *input_Q,
1294 char *input_D, char *input_E,
1295 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001296 int is_priv,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001297 int result )
1298{
1299 unsigned char bufN[1000];
1300 unsigned char bufP[1000];
1301 unsigned char bufQ[1000];
1302 unsigned char bufD[1000];
1303 unsigned char bufE[1000];
1304
Hanno Beckere1582a82017-09-29 11:51:05 +01001305 /* Buffers used for encryption-decryption test */
1306 unsigned char *buf_orig = NULL;
1307 unsigned char *buf_enc = NULL;
1308 unsigned char *buf_dec = NULL;
1309
Hanno Beckerc77ab892017-08-23 11:01:06 +01001310 size_t lenN = 0;
1311 size_t lenP = 0;
1312 size_t lenQ = 0;
1313 size_t lenD = 0;
1314 size_t lenE = 0;
1315
1316 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001317 mbedtls_entropy_context entropy;
1318 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001319
Hanno Beckerc77ab892017-08-23 11:01:06 +01001320 const char *pers = "test_suite_rsa";
1321
1322 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001323 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001324 mbedtls_rsa_init( &ctx, 0, 0 );
1325
Hanno Beckerc77ab892017-08-23 11:01:06 +01001326 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1327 &entropy, (const unsigned char *) pers,
1328 strlen( pers ) ) == 0 );
1329
Hanno Beckerc77ab892017-08-23 11:01:06 +01001330 if( strlen( input_N ) )
1331 lenN = unhexify( bufN, input_N );
1332
1333 if( strlen( input_P ) )
1334 lenP = unhexify( bufP, input_P );
1335
1336 if( strlen( input_Q ) )
1337 lenQ = unhexify( bufQ, input_Q );
1338
1339 if( strlen( input_D ) )
1340 lenD = unhexify( bufD, input_D );
1341
1342 if( strlen( input_E ) )
1343 lenE = unhexify( bufE, input_E );
1344
1345 if( !successive )
1346 {
1347 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1348 ( lenN > 0 ) ? bufN : NULL, lenN,
1349 ( lenP > 0 ) ? bufP : NULL, lenP,
1350 ( lenQ > 0 ) ? bufQ : NULL, lenQ,
1351 ( lenD > 0 ) ? bufD : NULL, lenD,
1352 ( lenE > 0 ) ? bufE : NULL, lenE ) == 0 );
1353 }
1354 else
1355 {
1356 /* Import N, P, Q, D, E separately.
1357 * This should make no functional difference. */
1358
1359 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1360 ( lenN > 0 ) ? bufN : NULL, lenN,
1361 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1362
1363 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1364 NULL, 0,
1365 ( lenP > 0 ) ? bufP : NULL, lenP,
1366 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1367
1368 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1369 NULL, 0, NULL, 0,
1370 ( lenQ > 0 ) ? bufQ : NULL, lenQ,
1371 NULL, 0, NULL, 0 ) == 0 );
1372
1373 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1374 NULL, 0, NULL, 0, NULL, 0,
1375 ( lenD > 0 ) ? bufD : NULL, lenD,
1376 NULL, 0 ) == 0 );
1377
1378 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1379 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1380 ( lenE > 0 ) ? bufE : NULL, lenE ) == 0 );
1381 }
1382
Hanno Becker7f25f852017-10-10 16:56:22 +01001383 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == result );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001384
Hanno Beckere1582a82017-09-29 11:51:05 +01001385 /* On expected success, perform some public and private
1386 * key operations to check if the key is working properly. */
1387 if( result == 0 )
1388 {
1389 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
1390
1391 /* Did we expect a full private key to be setup? */
1392 if( is_priv )
1393 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
1394
1395 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1396 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1397 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1398 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1399 goto exit;
1400
1401 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1402 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1403
1404 /* Make sure the number we're generating is smaller than the modulus */
1405 buf_orig[0] = 0x00;
1406
1407 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1408
1409 if( is_priv )
1410 {
1411 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1412 &ctr_drbg, buf_enc,
1413 buf_dec ) == 0 );
1414
1415 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1416 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1417 }
1418 }
1419
Hanno Beckerc77ab892017-08-23 11:01:06 +01001420exit:
1421
Hanno Becker3f3ae852017-10-02 10:08:39 +01001422 mbedtls_free( buf_orig );
1423 mbedtls_free( buf_enc );
1424 mbedtls_free( buf_dec );
1425
Hanno Beckerc77ab892017-08-23 11:01:06 +01001426 mbedtls_rsa_free( &ctx );
1427
1428 mbedtls_ctr_drbg_free( &ctr_drbg );
1429 mbedtls_entropy_free( &entropy );
1430
1431}
1432/* END_CASE */
1433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +02001435void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +00001436{
Andres AG93012e82016-09-09 09:10:28 +01001437 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001438}
Paul Bakker33b43f12013-08-20 11:48:36 +02001439/* END_CASE */