blob: bb2df8d93046c5bef7bd9404c9b7b25c9de2ffe5 [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"
Chris Jones66a4cd42021-03-09 16:04:12 +00003#include "rsa_alt_helpers.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/md5.h"
5#include "mbedtls/sha1.h"
6#include "mbedtls/sha256.h"
7#include "mbedtls/sha512.h"
8#include "mbedtls/entropy.h"
9#include "mbedtls/ctr_drbg.h"
Hanno Becker47deec42017-07-24 12:27:09 +010010
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 */
Ronald Cronea7631b2021-06-03 18:51:59 +020019void rsa_invalid_param( )
20{
21 mbedtls_rsa_context ctx;
22 const int invalid_padding = 42;
23 const int invalid_hash_id = 0xff;
24
Ronald Cronc1905a12021-06-05 11:11:14 +020025 mbedtls_rsa_init( &ctx );
Ronald Cronea7631b2021-06-03 18:51:59 +020026
27 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
28 invalid_padding,
29 MBEDTLS_MD_NONE ),
30 MBEDTLS_ERR_RSA_INVALID_PADDING );
31
32 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
33 MBEDTLS_RSA_PKCS_V21,
34 invalid_hash_id ),
35 MBEDTLS_ERR_RSA_INVALID_PADDING );
36
Ronald Cron3a0375f2021-06-08 10:22:28 +020037#if !defined(MBEDTLS_PKCS1_V15)
38 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
39 MBEDTLS_RSA_PKCS_V15,
40 MBEDTLS_MD_NONE ),
41 MBEDTLS_ERR_RSA_INVALID_PADDING );
42#endif
43
44#if !defined(MBEDTLS_PKCS1_V21)
45 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
46 MBEDTLS_RSA_PKCS_V21,
47 MBEDTLS_MD_NONE ),
48 MBEDTLS_ERR_RSA_INVALID_PADDING );
49#endif
50
Ronald Cronea7631b2021-06-03 18:51:59 +020051exit:
52 mbedtls_rsa_free( &ctx );
53}
54/* END_CASE */
55
56/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +010057void rsa_init_free( int reinit )
58{
59 mbedtls_rsa_context ctx;
60
61 /* Double free is not explicitly documented to work, but we rely on it
62 * even inside the library so that you can call mbedtls_rsa_free()
63 * unconditionally on an error path without checking whether it has
64 * already been called in the success path. */
65
Ronald Cronc1905a12021-06-05 11:11:14 +020066 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +010067 mbedtls_rsa_free( &ctx );
68
69 if( reinit )
Ronald Cronc1905a12021-06-05 11:11:14 +020070 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +010071 mbedtls_rsa_free( &ctx );
72
73 /* This test case always succeeds, functionally speaking. A plausible
74 * bug might trigger an invalid pointer dereference or a memory leak. */
75 goto exit;
76}
77/* END_CASE */
78
79/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010080void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +010081 int digest, int mod, int radix_P, char * input_P,
82 int radix_Q, char * input_Q, int radix_N,
83 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +020084 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000085{
Ron Eldorfdc15bd2018-11-22 15:47:51 +020086 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
87 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010089 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +020090 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +000091
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010092 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
93 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +020094 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +020095 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,padding_mode,
96 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000097
Ron Eldorfdc15bd2018-11-22 15:47:51 +020098 memset( hash_result, 0x00, sizeof( hash_result ) );
99 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200100 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000101
Gilles Peskine20edee72021-06-10 23:18:39 +0200102 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
103 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
104 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
105 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000106
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100107 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
108 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100109 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000111
Paul Bakker42a29bf2009-07-07 20:18:41 +0000112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100114 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000115
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200116 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100117 &rnd_info, digest, 0, hash_result,
118 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200119 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000120 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000121
Ronald Cronac6ae352020-06-26 14:33:03 +0200122 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
123 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000124 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000125
Paul Bakkerbd51b262014-07-10 15:26:12 +0200126exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100127 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
128 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000130}
Paul Bakker33b43f12013-08-20 11:48:36 +0200131/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000132
Paul Bakker33b43f12013-08-20 11:48:36 +0200133/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100134void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100135 int digest, int mod, int radix_N,
136 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100137 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000138{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200139 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000141
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100142 mbedtls_mpi N, E;
143
144 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200145 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200146 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
147 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200148 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000149
Gilles Peskine20edee72021-06-10 23:18:39 +0200150 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
151 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100152 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
153 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000155
Paul Bakker42a29bf2009-07-07 20:18:41 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100158 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000159
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100160 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, digest, 0, hash_result, result_str->x ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100161
Paul Bakkerbd51b262014-07-10 15:26:12 +0200162exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100163 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000165}
Paul Bakker33b43f12013-08-20 11:48:36 +0200166/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000167
Paul Bakker821fb082009-07-12 13:26:42 +0000168
Paul Bakker33b43f12013-08-20 11:48:36 +0200169/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100170void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100171 int padding_mode, int mod, int radix_P,
172 char * input_P, int radix_Q, char * input_Q,
173 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200174 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000175{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200176 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100178 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200179 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000180
Ronald Cronc1905a12021-06-05 11:11:14 +0200181 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200182 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
183 MBEDTLS_MD_NONE ) == 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100184 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
185 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000186
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200187 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200188 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000189
Gilles Peskine20edee72021-06-10 23:18:39 +0200190 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
191 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
192 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
193 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000194
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100195 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
196 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100197 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000199
Paul Bakker821fb082009-07-12 13:26:42 +0000200
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200201 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100202 &rnd_info, MBEDTLS_MD_NONE,
203 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200204 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000205
Paul Bakker821fb082009-07-12 13:26:42 +0000206
Ronald Cronac6ae352020-06-26 14:33:03 +0200207 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
208 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000209
Paul Bakkerbd51b262014-07-10 15:26:12 +0200210exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100211 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
212 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000215}
Paul Bakker33b43f12013-08-20 11:48:36 +0200216/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000217
Paul Bakker33b43f12013-08-20 11:48:36 +0200218/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100219void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200220 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100221 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100222 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000223{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200224 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000226
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100227 mbedtls_mpi N, E;
228 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
229
Ronald Cronc1905a12021-06-05 11:11:14 +0200230 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200231 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
232 MBEDTLS_MD_NONE ) == 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100233 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000234
Gilles Peskine20edee72021-06-10 23:18:39 +0200235 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
236 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000237
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100238 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
239 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000241
Paul Bakker821fb082009-07-12 13:26:42 +0000242
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100243 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, MBEDTLS_MD_NONE, hash_result->len, hash_result->x, result_str->x ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100244
Paul Bakkerbd51b262014-07-10 15:26:12 +0200245exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100246 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000248}
Paul Bakker33b43f12013-08-20 11:48:36 +0200249/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000250
Paul Bakker33b43f12013-08-20 11:48:36 +0200251/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100252void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100253 int mod, int radix_N, char * input_N,
254 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200255 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000256{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200257 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200259 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000260
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100261 mbedtls_mpi N, E;
262 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
263
Ronald Cron351f0ee2020-06-10 12:12:18 +0200264 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000265
Ronald Cronc1905a12021-06-05 11:11:14 +0200266 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200267 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
268 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200269 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000270
Gilles Peskine20edee72021-06-10 23:18:39 +0200271 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
272 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000273
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100274 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
275 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000277
Paul Bakker42a29bf2009-07-07 20:18:41 +0000278
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200279 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
280 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100281 &rnd_info, message_str->len,
282 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200283 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200284 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000285 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000286
Ronald Cronac6ae352020-06-26 14:33:03 +0200287 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
288 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000289 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100290
Paul Bakkerbd51b262014-07-10 15:26:12 +0200291exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100292 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000294}
Paul Bakker33b43f12013-08-20 11:48:36 +0200295/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000296
Paul Bakker33b43f12013-08-20 11:48:36 +0200297/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100298void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100299 int mod, int radix_N, char * input_N,
300 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200301 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000302{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200303 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000305
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100306 mbedtls_mpi N, E;
307
308 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200309 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200310 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
311 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200312 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000313
Gilles Peskine20edee72021-06-10 23:18:39 +0200314 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
315 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000316
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100317 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
318 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000320
Paul Bakkera6656852010-07-18 19:47:14 +0000321
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200322 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100323 NULL, message_str->len,
324 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200325 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200326 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000327 {
Paul Bakkera6656852010-07-18 19:47:14 +0000328
Ronald Cronac6ae352020-06-26 14:33:03 +0200329 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
330 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000331 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100332
Paul Bakkerbd51b262014-07-10 15:26:12 +0200333exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100334 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000336}
Paul Bakker33b43f12013-08-20 11:48:36 +0200337/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000338
Paul Bakker33b43f12013-08-20 11:48:36 +0200339/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100340void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100341 int mod, int radix_P, char * input_P,
342 int radix_Q, char * input_Q, int radix_N,
343 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200344 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100345 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000346{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200347 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000349 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200350 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100351 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000352
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100353 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
354 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
355
Ronald Cronc1905a12021-06-05 11:11:14 +0200356 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200357 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
358 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000359
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200360 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200361 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000362
Paul Bakker42a29bf2009-07-07 20:18:41 +0000363
Gilles Peskine20edee72021-06-10 23:18:39 +0200364 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
365 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
366 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
367 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000368
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100369 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
370 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100371 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000373
Paul Bakker69998dd2009-07-11 19:15:20 +0000374 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000375
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200376 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100377 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200378 &output_len, message_str->x, output,
379 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200380 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000381 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000382
Ronald Cronac6ae352020-06-26 14:33:03 +0200383 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200384 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200385 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000386 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000387
Paul Bakkerbd51b262014-07-10 15:26:12 +0200388exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100389 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
390 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000392}
Paul Bakker33b43f12013-08-20 11:48:36 +0200393/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000394
Paul Bakker33b43f12013-08-20 11:48:36 +0200395/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100396void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100397 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200398 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000399{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200400 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000402
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100403 mbedtls_mpi N, E;
404
405 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200406 mbedtls_rsa_init( &ctx );
407 mbedtls_rsa_init( &ctx2 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200408 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000409
Gilles Peskine20edee72021-06-10 23:18:39 +0200410 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
411 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000412
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100413 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
414 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000416
Paul Bakker821fb082009-07-12 13:26:42 +0000417
Azim Khand30ca132017-06-09 04:32:58 +0100418 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200419 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000420 {
Paul Bakker821fb082009-07-12 13:26:42 +0000421
Ronald Cronac6ae352020-06-26 14:33:03 +0200422 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
423 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000424 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100425
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100426 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200428 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100432
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200433 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100434 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100435 if( result == 0 )
436 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100437
Ronald Cronac6ae352020-06-26 14:33:03 +0200438 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
439 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100440 }
441
Paul Bakkerbd51b262014-07-10 15:26:12 +0200442exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100443 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_rsa_free( &ctx );
445 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000446}
Paul Bakker33b43f12013-08-20 11:48:36 +0200447/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000448
Paul Bakker33b43f12013-08-20 11:48:36 +0200449/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100450void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100451 char * input_P, int radix_Q, char * input_Q,
452 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200453 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100454 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000455{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200456 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100458 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200459 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200460 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000461
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100462 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
463 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200464 mbedtls_rsa_init( &ctx );
465 mbedtls_rsa_init( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000466
Ronald Cron351f0ee2020-06-10 12:12:18 +0200467 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000468
Gilles Peskine20edee72021-06-10 23:18:39 +0200469 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
470 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
471 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
472 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000473
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100474 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
475 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100476 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000478
Paul Bakker821fb082009-07-12 13:26:42 +0000479
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200480 /* repeat three times to test updating of blinding values */
481 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000482 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200483 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200484 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
485 &rnd_info, message_str->x,
486 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200487 if( result == 0 )
488 {
Paul Bakker821fb082009-07-12 13:26:42 +0000489
Ronald Cronac6ae352020-06-26 14:33:03 +0200490 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200491 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200492 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200493 }
Paul Bakker821fb082009-07-12 13:26:42 +0000494 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000495
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100496 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200498 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100502
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200503 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200504 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
505 &rnd_info, message_str->x,
506 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100507 if( result == 0 )
508 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100509
Ronald Cronac6ae352020-06-26 14:33:03 +0200510 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200511 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200512 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100513 }
514
Paul Bakkerbd51b262014-07-10 15:26:12 +0200515exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100516 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
517 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000520}
Paul Bakker33b43f12013-08-20 11:48:36 +0200521/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000522
Paul Bakker33b43f12013-08-20 11:48:36 +0200523/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100524void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000525{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_rsa_context ctx;
527 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000530}
Paul Bakker33b43f12013-08-20 11:48:36 +0200531/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000532
Paul Bakker33b43f12013-08-20 11:48:36 +0200533/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100534void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
535 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000536{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100538 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000539
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100540 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200541 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000542
Paul Bakker33b43f12013-08-20 11:48:36 +0200543 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000544 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200545 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000546 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200547 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000548 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200549 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000550 }
551
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100552 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100554
Paul Bakkerbd51b262014-07-10 15:26:12 +0200555exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100556 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000558}
Paul Bakker33b43f12013-08-20 11:48:36 +0200559/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000560
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100562void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
563 int radix_Q, char * input_Q, int radix_N,
564 char * input_N, int radix_E, char * input_E,
565 int radix_D, char * input_D, int radix_DP,
566 char * input_DP, int radix_DQ,
567 char * input_DQ, int radix_QP,
568 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000571
Ronald Cronc1905a12021-06-05 11:11:14 +0200572 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000573
Paul Bakker33b43f12013-08-20 11:48:36 +0200574 ctx.len = mod / 8;
575 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000576 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200577 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000578 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200579 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000580 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200581 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000582 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200583 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000584 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200585 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000586 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200587 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000588 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200589 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000590 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200591 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000592 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200593 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000594 }
Hanno Becker131134f2017-08-23 08:31:07 +0100595#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200596 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000597 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200598 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000599 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200600 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000601 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200602 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000603 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200604 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000605 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200606 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000607 }
Hanno Becker131134f2017-08-23 08:31:07 +0100608#else
609 ((void) radix_DP); ((void) input_DP);
610 ((void) radix_DQ); ((void) input_DQ);
611 ((void) radix_QP); ((void) input_QP);
612#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100615
Paul Bakkerbd51b262014-07-10 15:26:12 +0200616exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000618}
Paul Bakker33b43f12013-08-20 11:48:36 +0200619/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000620
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100621/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100622void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
623 int radix_Epub, char * input_Epub, int radix_P,
624 char * input_P, int radix_Q, char * input_Q,
625 int radix_N, char * input_N, int radix_E,
626 char * input_E, int radix_D, char * input_D,
627 int radix_DP, char * input_DP, int radix_DQ,
628 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100629 int result )
630{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100632
Ronald Cronc1905a12021-06-05 11:11:14 +0200633 mbedtls_rsa_init( &pub );
634 mbedtls_rsa_init( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100635
636 pub.len = mod / 8;
637 prv.len = mod / 8;
638
639 if( strlen( input_Npub ) )
640 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200641 TEST_ASSERT( mbedtls_test_read_mpi( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100642 }
643 if( strlen( input_Epub ) )
644 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200645 TEST_ASSERT( mbedtls_test_read_mpi( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100646 }
647
648 if( strlen( input_P ) )
649 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200650 TEST_ASSERT( mbedtls_test_read_mpi( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100651 }
652 if( strlen( input_Q ) )
653 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200654 TEST_ASSERT( mbedtls_test_read_mpi( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100655 }
656 if( strlen( input_N ) )
657 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200658 TEST_ASSERT( mbedtls_test_read_mpi( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100659 }
660 if( strlen( input_E ) )
661 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200662 TEST_ASSERT( mbedtls_test_read_mpi( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100663 }
664 if( strlen( input_D ) )
665 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200666 TEST_ASSERT( mbedtls_test_read_mpi( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100667 }
Hanno Becker131134f2017-08-23 08:31:07 +0100668#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100669 if( strlen( input_DP ) )
670 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200671 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100672 }
673 if( strlen( input_DQ ) )
674 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200675 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100676 }
677 if( strlen( input_QP ) )
678 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200679 TEST_ASSERT( mbedtls_test_read_mpi( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100680 }
Hanno Becker131134f2017-08-23 08:31:07 +0100681#else
682 ((void) radix_DP); ((void) input_DP);
683 ((void) radix_DQ); ((void) input_DQ);
684 ((void) radix_QP); ((void) input_QP);
685#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100688
689exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_rsa_free( &pub );
691 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100692}
693/* END_CASE */
694
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100695/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000697{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 mbedtls_rsa_context ctx;
699 mbedtls_entropy_context entropy;
700 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200701 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000702
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200703 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_entropy_init( &entropy );
Ronald Cronc1905a12021-06-05 11:11:14 +0200705 mbedtls_rsa_init ( &ctx );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000706
Hanno Beckera47023e2017-12-22 17:08:03 +0000707 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
708 &entropy, (const unsigned char *) pers,
709 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &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
Gilles Peskine20edee72021-06-10 23:18:39 +0200740 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
741 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 );
742 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
743 TEST_ASSERT( mbedtls_test_read_mpi( &Qp, radix_P, output_P ) == 0 );
744 TEST_ASSERT( mbedtls_test_read_mpi( &Pp, radix_Q, output_Q ) == 0 );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100745
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
Gilles Peskine20edee72021-06-10 23:18:39 +0200781 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
782 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
783 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
784 TEST_ASSERT( mbedtls_test_read_mpi( &Dp, radix_D, output_D ) == 0 );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100785
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 Beckerf40cdf92017-12-22 11:03:27 +0000826/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +0100827void 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 Becker04877a42017-10-11 10:01:33 +0100834 int res_check,
835 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100836{
837 mbedtls_mpi N, P, Q, D, E;
838 mbedtls_rsa_context ctx;
839
Hanno Beckere1582a82017-09-29 11:51:05 +0100840 /* Buffers used for encryption-decryption test */
841 unsigned char *buf_orig = NULL;
842 unsigned char *buf_enc = NULL;
843 unsigned char *buf_dec = NULL;
844
Hanno Beckerc77ab892017-08-23 11:01:06 +0100845 mbedtls_entropy_context entropy;
846 mbedtls_ctr_drbg_context ctr_drbg;
847 const char *pers = "test_suite_rsa";
848
Hanno Becker4d6e8342017-09-29 11:50:18 +0100849 const int have_N = ( strlen( input_N ) > 0 );
850 const int have_P = ( strlen( input_P ) > 0 );
851 const int have_Q = ( strlen( input_Q ) > 0 );
852 const int have_D = ( strlen( input_D ) > 0 );
853 const int have_E = ( strlen( input_E ) > 0 );
854
Hanno Beckerc77ab892017-08-23 11:01:06 +0100855 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100856 mbedtls_entropy_init( &entropy );
Ronald Cronc1905a12021-06-05 11:11:14 +0200857 mbedtls_rsa_init( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100858
859 mbedtls_mpi_init( &N );
860 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
861 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
862
Hanno Beckerd4d60572018-01-10 07:12:01 +0000863 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
864 (const unsigned char *) pers, strlen( pers ) ) == 0 );
865
Hanno Becker4d6e8342017-09-29 11:50:18 +0100866 if( have_N )
Gilles Peskine20edee72021-06-10 23:18:39 +0200867 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100868
Hanno Becker4d6e8342017-09-29 11:50:18 +0100869 if( have_P )
Gilles Peskine20edee72021-06-10 23:18:39 +0200870 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100871
Hanno Becker4d6e8342017-09-29 11:50:18 +0100872 if( have_Q )
Gilles Peskine20edee72021-06-10 23:18:39 +0200873 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100874
Hanno Becker4d6e8342017-09-29 11:50:18 +0100875 if( have_D )
Gilles Peskine20edee72021-06-10 23:18:39 +0200876 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100877
Hanno Becker4d6e8342017-09-29 11:50:18 +0100878 if( have_E )
Gilles Peskine20edee72021-06-10 23:18:39 +0200879 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100880
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 Becker04877a42017-10-11 10:01:33 +0100919 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
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. */
Hanno Becker04877a42017-10-11 10:01:33 +0100923 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +0100924 {
Hanno Beckere1582a82017-09-29 11:51:05 +0100925 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +0100926 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
927 else
928 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
929
930 if( res_check != 0 )
931 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +0100932
933 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
934 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
935 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
936 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
937 goto exit;
938
939 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
940 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
941
942 /* Make sure the number we're generating is smaller than the modulus */
943 buf_orig[0] = 0x00;
944
945 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
946
947 if( is_priv )
948 {
949 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
950 &ctr_drbg, buf_enc,
951 buf_dec ) == 0 );
952
953 TEST_ASSERT( memcmp( buf_orig, buf_dec,
954 mbedtls_rsa_get_len( &ctx ) ) == 0 );
955 }
956 }
957
Hanno Beckerc77ab892017-08-23 11:01:06 +0100958exit:
959
Hanno Beckere1582a82017-09-29 11:51:05 +0100960 mbedtls_free( buf_orig );
961 mbedtls_free( buf_enc );
962 mbedtls_free( buf_dec );
963
Hanno Beckerc77ab892017-08-23 11:01:06 +0100964 mbedtls_rsa_free( &ctx );
965
966 mbedtls_ctr_drbg_free( &ctr_drbg );
967 mbedtls_entropy_free( &entropy );
968
969 mbedtls_mpi_free( &N );
970 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
971 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
972}
973/* END_CASE */
974
Hanno Becker417f2d62017-08-23 11:44:51 +0100975/* BEGIN_CASE */
976void mbedtls_rsa_export( int radix_N, char *input_N,
977 int radix_P, char *input_P,
978 int radix_Q, char *input_Q,
979 int radix_D, char *input_D,
980 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +0100981 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +0100982 int successive )
983{
984 /* Original MPI's with which we set up the RSA context */
985 mbedtls_mpi N, P, Q, D, E;
986
987 /* Exported MPI's */
988 mbedtls_mpi Ne, Pe, Qe, De, Ee;
989
990 const int have_N = ( strlen( input_N ) > 0 );
991 const int have_P = ( strlen( input_P ) > 0 );
992 const int have_Q = ( strlen( input_Q ) > 0 );
993 const int have_D = ( strlen( input_D ) > 0 );
994 const int have_E = ( strlen( input_E ) > 0 );
995
Hanno Becker417f2d62017-08-23 11:44:51 +0100996 mbedtls_rsa_context ctx;
997
Ronald Cronc1905a12021-06-05 11:11:14 +0200998 mbedtls_rsa_init( &ctx );
Hanno Becker417f2d62017-08-23 11:44:51 +0100999
1000 mbedtls_mpi_init( &N );
1001 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1002 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1003
1004 mbedtls_mpi_init( &Ne );
1005 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1006 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1007
1008 /* Setup RSA context */
1009
1010 if( have_N )
Gilles Peskine20edee72021-06-10 23:18:39 +02001011 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001012
1013 if( have_P )
Gilles Peskine20edee72021-06-10 23:18:39 +02001014 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001015
1016 if( have_Q )
Gilles Peskine20edee72021-06-10 23:18:39 +02001017 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001018
1019 if( have_D )
Gilles Peskine20edee72021-06-10 23:18:39 +02001020 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001021
1022 if( have_E )
Gilles Peskine20edee72021-06-10 23:18:39 +02001023 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001024
1025 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1026 strlen( input_N ) ? &N : NULL,
1027 strlen( input_P ) ? &P : NULL,
1028 strlen( input_Q ) ? &Q : NULL,
1029 strlen( input_D ) ? &D : NULL,
1030 strlen( input_E ) ? &E : NULL ) == 0 );
1031
Hanno Becker7f25f852017-10-10 16:56:22 +01001032 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001033
1034 /*
1035 * Export parameters and compare to original ones.
1036 */
1037
1038 /* N and E must always be present. */
1039 if( !successive )
1040 {
1041 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1042 }
1043 else
1044 {
1045 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1046 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1047 }
1048 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1049 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1050
1051 /* If we were providing enough information to setup a complete private context,
1052 * we expect to be able to export all core parameters. */
1053
1054 if( is_priv )
1055 {
1056 if( !successive )
1057 {
1058 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1059 &De, NULL ) == 0 );
1060 }
1061 else
1062 {
1063 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1064 NULL, NULL ) == 0 );
1065 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1066 NULL, NULL ) == 0 );
1067 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1068 &De, NULL ) == 0 );
1069 }
1070
1071 if( have_P )
1072 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1073
1074 if( have_Q )
1075 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1076
1077 if( have_D )
1078 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1079
1080 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001081 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1082 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001083 }
1084
1085exit:
1086
1087 mbedtls_rsa_free( &ctx );
1088
1089 mbedtls_mpi_free( &N );
1090 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1091 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1092
1093 mbedtls_mpi_free( &Ne );
1094 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1095 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1096}
1097/* END_CASE */
1098
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001099/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001100void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1101 int radix_P, char *input_P,
1102 int radix_Q, char *input_Q,
1103 int radix_D, char *input_D,
1104 int radix_E, char *input_E,
1105 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001106{
1107 /* Original MPI's with which we set up the RSA context */
1108 mbedtls_mpi N, P, Q, D, E;
1109
1110 const int have_N = ( strlen( input_N ) > 0 );
1111 const int have_P = ( strlen( input_P ) > 0 );
1112 const int have_Q = ( strlen( input_Q ) > 0 );
1113 const int have_D = ( strlen( input_D ) > 0 );
1114 const int have_E = ( strlen( input_E ) > 0 );
1115
1116 mbedtls_entropy_context entropy;
1117 mbedtls_ctr_drbg_context ctr_drbg;
1118 const char *pers = "test_suite_rsa";
1119
1120 mbedtls_mpi_init( &N );
1121 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1122 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1123
1124 mbedtls_ctr_drbg_init( &ctr_drbg );
1125 mbedtls_entropy_init( &entropy );
1126 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1127 &entropy, (const unsigned char *) pers,
1128 strlen( pers ) ) == 0 );
1129
1130 if( have_N )
Gilles Peskine20edee72021-06-10 23:18:39 +02001131 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001132
1133 if( have_P )
Gilles Peskine20edee72021-06-10 23:18:39 +02001134 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001135
1136 if( have_Q )
Gilles Peskine20edee72021-06-10 23:18:39 +02001137 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001138
1139 if( have_D )
Gilles Peskine20edee72021-06-10 23:18:39 +02001140 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001141
1142 if( have_E )
Gilles Peskine20edee72021-06-10 23:18:39 +02001143 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001144
Hanno Becker750e8b42017-08-25 07:54:27 +01001145 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1146 have_P ? &P : NULL,
1147 have_Q ? &Q : NULL,
1148 have_D ? &D : NULL,
1149 have_E ? &E : NULL,
1150 prng ? mbedtls_ctr_drbg_random : NULL,
1151 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001152exit:
1153
1154 mbedtls_ctr_drbg_free( &ctr_drbg );
1155 mbedtls_entropy_free( &entropy );
1156
1157 mbedtls_mpi_free( &N );
1158 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1159 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1160}
1161/* END_CASE */
1162
Hanno Beckerc77ab892017-08-23 11:01:06 +01001163/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001164void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1165 data_t *input_Q, data_t *input_D,
1166 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001167 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001168{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001169 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001170 unsigned char bufNe[256];
1171 unsigned char bufPe[128];
1172 unsigned char bufQe[128];
1173 unsigned char bufDe[256];
1174 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001175
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001176 mbedtls_rsa_context ctx;
1177
Ronald Cronc1905a12021-06-05 11:11:14 +02001178 mbedtls_rsa_init( &ctx );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001179
1180 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001181 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001182 input_N->len ? input_N->x : NULL, input_N->len,
1183 input_P->len ? input_P->x : NULL, input_P->len,
1184 input_Q->len ? input_Q->x : NULL, input_Q->len,
1185 input_D->len ? input_D->x : NULL, input_D->len,
1186 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001187
Hanno Becker7f25f852017-10-10 16:56:22 +01001188 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001189
1190 /*
1191 * Export parameters and compare to original ones.
1192 */
1193
1194 /* N and E must always be present. */
1195 if( !successive )
1196 {
Azim Khand30ca132017-06-09 04:32:58 +01001197 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001198 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001199 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001200 }
1201 else
1202 {
Azim Khand30ca132017-06-09 04:32:58 +01001203 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001204 NULL, 0, NULL, 0, NULL, 0,
1205 NULL, 0 ) == 0 );
1206 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1207 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001208 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001209 }
Azim Khand30ca132017-06-09 04:32:58 +01001210 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1211 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001212
1213 /* If we were providing enough information to setup a complete private context,
1214 * we expect to be able to export all core parameters. */
1215
1216 if( is_priv )
1217 {
1218 if( !successive )
1219 {
1220 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001221 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1222 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1223 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001224 NULL, 0 ) == 0 );
1225 }
1226 else
1227 {
1228 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001229 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001230 NULL, 0, NULL, 0,
1231 NULL, 0 ) == 0 );
1232
1233 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001234 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001235 NULL, 0, NULL, 0 ) == 0 );
1236
Azim Khand30ca132017-06-09 04:32:58 +01001237 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1238 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001239 NULL, 0 ) == 0 );
1240 }
1241
Azim Khand30ca132017-06-09 04:32:58 +01001242 if( input_P->len )
1243 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001244
Azim Khand30ca132017-06-09 04:32:58 +01001245 if( input_Q->len )
1246 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001247
Azim Khand30ca132017-06-09 04:32:58 +01001248 if( input_D->len )
1249 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001250
1251 }
1252
1253exit:
1254 mbedtls_rsa_free( &ctx );
1255}
1256/* END_CASE */
1257
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001258/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001259void mbedtls_rsa_import_raw( data_t *input_N,
1260 data_t *input_P, data_t *input_Q,
1261 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001262 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001263 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001264 int res_check,
1265 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001266{
Hanno Beckere1582a82017-09-29 11:51:05 +01001267 /* Buffers used for encryption-decryption test */
1268 unsigned char *buf_orig = NULL;
1269 unsigned char *buf_enc = NULL;
1270 unsigned char *buf_dec = NULL;
1271
Hanno Beckerc77ab892017-08-23 11:01:06 +01001272 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001273 mbedtls_entropy_context entropy;
1274 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001275
Hanno Beckerc77ab892017-08-23 11:01:06 +01001276 const char *pers = "test_suite_rsa";
1277
1278 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001279 mbedtls_entropy_init( &entropy );
Ronald Cronc1905a12021-06-05 11:11:14 +02001280 mbedtls_rsa_init( &ctx );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001281
Hanno Beckerc77ab892017-08-23 11:01:06 +01001282 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1283 &entropy, (const unsigned char *) pers,
1284 strlen( pers ) ) == 0 );
1285
Hanno Beckerc77ab892017-08-23 11:01:06 +01001286 if( !successive )
1287 {
1288 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001289 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1290 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1291 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1292 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1293 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001294 }
1295 else
1296 {
1297 /* Import N, P, Q, D, E separately.
1298 * This should make no functional difference. */
1299
1300 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001301 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001302 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1303
1304 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1305 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001306 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001307 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1308
1309 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1310 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001311 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001312 NULL, 0, NULL, 0 ) == 0 );
1313
1314 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1315 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001316 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001317 NULL, 0 ) == 0 );
1318
1319 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1320 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001321 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001322 }
1323
Hanno Becker04877a42017-10-11 10:01:33 +01001324 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001325
Hanno Beckere1582a82017-09-29 11:51:05 +01001326 /* On expected success, perform some public and private
1327 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001328 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001329 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001330 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001331 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1332 else
1333 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1334
1335 if( res_check != 0 )
1336 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001337
1338 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1339 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1340 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1341 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1342 goto exit;
1343
1344 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1345 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1346
1347 /* Make sure the number we're generating is smaller than the modulus */
1348 buf_orig[0] = 0x00;
1349
1350 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1351
1352 if( is_priv )
1353 {
1354 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1355 &ctr_drbg, buf_enc,
1356 buf_dec ) == 0 );
1357
1358 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1359 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1360 }
1361 }
1362
Hanno Beckerc77ab892017-08-23 11:01:06 +01001363exit:
1364
Hanno Becker3f3ae852017-10-02 10:08:39 +01001365 mbedtls_free( buf_orig );
1366 mbedtls_free( buf_enc );
1367 mbedtls_free( buf_dec );
1368
Hanno Beckerc77ab892017-08-23 11:01:06 +01001369 mbedtls_rsa_free( &ctx );
1370
1371 mbedtls_ctr_drbg_free( &ctr_drbg );
1372 mbedtls_entropy_free( &entropy );
1373
1374}
1375/* END_CASE */
1376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001378void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001379{
Andres AG93012e82016-09-09 09:10:28 +01001380 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001381}
Paul Bakker33b43f12013-08-20 11:48:36 +02001382/* END_CASE */