blob: 639bcb89dcd475b103117484e9b556239a580f19 [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é-Gonnardfbf09152014-02-03 11:58:55 +0100166 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100168 {
169 memset( output, 0x00, 1000 );
170 memset( output_str, 0x00, 1000 );
171
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100172 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100174 hash_len, hash_result, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100175
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100176#if !defined(MBEDTLS_RSA_ALT)
177 TEST_ASSERT( res == 0 );
178#else
179 TEST_ASSERT( ( res == 0 ) ||
180 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
181#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100182
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100183 if( res == 0 )
184 {
185 hexify( output_str, output, ctx.len );
186 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
187 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100188 }
189
Paul Bakkerbd51b262014-07-10 15:26:12 +0200190exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100191 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
192 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000195}
Paul Bakker33b43f12013-08-20 11:48:36 +0200196/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000197
Paul Bakker33b43f12013-08-20 11:48:36 +0200198/* BEGIN_CASE */
199void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string,
200 int padding_mode, int mod, int radix_N,
201 char *input_N, int radix_E, char *input_E,
202 char *result_hex_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000203{
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100204 int res;
Paul Bakker821fb082009-07-12 13:26:42 +0000205 unsigned char message_str[1000];
206 unsigned char hash_result[1000];
207 unsigned char result_str[1000];
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100208 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_rsa_context ctx;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100210 size_t hash_len, olen;
Paul Bakker821fb082009-07-12 13:26:42 +0000211
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100212 mbedtls_mpi N, E;
213 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000216 memset( message_str, 0x00, 1000 );
217 memset( hash_result, 0x00, 1000 );
218 memset( result_str, 0x00, 1000 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100219 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000220
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100221 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
222 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000223
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100224 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
225 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000227
Paul Bakker33b43f12013-08-20 11:48:36 +0200228 unhexify( message_str, message_hex_string );
229 hash_len = unhexify( hash_result, hash_result_string );
230 unhexify( result_str, result_hex_str );
Paul Bakker821fb082009-07-12 13:26:42 +0000231
Hanno Becker8fd55482017-08-23 14:07:48 +0100232 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
233 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE,
234 hash_len, hash_result,
235 result_str ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100236
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100237 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100239 {
240 int ok;
241
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100242 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100244 &olen, result_str, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100245
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100246#if !defined(MBEDTLS_RSA_ALT)
247 TEST_ASSERT( res == 0 );
248#else
249 TEST_ASSERT( ( res == 0 ) ||
250 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
251#endif
252
253 if( res == 0 )
254 {
255 ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0;
256 if( correct == 0 )
257 TEST_ASSERT( ok == 1 );
258 else
259 TEST_ASSERT( ok == 0 );
260 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100261 }
262
Paul Bakkerbd51b262014-07-10 15:26:12 +0200263exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100264 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000266}
Paul Bakker33b43f12013-08-20 11:48:36 +0200267/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000268
Paul Bakker33b43f12013-08-20 11:48:36 +0200269/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270void mbedtls_rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200271 int radix_N, char *input_N, int radix_E, char *input_E,
272 char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000273{
274 unsigned char message_str[1000];
275 unsigned char output[1000];
276 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000278 size_t msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000279 rnd_pseudo_info rnd_info;
280
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100281 mbedtls_mpi N, E;
282 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
283
Paul Bakker997bbd12011-03-13 15:45:42 +0000284 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000287 memset( message_str, 0x00, 1000 );
288 memset( output, 0x00, 1000 );
289 memset( output_str, 0x00, 1000 );
290
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100291 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
292 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000293
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100294 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
295 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000297
Paul Bakker33b43f12013-08-20 11:48:36 +0200298 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000299
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100300 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
301 MBEDTLS_RSA_PUBLIC, msg_len,
302 message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200303 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000304 {
305 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000306
Paul Bakker33b43f12013-08-20 11:48:36 +0200307 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000308 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100309
Paul Bakkerbd51b262014-07-10 15:26:12 +0200310exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100311 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000313}
Paul Bakker33b43f12013-08-20 11:48:36 +0200314/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000315
Paul Bakker33b43f12013-08-20 11:48:36 +0200316/* BEGIN_CASE */
317void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode,
318 int mod, int radix_N, char *input_N,
319 int radix_E, char *input_E,
320 char *result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000321{
322 unsigned char message_str[1000];
323 unsigned char output[1000];
324 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000326 size_t msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000327
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100328 mbedtls_mpi N, E;
329
330 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000332 memset( message_str, 0x00, 1000 );
333 memset( output, 0x00, 1000 );
334 memset( output_str, 0x00, 1000 );
335
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100336 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
337 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000338
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100339 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
340 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000342
Paul Bakker33b43f12013-08-20 11:48:36 +0200343 msg_len = unhexify( message_str, message_hex_string );
Paul Bakkera6656852010-07-18 19:47:14 +0000344
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100345 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL,
346 MBEDTLS_RSA_PUBLIC, msg_len,
347 message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200348 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000349 {
350 hexify( output_str, output, ctx.len );
351
Paul Bakker33b43f12013-08-20 11:48:36 +0200352 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000353 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100354
Paul Bakkerbd51b262014-07-10 15:26:12 +0200355exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100356 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000358}
Paul Bakker33b43f12013-08-20 11:48:36 +0200359/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000360
Paul Bakker33b43f12013-08-20 11:48:36 +0200361/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362void mbedtls_rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200363 int radix_P, char *input_P, int radix_Q, char *input_Q,
364 int radix_N, char *input_N, int radix_E, char *input_E,
365 int max_output, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000366{
367 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000368 unsigned char output[1000];
369 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000371 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200372 rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100373 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000374
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100375 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
376 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000379
380 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000381 memset( output, 0x00, 1000 );
382 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200383 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000384
Paul Bakker42a29bf2009-07-07 20:18:41 +0000385
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100386 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
387 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
388 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
389 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000390
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100391 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
392 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100393 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000395
Paul Bakker33b43f12013-08-20 11:48:36 +0200396 unhexify( message_str, message_hex_string );
Paul Bakker69998dd2009-07-11 19:15:20 +0000397 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 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 +0200400 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000401 {
402 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000403
Paul Bakker33b43f12013-08-20 11:48:36 +0200404 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000405 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000406
Paul Bakkerbd51b262014-07-10 15:26:12 +0200407exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100408 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
409 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000411}
Paul Bakker33b43f12013-08-20 11:48:36 +0200412/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000413
Paul Bakker33b43f12013-08-20 11:48:36 +0200414/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415void mbedtls_rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N,
Paul Bakker33b43f12013-08-20 11:48:36 +0200416 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000417{
418 unsigned char message_str[1000];
419 unsigned char output[1000];
420 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000422
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100423 mbedtls_mpi N, E;
424
425 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
427 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000428 memset( message_str, 0x00, 1000 );
429 memset( output, 0x00, 1000 );
430 memset( output_str, 0x00, 1000 );
431
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100432 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
433 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000434
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100435 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
436 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000438
Paul Bakker33b43f12013-08-20 11:48:36 +0200439 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200442 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000443 {
444 hexify( output_str, output, ctx.len );
445
Paul Bakker33b43f12013-08-20 11:48:36 +0200446 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000447 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100448
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100449 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200451 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100455
456 memset( output, 0x00, 1000 );
457 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100459 if( result == 0 )
460 {
461 hexify( output_str, output, ctx2.len );
462
463 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
464 }
465
Paul Bakkerbd51b262014-07-10 15:26:12 +0200466exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100467 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_rsa_free( &ctx );
469 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000470}
Paul Bakker33b43f12013-08-20 11:48:36 +0200471/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000472
Paul Bakker33b43f12013-08-20 11:48:36 +0200473/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474void mbedtls_rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
Paul Bakker33b43f12013-08-20 11:48:36 +0200475 int radix_Q, char *input_Q, int radix_N, char *input_N,
476 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000477{
478 unsigned char message_str[1000];
479 unsigned char output[1000];
480 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100482 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200483 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200484 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000485
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100486 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
487 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
489 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000490
491 memset( message_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200492 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000493
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100494 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
495 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
496 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
497 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000498
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100499 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
500 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100501 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000503
Paul Bakker33b43f12013-08-20 11:48:36 +0200504 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000505
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200506 /* repeat three times to test updating of blinding values */
507 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000508 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200509 memset( output, 0x00, 1000 );
510 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200512 message_str, output ) == result );
513 if( result == 0 )
514 {
515 hexify( output_str, output, ctx.len );
Paul Bakker821fb082009-07-12 13:26:42 +0000516
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200517 TEST_ASSERT( strcasecmp( (char *) output_str,
518 result_hex_str ) == 0 );
519 }
Paul Bakker821fb082009-07-12 13:26:42 +0000520 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000521
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100522 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200524 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100528
529 memset( output, 0x00, 1000 );
530 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100532 message_str, output ) == result );
533 if( result == 0 )
534 {
535 hexify( output_str, output, ctx2.len );
536
537 TEST_ASSERT( strcasecmp( (char *) output_str,
538 result_hex_str ) == 0 );
539 }
540
Paul Bakkerbd51b262014-07-10 15:26:12 +0200541exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100542 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
543 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000546}
Paul Bakker33b43f12013-08-20 11:48:36 +0200547/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000548
Paul Bakker33b43f12013-08-20 11:48:36 +0200549/* BEGIN_CASE */
550void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_rsa_context ctx;
553 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000556}
Paul Bakker33b43f12013-08-20 11:48:36 +0200557/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000558
Paul Bakker33b43f12013-08-20 11:48:36 +0200559/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560void mbedtls_rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
Paul Bakker33b43f12013-08-20 11:48:36 +0200561 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000562{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100564 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000565
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100566 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000568
Paul Bakker33b43f12013-08-20 11:48:36 +0200569 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000570 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100571 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000572 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200573 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000574 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100575 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000576 }
577
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100578 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100580
Paul Bakkerbd51b262014-07-10 15:26:12 +0200581exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100582 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000584}
Paul Bakker33b43f12013-08-20 11:48:36 +0200585/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000586
Paul Bakker33b43f12013-08-20 11:48:36 +0200587/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588void mbedtls_rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
Paul Bakker33b43f12013-08-20 11:48:36 +0200589 char *input_Q, int radix_N, char *input_N,
590 int radix_E, char *input_E, int radix_D, char *input_D,
591 int radix_DP, char *input_DP, int radix_DQ,
592 char *input_DQ, int radix_QP, char *input_QP,
593 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000594{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000598
Paul Bakker33b43f12013-08-20 11:48:36 +0200599 ctx.len = mod / 8;
600 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000603 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200604 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000607 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200608 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000611 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200612 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000615 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200616 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000619 }
Hanno Becker131134f2017-08-23 08:31:07 +0100620#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200621 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000624 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200625 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000628 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200629 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000632 }
Hanno Becker131134f2017-08-23 08:31:07 +0100633#else
634 ((void) radix_DP); ((void) input_DP);
635 ((void) radix_DQ); ((void) input_DQ);
636 ((void) radix_QP); ((void) input_QP);
637#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100640
Paul Bakkerbd51b262014-07-10 15:26:12 +0200641exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000643}
Paul Bakker33b43f12013-08-20 11:48:36 +0200644/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000645
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100646/* BEGIN_CASE */
647void rsa_check_pubpriv( int mod, int radix_Npub, char *input_Npub,
648 int radix_Epub, char *input_Epub,
649 int radix_P, char *input_P, int radix_Q,
650 char *input_Q, int radix_N, char *input_N,
651 int radix_E, char *input_E, int radix_D, char *input_D,
652 int radix_DP, char *input_DP, int radix_DQ,
653 char *input_DQ, int radix_QP, char *input_QP,
654 int result )
655{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
659 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100660
661 pub.len = mod / 8;
662 prv.len = mod / 8;
663
664 if( strlen( input_Npub ) )
665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100667 }
668 if( strlen( input_Epub ) )
669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100671 }
672
673 if( strlen( input_P ) )
674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100676 }
677 if( strlen( input_Q ) )
678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100680 }
681 if( strlen( input_N ) )
682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100684 }
685 if( strlen( input_E ) )
686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100688 }
689 if( strlen( input_D ) )
690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100692 }
Hanno Becker131134f2017-08-23 08:31:07 +0100693#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100694 if( strlen( input_DP ) )
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100697 }
698 if( strlen( input_DQ ) )
699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100701 }
702 if( strlen( input_QP ) )
703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100705 }
Hanno Becker131134f2017-08-23 08:31:07 +0100706#else
707 ((void) radix_DP); ((void) input_DP);
708 ((void) radix_DQ); ((void) input_DQ);
709 ((void) radix_QP); ((void) input_QP);
710#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100713
714exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_rsa_free( &pub );
716 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100717}
718/* END_CASE */
719
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100720/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000722{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_rsa_context ctx;
724 mbedtls_entropy_context entropy;
725 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200726 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000727
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200728 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +0100730 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000731
Hanno Beckera47023e2017-12-22 17:08:03 +0000732 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
733 &entropy, (const unsigned char *) pers,
734 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200737 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100740 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000741 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100742
Paul Bakkerbd51b262014-07-10 15:26:12 +0200743exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_rsa_free( &ctx );
745 mbedtls_ctr_drbg_free( &ctr_drbg );
746 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000747}
Paul Bakker33b43f12013-08-20 11:48:36 +0200748/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000749
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100750/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +0100751void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100752 int radix_D, char *input_D,
753 int radix_E, char *input_E,
754 int radix_P, char *output_P,
755 int radix_Q, char *output_Q,
756 int corrupt, int result )
757{
758 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
759
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100760 mbedtls_mpi_init( &N );
761 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
762 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
763 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
764
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100765 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
766 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
767 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
768 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
769 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
770
771 if( corrupt )
772 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
773
774 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100775 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100776
777 if( !corrupt )
778 {
779 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
780 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
781 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
782 }
783
784exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100785 mbedtls_mpi_free( &N );
786 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
787 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
788 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100789}
790/* END_CASE */
791
Hanno Becker6b4ce492017-08-23 11:00:21 +0100792/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100793void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
794 int radix_Q, char *input_Q,
795 int radix_E, char *input_E,
796 int radix_D, char *output_D,
797 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +0100798{
799 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
800
801 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
802 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
803 mbedtls_mpi_init( &E );
804 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
805
806 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
807 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
808 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
809 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
810
811 if( corrupt )
812 {
813 /* Make E even */
814 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
815 }
816
817 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100818 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
819 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100820
821 if( !corrupt )
822 {
823 /*
824 * Check that D and Dp agree modulo LCM(P-1, Q-1).
825 */
826
827 /* Replace P,Q by P-1, Q-1 */
828 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
829 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
830
831 /* Check D == Dp modulo P-1 */
832 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
833 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
834 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
835
836 /* Check D == Dp modulo Q-1 */
837 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
838 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
839 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
840 }
841
842exit:
843
844 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
845 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
846 mbedtls_mpi_free( &E );
847 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
848}
849/* END_CASE */
850
Hanno Beckerf40cdf92017-12-22 11:03:27 +0000851/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +0100852void mbedtls_rsa_import( int radix_N, char *input_N,
853 int radix_P, char *input_P,
854 int radix_Q, char *input_Q,
855 int radix_D, char *input_D,
856 int radix_E, char *input_E,
857 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +0100858 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +0100859 int res_check,
860 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100861{
862 mbedtls_mpi N, P, Q, D, E;
863 mbedtls_rsa_context ctx;
864
Hanno Beckere1582a82017-09-29 11:51:05 +0100865 /* Buffers used for encryption-decryption test */
866 unsigned char *buf_orig = NULL;
867 unsigned char *buf_enc = NULL;
868 unsigned char *buf_dec = NULL;
869
Hanno Beckerc77ab892017-08-23 11:01:06 +0100870 mbedtls_entropy_context entropy;
871 mbedtls_ctr_drbg_context ctr_drbg;
872 const char *pers = "test_suite_rsa";
873
Hanno Becker4d6e8342017-09-29 11:50:18 +0100874 const int have_N = ( strlen( input_N ) > 0 );
875 const int have_P = ( strlen( input_P ) > 0 );
876 const int have_Q = ( strlen( input_Q ) > 0 );
877 const int have_D = ( strlen( input_D ) > 0 );
878 const int have_E = ( strlen( input_E ) > 0 );
879
Hanno Beckerc77ab892017-08-23 11:01:06 +0100880 mbedtls_ctr_drbg_init( &ctr_drbg );
881
882 mbedtls_entropy_init( &entropy );
883 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
884 (const unsigned char *) pers, strlen( pers ) ) == 0 );
885
886 mbedtls_rsa_init( &ctx, 0, 0 );
887
888 mbedtls_mpi_init( &N );
889 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
890 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
891
Hanno Becker4d6e8342017-09-29 11:50:18 +0100892 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100893 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
894
Hanno Becker4d6e8342017-09-29 11:50:18 +0100895 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100896 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
897
Hanno Becker4d6e8342017-09-29 11:50:18 +0100898 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100899 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
900
Hanno Becker4d6e8342017-09-29 11:50:18 +0100901 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100902 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
903
Hanno Becker4d6e8342017-09-29 11:50:18 +0100904 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100905 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
906
907 if( !successive )
908 {
909 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100910 have_N ? &N : NULL,
911 have_P ? &P : NULL,
912 have_Q ? &Q : NULL,
913 have_D ? &D : NULL,
914 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100915 }
916 else
917 {
918 /* Import N, P, Q, D, E separately.
919 * This should make no functional difference. */
920
921 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100922 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100923 NULL, NULL, NULL, NULL ) == 0 );
924
925 TEST_ASSERT( mbedtls_rsa_import( &ctx,
926 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100927 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100928 NULL, NULL, NULL ) == 0 );
929
930 TEST_ASSERT( mbedtls_rsa_import( &ctx,
931 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100932 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100933 NULL, NULL ) == 0 );
934
935 TEST_ASSERT( mbedtls_rsa_import( &ctx,
936 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100937 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100938 NULL ) == 0 );
939
940 TEST_ASSERT( mbedtls_rsa_import( &ctx,
941 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100942 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100943 }
944
Hanno Becker04877a42017-10-11 10:01:33 +0100945 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100946
Hanno Beckere1582a82017-09-29 11:51:05 +0100947 /* On expected success, perform some public and private
948 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +0100949 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +0100950 {
Hanno Beckere1582a82017-09-29 11:51:05 +0100951 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +0100952 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
953 else
954 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
955
956 if( res_check != 0 )
957 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +0100958
959 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
960 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
961 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
962 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
963 goto exit;
964
965 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
966 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
967
968 /* Make sure the number we're generating is smaller than the modulus */
969 buf_orig[0] = 0x00;
970
971 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
972
973 if( is_priv )
974 {
975 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
976 &ctr_drbg, buf_enc,
977 buf_dec ) == 0 );
978
979 TEST_ASSERT( memcmp( buf_orig, buf_dec,
980 mbedtls_rsa_get_len( &ctx ) ) == 0 );
981 }
982 }
983
Hanno Beckerc77ab892017-08-23 11:01:06 +0100984exit:
985
Hanno Beckere1582a82017-09-29 11:51:05 +0100986 mbedtls_free( buf_orig );
987 mbedtls_free( buf_enc );
988 mbedtls_free( buf_dec );
989
Hanno Beckerc77ab892017-08-23 11:01:06 +0100990 mbedtls_rsa_free( &ctx );
991
992 mbedtls_ctr_drbg_free( &ctr_drbg );
993 mbedtls_entropy_free( &entropy );
994
995 mbedtls_mpi_free( &N );
996 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
997 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
998}
999/* END_CASE */
1000
Hanno Becker417f2d62017-08-23 11:44:51 +01001001/* BEGIN_CASE */
1002void mbedtls_rsa_export( int radix_N, char *input_N,
1003 int radix_P, char *input_P,
1004 int radix_Q, char *input_Q,
1005 int radix_D, char *input_D,
1006 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001007 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001008 int successive )
1009{
1010 /* Original MPI's with which we set up the RSA context */
1011 mbedtls_mpi N, P, Q, D, E;
1012
1013 /* Exported MPI's */
1014 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1015
1016 const int have_N = ( strlen( input_N ) > 0 );
1017 const int have_P = ( strlen( input_P ) > 0 );
1018 const int have_Q = ( strlen( input_Q ) > 0 );
1019 const int have_D = ( strlen( input_D ) > 0 );
1020 const int have_E = ( strlen( input_E ) > 0 );
1021
Hanno Becker417f2d62017-08-23 11:44:51 +01001022 mbedtls_rsa_context ctx;
1023
1024 mbedtls_rsa_init( &ctx, 0, 0 );
1025
1026 mbedtls_mpi_init( &N );
1027 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1028 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1029
1030 mbedtls_mpi_init( &Ne );
1031 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1032 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1033
1034 /* Setup RSA context */
1035
1036 if( have_N )
1037 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1038
1039 if( have_P )
1040 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1041
1042 if( have_Q )
1043 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1044
1045 if( have_D )
1046 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1047
1048 if( have_E )
1049 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1050
1051 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1052 strlen( input_N ) ? &N : NULL,
1053 strlen( input_P ) ? &P : NULL,
1054 strlen( input_Q ) ? &Q : NULL,
1055 strlen( input_D ) ? &D : NULL,
1056 strlen( input_E ) ? &E : NULL ) == 0 );
1057
Hanno Becker7f25f852017-10-10 16:56:22 +01001058 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001059
1060 /*
1061 * Export parameters and compare to original ones.
1062 */
1063
1064 /* N and E must always be present. */
1065 if( !successive )
1066 {
1067 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1068 }
1069 else
1070 {
1071 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1072 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1073 }
1074 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1075 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1076
1077 /* If we were providing enough information to setup a complete private context,
1078 * we expect to be able to export all core parameters. */
1079
1080 if( is_priv )
1081 {
1082 if( !successive )
1083 {
1084 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1085 &De, NULL ) == 0 );
1086 }
1087 else
1088 {
1089 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1090 NULL, NULL ) == 0 );
1091 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1092 NULL, NULL ) == 0 );
1093 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1094 &De, NULL ) == 0 );
1095 }
1096
1097 if( have_P )
1098 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1099
1100 if( have_Q )
1101 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1102
1103 if( have_D )
1104 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1105
1106 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001107 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1108 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001109 }
1110
1111exit:
1112
1113 mbedtls_rsa_free( &ctx );
1114
1115 mbedtls_mpi_free( &N );
1116 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1117 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1118
1119 mbedtls_mpi_free( &Ne );
1120 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1121 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1122}
1123/* END_CASE */
1124
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001125/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Becker750e8b42017-08-25 07:54:27 +01001126void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1127 int radix_P, char *input_P,
1128 int radix_Q, char *input_Q,
1129 int radix_D, char *input_D,
1130 int radix_E, char *input_E,
1131 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001132{
1133 /* Original MPI's with which we set up the RSA context */
1134 mbedtls_mpi N, P, Q, D, E;
1135
1136 const int have_N = ( strlen( input_N ) > 0 );
1137 const int have_P = ( strlen( input_P ) > 0 );
1138 const int have_Q = ( strlen( input_Q ) > 0 );
1139 const int have_D = ( strlen( input_D ) > 0 );
1140 const int have_E = ( strlen( input_E ) > 0 );
1141
1142 mbedtls_entropy_context entropy;
1143 mbedtls_ctr_drbg_context ctr_drbg;
1144 const char *pers = "test_suite_rsa";
1145
1146 mbedtls_mpi_init( &N );
1147 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1148 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1149
1150 mbedtls_ctr_drbg_init( &ctr_drbg );
1151 mbedtls_entropy_init( &entropy );
1152 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1153 &entropy, (const unsigned char *) pers,
1154 strlen( pers ) ) == 0 );
1155
1156 if( have_N )
1157 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1158
1159 if( have_P )
1160 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1161
1162 if( have_Q )
1163 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1164
1165 if( have_D )
1166 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1167
1168 if( have_E )
1169 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1170
Hanno Becker750e8b42017-08-25 07:54:27 +01001171 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1172 have_P ? &P : NULL,
1173 have_Q ? &Q : NULL,
1174 have_D ? &D : NULL,
1175 have_E ? &E : NULL,
1176 prng ? mbedtls_ctr_drbg_random : NULL,
1177 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001178exit:
1179
1180 mbedtls_ctr_drbg_free( &ctr_drbg );
1181 mbedtls_entropy_free( &entropy );
1182
1183 mbedtls_mpi_free( &N );
1184 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1185 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1186}
1187/* END_CASE */
1188
Hanno Beckerc77ab892017-08-23 11:01:06 +01001189/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001190void mbedtls_rsa_export_raw( char *input_N, char *input_P,
1191 char *input_Q, char *input_D,
Hanno Beckere1582a82017-09-29 11:51:05 +01001192 char *input_E, int is_priv,
1193 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001194{
1195 /* Original raw buffers with which we set up the RSA context */
1196 unsigned char bufN[1000];
1197 unsigned char bufP[1000];
1198 unsigned char bufQ[1000];
1199 unsigned char bufD[1000];
1200 unsigned char bufE[1000];
1201
1202 size_t lenN = 0;
1203 size_t lenP = 0;
1204 size_t lenQ = 0;
1205 size_t lenD = 0;
1206 size_t lenE = 0;
1207
1208 /* Exported buffers */
1209 unsigned char bufNe[ sizeof( bufN ) ];
1210 unsigned char bufPe[ sizeof( bufP ) ];
1211 unsigned char bufQe[ sizeof( bufQ ) ];
1212 unsigned char bufDe[ sizeof( bufD ) ];
1213 unsigned char bufEe[ sizeof( bufE ) ];
1214
1215 const int have_N = ( strlen( input_N ) > 0 );
1216 const int have_P = ( strlen( input_P ) > 0 );
1217 const int have_Q = ( strlen( input_Q ) > 0 );
1218 const int have_D = ( strlen( input_D ) > 0 );
1219 const int have_E = ( strlen( input_E ) > 0 );
1220
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001221 mbedtls_rsa_context ctx;
1222
1223 mbedtls_rsa_init( &ctx, 0, 0 );
1224
1225 /* Setup RSA context */
1226
1227 if( have_N )
1228 lenN = unhexify( bufN, input_N );
1229
1230 if( have_P )
1231 lenP = unhexify( bufP, input_P );
1232
1233 if( have_Q )
1234 lenQ = unhexify( bufQ, input_Q );
1235
1236 if( have_D )
1237 lenD = unhexify( bufD, input_D );
1238
1239 if( have_E )
1240 lenE = unhexify( bufE, input_E );
1241
1242 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1243 have_N ? bufN : NULL, lenN,
1244 have_P ? bufP : NULL, lenP,
1245 have_Q ? bufQ : NULL, lenQ,
1246 have_D ? bufD : NULL, lenD,
1247 have_E ? bufE : NULL, lenE ) == 0 );
1248
Hanno Becker7f25f852017-10-10 16:56:22 +01001249 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001250
1251 /*
1252 * Export parameters and compare to original ones.
1253 */
1254
1255 /* N and E must always be present. */
1256 if( !successive )
1257 {
1258 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, lenN,
1259 NULL, 0, NULL, 0, NULL, 0,
1260 bufEe, lenE ) == 0 );
1261 }
1262 else
1263 {
1264 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, lenN,
1265 NULL, 0, NULL, 0, NULL, 0,
1266 NULL, 0 ) == 0 );
1267 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1268 NULL, 0, NULL, 0, NULL, 0,
1269 bufEe, lenE ) == 0 );
1270 }
1271 TEST_ASSERT( memcmp( bufN, bufNe, lenN ) == 0 );
1272 TEST_ASSERT( memcmp( bufE, bufEe, lenE ) == 0 );
1273
1274 /* If we were providing enough information to setup a complete private context,
1275 * we expect to be able to export all core parameters. */
1276
1277 if( is_priv )
1278 {
1279 if( !successive )
1280 {
1281 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1282 bufPe, lenP ? lenP : sizeof( bufPe ),
1283 bufQe, lenQ ? lenQ : sizeof( bufQe ),
1284 bufDe, lenD ? lenD : sizeof( bufDe ),
1285 NULL, 0 ) == 0 );
1286 }
1287 else
1288 {
1289 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1290 bufPe, lenP ? lenP : sizeof( bufPe ),
1291 NULL, 0, NULL, 0,
1292 NULL, 0 ) == 0 );
1293
1294 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
1295 bufQe, lenQ ? lenQ : sizeof( bufQe ),
1296 NULL, 0, NULL, 0 ) == 0 );
1297
1298 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
1299 NULL, 0, bufDe, lenD ? lenD : sizeof( bufDe ),
1300 NULL, 0 ) == 0 );
1301 }
1302
1303 if( have_P )
1304 TEST_ASSERT( memcmp( bufP, bufPe, lenP ) == 0 );
1305
1306 if( have_Q )
1307 TEST_ASSERT( memcmp( bufQ, bufQe, lenQ ) == 0 );
1308
1309 if( have_D )
1310 TEST_ASSERT( memcmp( bufD, bufDe, lenD ) == 0 );
1311
1312 }
1313
1314exit:
1315 mbedtls_rsa_free( &ctx );
1316}
1317/* END_CASE */
1318
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001319/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001320void mbedtls_rsa_import_raw( char *input_N,
1321 char *input_P, char *input_Q,
1322 char *input_D, char *input_E,
1323 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001324 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001325 int res_check,
1326 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001327{
1328 unsigned char bufN[1000];
1329 unsigned char bufP[1000];
1330 unsigned char bufQ[1000];
1331 unsigned char bufD[1000];
1332 unsigned char bufE[1000];
1333
Hanno Beckere1582a82017-09-29 11:51:05 +01001334 /* Buffers used for encryption-decryption test */
1335 unsigned char *buf_orig = NULL;
1336 unsigned char *buf_enc = NULL;
1337 unsigned char *buf_dec = NULL;
1338
Hanno Beckerc77ab892017-08-23 11:01:06 +01001339 size_t lenN = 0;
1340 size_t lenP = 0;
1341 size_t lenQ = 0;
1342 size_t lenD = 0;
1343 size_t lenE = 0;
1344
1345 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001346 mbedtls_entropy_context entropy;
1347 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001348
Hanno Beckerc77ab892017-08-23 11:01:06 +01001349 const char *pers = "test_suite_rsa";
1350
1351 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001352 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001353 mbedtls_rsa_init( &ctx, 0, 0 );
1354
Hanno Beckerc77ab892017-08-23 11:01:06 +01001355 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1356 &entropy, (const unsigned char *) pers,
1357 strlen( pers ) ) == 0 );
1358
Hanno Beckerc77ab892017-08-23 11:01:06 +01001359 if( strlen( input_N ) )
1360 lenN = unhexify( bufN, input_N );
1361
1362 if( strlen( input_P ) )
1363 lenP = unhexify( bufP, input_P );
1364
1365 if( strlen( input_Q ) )
1366 lenQ = unhexify( bufQ, input_Q );
1367
1368 if( strlen( input_D ) )
1369 lenD = unhexify( bufD, input_D );
1370
1371 if( strlen( input_E ) )
1372 lenE = unhexify( bufE, input_E );
1373
1374 if( !successive )
1375 {
1376 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1377 ( lenN > 0 ) ? bufN : NULL, lenN,
1378 ( lenP > 0 ) ? bufP : NULL, lenP,
1379 ( lenQ > 0 ) ? bufQ : NULL, lenQ,
1380 ( lenD > 0 ) ? bufD : NULL, lenD,
1381 ( lenE > 0 ) ? bufE : NULL, lenE ) == 0 );
1382 }
1383 else
1384 {
1385 /* Import N, P, Q, D, E separately.
1386 * This should make no functional difference. */
1387
1388 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1389 ( lenN > 0 ) ? bufN : NULL, lenN,
1390 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1391
1392 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1393 NULL, 0,
1394 ( lenP > 0 ) ? bufP : NULL, lenP,
1395 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1396
1397 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1398 NULL, 0, NULL, 0,
1399 ( lenQ > 0 ) ? bufQ : NULL, lenQ,
1400 NULL, 0, NULL, 0 ) == 0 );
1401
1402 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1403 NULL, 0, NULL, 0, NULL, 0,
1404 ( lenD > 0 ) ? bufD : NULL, lenD,
1405 NULL, 0 ) == 0 );
1406
1407 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1408 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1409 ( lenE > 0 ) ? bufE : NULL, lenE ) == 0 );
1410 }
1411
Hanno Becker04877a42017-10-11 10:01:33 +01001412 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001413
Hanno Beckere1582a82017-09-29 11:51:05 +01001414 /* On expected success, perform some public and private
1415 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001416 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001417 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001418 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001419 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1420 else
1421 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1422
1423 if( res_check != 0 )
1424 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001425
1426 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1427 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1428 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1429 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1430 goto exit;
1431
1432 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1433 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1434
1435 /* Make sure the number we're generating is smaller than the modulus */
1436 buf_orig[0] = 0x00;
1437
1438 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1439
1440 if( is_priv )
1441 {
1442 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1443 &ctr_drbg, buf_enc,
1444 buf_dec ) == 0 );
1445
1446 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1447 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1448 }
1449 }
1450
Hanno Beckerc77ab892017-08-23 11:01:06 +01001451exit:
1452
Hanno Becker3f3ae852017-10-02 10:08:39 +01001453 mbedtls_free( buf_orig );
1454 mbedtls_free( buf_enc );
1455 mbedtls_free( buf_dec );
1456
Hanno Beckerc77ab892017-08-23 11:01:06 +01001457 mbedtls_rsa_free( &ctx );
1458
1459 mbedtls_ctr_drbg_free( &ctr_drbg );
1460 mbedtls_entropy_free( &entropy );
1461
1462}
1463/* END_CASE */
1464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +02001466void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +00001467{
Andres AG93012e82016-09-09 09:10:28 +01001468 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001469}
Paul Bakker33b43f12013-08-20 11:48:36 +02001470/* END_CASE */