blob: aed05a4204be24777dd9bd836a8f5948f38cc7dd [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/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 */
Ronald Cronea7631b2021-06-03 18:51:59 +020021void rsa_invalid_param( )
22{
23 mbedtls_rsa_context ctx;
24 const int invalid_padding = 42;
25 const int invalid_hash_id = 0xff;
26
Ronald Cronc1905a12021-06-05 11:11:14 +020027 mbedtls_rsa_init( &ctx );
Ronald Cronea7631b2021-06-03 18:51:59 +020028
29 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
30 invalid_padding,
31 MBEDTLS_MD_NONE ),
32 MBEDTLS_ERR_RSA_INVALID_PADDING );
33
34 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
35 MBEDTLS_RSA_PKCS_V21,
36 invalid_hash_id ),
37 MBEDTLS_ERR_RSA_INVALID_PADDING );
38
39exit:
40 mbedtls_rsa_free( &ctx );
41}
42/* END_CASE */
43
44/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +010045void rsa_init_free( int reinit )
46{
47 mbedtls_rsa_context ctx;
48
49 /* Double free is not explicitly documented to work, but we rely on it
50 * even inside the library so that you can call mbedtls_rsa_free()
51 * unconditionally on an error path without checking whether it has
52 * already been called in the success path. */
53
Ronald Cronc1905a12021-06-05 11:11:14 +020054 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +010055 mbedtls_rsa_free( &ctx );
56
57 if( reinit )
Ronald Cronc1905a12021-06-05 11:11:14 +020058 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +010059 mbedtls_rsa_free( &ctx );
60
61 /* This test case always succeeds, functionally speaking. A plausible
62 * bug might trigger an invalid pointer dereference or a memory leak. */
63 goto exit;
64}
65/* END_CASE */
66
67/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010068void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +010069 int digest, int mod, int radix_P, char * input_P,
70 int radix_Q, char * input_Q, int radix_N,
71 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +020072 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000073{
Ron Eldorfdc15bd2018-11-22 15:47:51 +020074 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
75 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010077 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +020078 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +000079
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010080 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
81 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +020082 mbedtls_rsa_init( &ctx );
83 mbedtls_rsa_set_padding( &ctx, padding_mode, MBEDTLS_MD_NONE );
Paul Bakker42a29bf2009-07-07 20:18:41 +000084
Ron Eldorfdc15bd2018-11-22 15:47:51 +020085 memset( hash_result, 0x00, sizeof( hash_result ) );
86 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +020087 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +000088
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010089 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
90 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
91 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
92 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000093
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010094 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
95 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +010096 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000098
Paul Bakker42a29bf2009-07-07 20:18:41 +000099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100101 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 +0000102
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200103 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100104 &rnd_info, digest, 0, hash_result,
105 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200106 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000107 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000108
Ronald Cronac6ae352020-06-26 14:33:03 +0200109 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
110 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000111 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000112
Paul Bakkerbd51b262014-07-10 15:26:12 +0200113exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100114 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
115 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000117}
Paul Bakker33b43f12013-08-20 11:48:36 +0200118/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000119
Paul Bakker33b43f12013-08-20 11:48:36 +0200120/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100121void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100122 int digest, int mod, int radix_N,
123 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100124 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000125{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200126 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000128
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100129 mbedtls_mpi N, E;
130
131 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200132 mbedtls_rsa_init( &ctx );
133 mbedtls_rsa_set_padding( &ctx, padding_mode, MBEDTLS_MD_NONE );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200134 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000135
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100136 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
137 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
138 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
139 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000141
Paul Bakker42a29bf2009-07-07 20:18:41 +0000142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100144 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 +0000145
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100146 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, digest, 0, hash_result, result_str->x ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100147
Paul Bakkerbd51b262014-07-10 15:26:12 +0200148exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100149 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000151}
Paul Bakker33b43f12013-08-20 11:48:36 +0200152/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000153
Paul Bakker821fb082009-07-12 13:26:42 +0000154
Paul Bakker33b43f12013-08-20 11:48:36 +0200155/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100156void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100157 int padding_mode, int mod, int radix_P,
158 char * input_P, int radix_Q, char * input_Q,
159 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200160 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000161{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200162 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100164 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200165 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000166
Ronald Cronc1905a12021-06-05 11:11:14 +0200167 mbedtls_rsa_init( &ctx );
168 mbedtls_rsa_set_padding( &ctx, padding_mode, MBEDTLS_MD_NONE );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100169 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
170 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000171
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200172 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200173 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000174
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100175 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
176 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
177 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
178 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000179
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100180 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
181 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100182 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000184
Paul Bakker821fb082009-07-12 13:26:42 +0000185
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200186 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100187 &rnd_info, MBEDTLS_MD_NONE,
188 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200189 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000190
Paul Bakker821fb082009-07-12 13:26:42 +0000191
Ronald Cronac6ae352020-06-26 14:33:03 +0200192 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
193 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000194
Paul Bakkerbd51b262014-07-10 15:26:12 +0200195exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100196 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
197 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000200}
Paul Bakker33b43f12013-08-20 11:48:36 +0200201/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000202
Paul Bakker33b43f12013-08-20 11:48:36 +0200203/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100204void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200205 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100206 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100207 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000208{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200209 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_rsa_context ctx;
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
Ronald Cronc1905a12021-06-05 11:11:14 +0200215 mbedtls_rsa_init( &ctx );
216 mbedtls_rsa_set_padding( &ctx, padding_mode, MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100217 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000218
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100219 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
220 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000221
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100222 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
223 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000225
Paul Bakker821fb082009-07-12 13:26:42 +0000226
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100227 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 +0100228
Paul Bakkerbd51b262014-07-10 15:26:12 +0200229exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100230 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000232}
Paul Bakker33b43f12013-08-20 11:48:36 +0200233/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000234
Paul Bakker33b43f12013-08-20 11:48:36 +0200235/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100236void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100237 int mod, int radix_N, char * input_N,
238 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200239 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000240{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200241 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200243 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000244
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100245 mbedtls_mpi N, E;
246 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
247
Ronald Cron351f0ee2020-06-10 12:12:18 +0200248 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000249
Ronald Cronc1905a12021-06-05 11:11:14 +0200250 mbedtls_rsa_init( &ctx );
251 mbedtls_rsa_set_padding( &ctx, padding_mode, MBEDTLS_MD_NONE );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200252 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000253
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100254 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
255 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000256
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100257 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
258 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000260
Paul Bakker42a29bf2009-07-07 20:18:41 +0000261
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200262 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
263 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100264 &rnd_info, message_str->len,
265 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200266 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200267 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000268 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000269
Ronald Cronac6ae352020-06-26 14:33:03 +0200270 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
271 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000272 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100273
Paul Bakkerbd51b262014-07-10 15:26:12 +0200274exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100275 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000277}
Paul Bakker33b43f12013-08-20 11:48:36 +0200278/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000279
Paul Bakker33b43f12013-08-20 11:48:36 +0200280/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100281void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100282 int mod, int radix_N, char * input_N,
283 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200284 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000285{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200286 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000288
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100289 mbedtls_mpi N, E;
290
291 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200292 mbedtls_rsa_init( &ctx );
293 mbedtls_rsa_set_padding( &ctx, padding_mode, MBEDTLS_MD_NONE );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200294 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000295
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100296 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
297 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000298
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100299 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
300 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000302
Paul Bakkera6656852010-07-18 19:47:14 +0000303
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200304 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100305 NULL, message_str->len,
306 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200307 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200308 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000309 {
Paul Bakkera6656852010-07-18 19:47:14 +0000310
Ronald Cronac6ae352020-06-26 14:33:03 +0200311 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
312 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000313 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100314
Paul Bakkerbd51b262014-07-10 15:26:12 +0200315exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100316 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000318}
Paul Bakker33b43f12013-08-20 11:48:36 +0200319/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000320
Paul Bakker33b43f12013-08-20 11:48:36 +0200321/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100322void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100323 int mod, int radix_P, char * input_P,
324 int radix_Q, char * input_Q, int radix_N,
325 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200326 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100327 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000328{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200329 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000331 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200332 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100333 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000334
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100335 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
336 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
337
Ronald Cronc1905a12021-06-05 11:11:14 +0200338 mbedtls_rsa_init( &ctx );
339 mbedtls_rsa_set_padding( &ctx, padding_mode, MBEDTLS_MD_NONE );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000340
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200341 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200342 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000343
Paul Bakker42a29bf2009-07-07 20:18:41 +0000344
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100345 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
346 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
347 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
348 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000349
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100350 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
351 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100352 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000354
Paul Bakker69998dd2009-07-11 19:15:20 +0000355 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000356
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200357 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100358 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200359 &output_len, message_str->x, output,
360 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200361 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000362 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000363
Ronald Cronac6ae352020-06-26 14:33:03 +0200364 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200365 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200366 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000367 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000368
Paul Bakkerbd51b262014-07-10 15:26:12 +0200369exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100370 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
371 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000373}
Paul Bakker33b43f12013-08-20 11:48:36 +0200374/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000375
Paul Bakker33b43f12013-08-20 11:48:36 +0200376/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100377void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100378 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200379 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000380{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200381 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000383
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100384 mbedtls_mpi N, E;
385
386 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200387 mbedtls_rsa_init( &ctx );
388 mbedtls_rsa_init( &ctx2 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200389 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000390
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100391 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
392 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000393
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100394 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
395 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000397
Paul Bakker821fb082009-07-12 13:26:42 +0000398
Azim Khand30ca132017-06-09 04:32:58 +0100399 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200400 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000401 {
Paul Bakker821fb082009-07-12 13:26:42 +0000402
Ronald Cronac6ae352020-06-26 14:33:03 +0200403 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
404 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000405 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100406
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100407 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200409 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100413
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200414 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100415 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100416 if( result == 0 )
417 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100418
Ronald Cronac6ae352020-06-26 14:33:03 +0200419 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
420 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100421 }
422
Paul Bakkerbd51b262014-07-10 15:26:12 +0200423exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100424 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_rsa_free( &ctx );
426 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000427}
Paul Bakker33b43f12013-08-20 11:48:36 +0200428/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000429
Paul Bakker33b43f12013-08-20 11:48:36 +0200430/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100431void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100432 char * input_P, int radix_Q, char * input_Q,
433 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200434 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100435 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000436{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200437 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100439 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200440 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200441 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000442
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100443 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
444 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200445 mbedtls_rsa_init( &ctx );
446 mbedtls_rsa_init( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000447
Ronald Cron351f0ee2020-06-10 12:12:18 +0200448 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000449
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100450 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
451 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
452 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
453 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000454
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100455 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
456 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100457 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000459
Paul Bakker821fb082009-07-12 13:26:42 +0000460
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200461 /* repeat three times to test updating of blinding values */
462 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000463 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200464 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200465 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
466 &rnd_info, message_str->x,
467 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200468 if( result == 0 )
469 {
Paul Bakker821fb082009-07-12 13:26:42 +0000470
Ronald Cronac6ae352020-06-26 14:33:03 +0200471 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200472 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200473 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200474 }
Paul Bakker821fb082009-07-12 13:26:42 +0000475 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000476
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100477 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200479 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100483
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200484 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200485 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
486 &rnd_info, message_str->x,
487 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100488 if( result == 0 )
489 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100490
Ronald Cronac6ae352020-06-26 14:33:03 +0200491 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200492 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200493 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100494 }
495
Paul Bakkerbd51b262014-07-10 15:26:12 +0200496exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100497 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
498 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000501}
Paul Bakker33b43f12013-08-20 11:48:36 +0200502/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000503
Paul Bakker33b43f12013-08-20 11:48:36 +0200504/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100505void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_rsa_context ctx;
508 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000511}
Paul Bakker33b43f12013-08-20 11:48:36 +0200512/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000513
Paul Bakker33b43f12013-08-20 11:48:36 +0200514/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100515void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
516 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100519 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000520
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100521 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200522 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000523
Paul Bakker33b43f12013-08-20 11:48:36 +0200524 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000525 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100526 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000527 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200528 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000529 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100530 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000531 }
532
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100533 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100535
Paul Bakkerbd51b262014-07-10 15:26:12 +0200536exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100537 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000539}
Paul Bakker33b43f12013-08-20 11:48:36 +0200540/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000541
Paul Bakker33b43f12013-08-20 11:48:36 +0200542/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100543void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
544 int radix_Q, char * input_Q, int radix_N,
545 char * input_N, int radix_E, char * input_E,
546 int radix_D, char * input_D, int radix_DP,
547 char * input_DP, int radix_DQ,
548 char * input_DQ, int radix_QP,
549 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000552
Ronald Cronc1905a12021-06-05 11:11:14 +0200553 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000554
Paul Bakker33b43f12013-08-20 11:48:36 +0200555 ctx.len = mod / 8;
556 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000559 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200560 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000563 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200564 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000567 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200568 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000571 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200572 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000575 }
Hanno Becker131134f2017-08-23 08:31:07 +0100576#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200577 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000580 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200581 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000584 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200585 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000588 }
Hanno Becker131134f2017-08-23 08:31:07 +0100589#else
590 ((void) radix_DP); ((void) input_DP);
591 ((void) radix_DQ); ((void) input_DQ);
592 ((void) radix_QP); ((void) input_QP);
593#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100596
Paul Bakkerbd51b262014-07-10 15:26:12 +0200597exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000599}
Paul Bakker33b43f12013-08-20 11:48:36 +0200600/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000601
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100602/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100603void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
604 int radix_Epub, char * input_Epub, int radix_P,
605 char * input_P, int radix_Q, char * input_Q,
606 int radix_N, char * input_N, int radix_E,
607 char * input_E, int radix_D, char * input_D,
608 int radix_DP, char * input_DP, int radix_DQ,
609 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100610 int result )
611{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100613
Ronald Cronc1905a12021-06-05 11:11:14 +0200614 mbedtls_rsa_init( &pub );
615 mbedtls_rsa_init( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100616
617 pub.len = mod / 8;
618 prv.len = mod / 8;
619
620 if( strlen( input_Npub ) )
621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100623 }
624 if( strlen( input_Epub ) )
625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100627 }
628
629 if( strlen( input_P ) )
630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100632 }
633 if( strlen( input_Q ) )
634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100636 }
637 if( strlen( input_N ) )
638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100640 }
641 if( strlen( input_E ) )
642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100644 }
645 if( strlen( input_D ) )
646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100648 }
Hanno Becker131134f2017-08-23 08:31:07 +0100649#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100650 if( strlen( input_DP ) )
651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100653 }
654 if( strlen( input_DQ ) )
655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100657 }
658 if( strlen( input_QP ) )
659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100661 }
Hanno Becker131134f2017-08-23 08:31:07 +0100662#else
663 ((void) radix_DP); ((void) input_DP);
664 ((void) radix_DQ); ((void) input_DQ);
665 ((void) radix_QP); ((void) input_QP);
666#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100669
670exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_rsa_free( &pub );
672 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100673}
674/* END_CASE */
675
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100676/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_rsa_context ctx;
680 mbedtls_entropy_context entropy;
681 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200682 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000683
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200684 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_entropy_init( &entropy );
Ronald Cronc1905a12021-06-05 11:11:14 +0200686 mbedtls_rsa_init ( &ctx );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000687
Hanno Beckera47023e2017-12-22 17:08:03 +0000688 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
689 &entropy, (const unsigned char *) pers,
690 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200693 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100696 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000697 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100698
Paul Bakkerbd51b262014-07-10 15:26:12 +0200699exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_rsa_free( &ctx );
701 mbedtls_ctr_drbg_free( &ctr_drbg );
702 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000703}
Paul Bakker33b43f12013-08-20 11:48:36 +0200704/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000705
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100706/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +0100707void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100708 int radix_D, char *input_D,
709 int radix_E, char *input_E,
710 int radix_P, char *output_P,
711 int radix_Q, char *output_Q,
712 int corrupt, int result )
713{
714 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
715
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100716 mbedtls_mpi_init( &N );
717 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
718 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
719 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
720
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100721 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
722 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
723 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
724 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
725 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
726
727 if( corrupt )
728 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
729
730 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100731 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100732
733 if( !corrupt )
734 {
735 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
736 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
737 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
738 }
739
740exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100741 mbedtls_mpi_free( &N );
742 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
743 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
744 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100745}
746/* END_CASE */
747
Hanno Becker6b4ce492017-08-23 11:00:21 +0100748/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100749void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
750 int radix_Q, char *input_Q,
751 int radix_E, char *input_E,
752 int radix_D, char *output_D,
753 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +0100754{
755 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
756
757 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
758 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
759 mbedtls_mpi_init( &E );
760 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
761
762 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
763 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
764 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
765 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
766
767 if( corrupt )
768 {
769 /* Make E even */
770 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
771 }
772
773 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100774 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
775 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100776
777 if( !corrupt )
778 {
779 /*
780 * Check that D and Dp agree modulo LCM(P-1, Q-1).
781 */
782
783 /* Replace P,Q by P-1, Q-1 */
784 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
785 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
786
787 /* Check D == Dp modulo P-1 */
788 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
789 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
790 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
791
792 /* Check D == Dp modulo Q-1 */
793 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
794 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
795 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
796 }
797
798exit:
799
800 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
801 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
802 mbedtls_mpi_free( &E );
803 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
804}
805/* END_CASE */
806
Hanno Beckerf40cdf92017-12-22 11:03:27 +0000807/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +0100808void mbedtls_rsa_import( int radix_N, char *input_N,
809 int radix_P, char *input_P,
810 int radix_Q, char *input_Q,
811 int radix_D, char *input_D,
812 int radix_E, char *input_E,
813 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +0100814 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +0100815 int res_check,
816 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100817{
818 mbedtls_mpi N, P, Q, D, E;
819 mbedtls_rsa_context ctx;
820
Hanno Beckere1582a82017-09-29 11:51:05 +0100821 /* Buffers used for encryption-decryption test */
822 unsigned char *buf_orig = NULL;
823 unsigned char *buf_enc = NULL;
824 unsigned char *buf_dec = NULL;
825
Hanno Beckerc77ab892017-08-23 11:01:06 +0100826 mbedtls_entropy_context entropy;
827 mbedtls_ctr_drbg_context ctr_drbg;
828 const char *pers = "test_suite_rsa";
829
Hanno Becker4d6e8342017-09-29 11:50:18 +0100830 const int have_N = ( strlen( input_N ) > 0 );
831 const int have_P = ( strlen( input_P ) > 0 );
832 const int have_Q = ( strlen( input_Q ) > 0 );
833 const int have_D = ( strlen( input_D ) > 0 );
834 const int have_E = ( strlen( input_E ) > 0 );
835
Hanno Beckerc77ab892017-08-23 11:01:06 +0100836 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100837 mbedtls_entropy_init( &entropy );
Ronald Cronc1905a12021-06-05 11:11:14 +0200838 mbedtls_rsa_init( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100839
840 mbedtls_mpi_init( &N );
841 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
842 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
843
Hanno Beckerd4d60572018-01-10 07:12:01 +0000844 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
845 (const unsigned char *) pers, strlen( pers ) ) == 0 );
846
Hanno Becker4d6e8342017-09-29 11:50:18 +0100847 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100848 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
849
Hanno Becker4d6e8342017-09-29 11:50:18 +0100850 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100851 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
852
Hanno Becker4d6e8342017-09-29 11:50:18 +0100853 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100854 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
855
Hanno Becker4d6e8342017-09-29 11:50:18 +0100856 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100857 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
858
Hanno Becker4d6e8342017-09-29 11:50:18 +0100859 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100860 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
861
862 if( !successive )
863 {
864 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100865 have_N ? &N : NULL,
866 have_P ? &P : NULL,
867 have_Q ? &Q : NULL,
868 have_D ? &D : NULL,
869 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100870 }
871 else
872 {
873 /* Import N, P, Q, D, E separately.
874 * This should make no functional difference. */
875
876 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100877 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100878 NULL, NULL, NULL, NULL ) == 0 );
879
880 TEST_ASSERT( mbedtls_rsa_import( &ctx,
881 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100882 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100883 NULL, NULL, NULL ) == 0 );
884
885 TEST_ASSERT( mbedtls_rsa_import( &ctx,
886 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100887 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100888 NULL, NULL ) == 0 );
889
890 TEST_ASSERT( mbedtls_rsa_import( &ctx,
891 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100892 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100893 NULL ) == 0 );
894
895 TEST_ASSERT( mbedtls_rsa_import( &ctx,
896 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100897 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100898 }
899
Hanno Becker04877a42017-10-11 10:01:33 +0100900 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100901
Hanno Beckere1582a82017-09-29 11:51:05 +0100902 /* On expected success, perform some public and private
903 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +0100904 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +0100905 {
Hanno Beckere1582a82017-09-29 11:51:05 +0100906 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +0100907 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
908 else
909 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
910
911 if( res_check != 0 )
912 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +0100913
914 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
915 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
916 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
917 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
918 goto exit;
919
920 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
921 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
922
923 /* Make sure the number we're generating is smaller than the modulus */
924 buf_orig[0] = 0x00;
925
926 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
927
928 if( is_priv )
929 {
930 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
931 &ctr_drbg, buf_enc,
932 buf_dec ) == 0 );
933
934 TEST_ASSERT( memcmp( buf_orig, buf_dec,
935 mbedtls_rsa_get_len( &ctx ) ) == 0 );
936 }
937 }
938
Hanno Beckerc77ab892017-08-23 11:01:06 +0100939exit:
940
Hanno Beckere1582a82017-09-29 11:51:05 +0100941 mbedtls_free( buf_orig );
942 mbedtls_free( buf_enc );
943 mbedtls_free( buf_dec );
944
Hanno Beckerc77ab892017-08-23 11:01:06 +0100945 mbedtls_rsa_free( &ctx );
946
947 mbedtls_ctr_drbg_free( &ctr_drbg );
948 mbedtls_entropy_free( &entropy );
949
950 mbedtls_mpi_free( &N );
951 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
952 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
953}
954/* END_CASE */
955
Hanno Becker417f2d62017-08-23 11:44:51 +0100956/* BEGIN_CASE */
957void mbedtls_rsa_export( int radix_N, char *input_N,
958 int radix_P, char *input_P,
959 int radix_Q, char *input_Q,
960 int radix_D, char *input_D,
961 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +0100962 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +0100963 int successive )
964{
965 /* Original MPI's with which we set up the RSA context */
966 mbedtls_mpi N, P, Q, D, E;
967
968 /* Exported MPI's */
969 mbedtls_mpi Ne, Pe, Qe, De, Ee;
970
971 const int have_N = ( strlen( input_N ) > 0 );
972 const int have_P = ( strlen( input_P ) > 0 );
973 const int have_Q = ( strlen( input_Q ) > 0 );
974 const int have_D = ( strlen( input_D ) > 0 );
975 const int have_E = ( strlen( input_E ) > 0 );
976
Hanno Becker417f2d62017-08-23 11:44:51 +0100977 mbedtls_rsa_context ctx;
978
Ronald Cronc1905a12021-06-05 11:11:14 +0200979 mbedtls_rsa_init( &ctx );
Hanno Becker417f2d62017-08-23 11:44:51 +0100980
981 mbedtls_mpi_init( &N );
982 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
983 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
984
985 mbedtls_mpi_init( &Ne );
986 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
987 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
988
989 /* Setup RSA context */
990
991 if( have_N )
992 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
993
994 if( have_P )
995 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
996
997 if( have_Q )
998 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
999
1000 if( have_D )
1001 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1002
1003 if( have_E )
1004 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1005
1006 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1007 strlen( input_N ) ? &N : NULL,
1008 strlen( input_P ) ? &P : NULL,
1009 strlen( input_Q ) ? &Q : NULL,
1010 strlen( input_D ) ? &D : NULL,
1011 strlen( input_E ) ? &E : NULL ) == 0 );
1012
Hanno Becker7f25f852017-10-10 16:56:22 +01001013 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001014
1015 /*
1016 * Export parameters and compare to original ones.
1017 */
1018
1019 /* N and E must always be present. */
1020 if( !successive )
1021 {
1022 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1023 }
1024 else
1025 {
1026 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1027 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1028 }
1029 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1030 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1031
1032 /* If we were providing enough information to setup a complete private context,
1033 * we expect to be able to export all core parameters. */
1034
1035 if( is_priv )
1036 {
1037 if( !successive )
1038 {
1039 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1040 &De, NULL ) == 0 );
1041 }
1042 else
1043 {
1044 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1045 NULL, NULL ) == 0 );
1046 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1047 NULL, NULL ) == 0 );
1048 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1049 &De, NULL ) == 0 );
1050 }
1051
1052 if( have_P )
1053 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1054
1055 if( have_Q )
1056 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1057
1058 if( have_D )
1059 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1060
1061 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001062 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1063 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001064 }
1065
1066exit:
1067
1068 mbedtls_rsa_free( &ctx );
1069
1070 mbedtls_mpi_free( &N );
1071 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1072 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1073
1074 mbedtls_mpi_free( &Ne );
1075 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1076 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1077}
1078/* END_CASE */
1079
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001080/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001081void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1082 int radix_P, char *input_P,
1083 int radix_Q, char *input_Q,
1084 int radix_D, char *input_D,
1085 int radix_E, char *input_E,
1086 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001087{
1088 /* Original MPI's with which we set up the RSA context */
1089 mbedtls_mpi N, P, Q, D, E;
1090
1091 const int have_N = ( strlen( input_N ) > 0 );
1092 const int have_P = ( strlen( input_P ) > 0 );
1093 const int have_Q = ( strlen( input_Q ) > 0 );
1094 const int have_D = ( strlen( input_D ) > 0 );
1095 const int have_E = ( strlen( input_E ) > 0 );
1096
1097 mbedtls_entropy_context entropy;
1098 mbedtls_ctr_drbg_context ctr_drbg;
1099 const char *pers = "test_suite_rsa";
1100
1101 mbedtls_mpi_init( &N );
1102 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1103 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1104
1105 mbedtls_ctr_drbg_init( &ctr_drbg );
1106 mbedtls_entropy_init( &entropy );
1107 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1108 &entropy, (const unsigned char *) pers,
1109 strlen( pers ) ) == 0 );
1110
1111 if( have_N )
1112 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1113
1114 if( have_P )
1115 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1116
1117 if( have_Q )
1118 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1119
1120 if( have_D )
1121 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1122
1123 if( have_E )
1124 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1125
Hanno Becker750e8b42017-08-25 07:54:27 +01001126 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1127 have_P ? &P : NULL,
1128 have_Q ? &Q : NULL,
1129 have_D ? &D : NULL,
1130 have_E ? &E : NULL,
1131 prng ? mbedtls_ctr_drbg_random : NULL,
1132 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001133exit:
1134
1135 mbedtls_ctr_drbg_free( &ctr_drbg );
1136 mbedtls_entropy_free( &entropy );
1137
1138 mbedtls_mpi_free( &N );
1139 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1140 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1141}
1142/* END_CASE */
1143
Hanno Beckerc77ab892017-08-23 11:01:06 +01001144/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001145void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1146 data_t *input_Q, data_t *input_D,
1147 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001148 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001149{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001150 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001151 unsigned char bufNe[256];
1152 unsigned char bufPe[128];
1153 unsigned char bufQe[128];
1154 unsigned char bufDe[256];
1155 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001156
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001157 mbedtls_rsa_context ctx;
1158
Ronald Cronc1905a12021-06-05 11:11:14 +02001159 mbedtls_rsa_init( &ctx );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001160
1161 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001162 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001163 input_N->len ? input_N->x : NULL, input_N->len,
1164 input_P->len ? input_P->x : NULL, input_P->len,
1165 input_Q->len ? input_Q->x : NULL, input_Q->len,
1166 input_D->len ? input_D->x : NULL, input_D->len,
1167 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001168
Hanno Becker7f25f852017-10-10 16:56:22 +01001169 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001170
1171 /*
1172 * Export parameters and compare to original ones.
1173 */
1174
1175 /* N and E must always be present. */
1176 if( !successive )
1177 {
Azim Khand30ca132017-06-09 04:32:58 +01001178 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001179 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001180 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001181 }
1182 else
1183 {
Azim Khand30ca132017-06-09 04:32:58 +01001184 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001185 NULL, 0, NULL, 0, NULL, 0,
1186 NULL, 0 ) == 0 );
1187 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1188 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001189 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001190 }
Azim Khand30ca132017-06-09 04:32:58 +01001191 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1192 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001193
1194 /* If we were providing enough information to setup a complete private context,
1195 * we expect to be able to export all core parameters. */
1196
1197 if( is_priv )
1198 {
1199 if( !successive )
1200 {
1201 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001202 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1203 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1204 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001205 NULL, 0 ) == 0 );
1206 }
1207 else
1208 {
1209 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001210 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001211 NULL, 0, NULL, 0,
1212 NULL, 0 ) == 0 );
1213
1214 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001215 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001216 NULL, 0, NULL, 0 ) == 0 );
1217
Azim Khand30ca132017-06-09 04:32:58 +01001218 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1219 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001220 NULL, 0 ) == 0 );
1221 }
1222
Azim Khand30ca132017-06-09 04:32:58 +01001223 if( input_P->len )
1224 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001225
Azim Khand30ca132017-06-09 04:32:58 +01001226 if( input_Q->len )
1227 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001228
Azim Khand30ca132017-06-09 04:32:58 +01001229 if( input_D->len )
1230 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001231
1232 }
1233
1234exit:
1235 mbedtls_rsa_free( &ctx );
1236}
1237/* END_CASE */
1238
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001239/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001240void mbedtls_rsa_import_raw( data_t *input_N,
1241 data_t *input_P, data_t *input_Q,
1242 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001243 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001244 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001245 int res_check,
1246 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001247{
Hanno Beckere1582a82017-09-29 11:51:05 +01001248 /* Buffers used for encryption-decryption test */
1249 unsigned char *buf_orig = NULL;
1250 unsigned char *buf_enc = NULL;
1251 unsigned char *buf_dec = NULL;
1252
Hanno Beckerc77ab892017-08-23 11:01:06 +01001253 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001254 mbedtls_entropy_context entropy;
1255 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001256
Hanno Beckerc77ab892017-08-23 11:01:06 +01001257 const char *pers = "test_suite_rsa";
1258
1259 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001260 mbedtls_entropy_init( &entropy );
Ronald Cronc1905a12021-06-05 11:11:14 +02001261 mbedtls_rsa_init( &ctx );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001262
Hanno Beckerc77ab892017-08-23 11:01:06 +01001263 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1264 &entropy, (const unsigned char *) pers,
1265 strlen( pers ) ) == 0 );
1266
Hanno Beckerc77ab892017-08-23 11:01:06 +01001267 if( !successive )
1268 {
1269 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001270 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1271 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1272 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1273 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1274 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001275 }
1276 else
1277 {
1278 /* Import N, P, Q, D, E separately.
1279 * This should make no functional difference. */
1280
1281 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001282 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001283 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1284
1285 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1286 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001287 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001288 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1289
1290 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1291 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001292 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001293 NULL, 0, NULL, 0 ) == 0 );
1294
1295 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1296 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001297 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001298 NULL, 0 ) == 0 );
1299
1300 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1301 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001302 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001303 }
1304
Hanno Becker04877a42017-10-11 10:01:33 +01001305 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001306
Hanno Beckere1582a82017-09-29 11:51:05 +01001307 /* On expected success, perform some public and private
1308 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001309 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001310 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001311 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001312 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1313 else
1314 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1315
1316 if( res_check != 0 )
1317 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001318
1319 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1320 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1321 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1322 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1323 goto exit;
1324
1325 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1326 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1327
1328 /* Make sure the number we're generating is smaller than the modulus */
1329 buf_orig[0] = 0x00;
1330
1331 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1332
1333 if( is_priv )
1334 {
1335 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1336 &ctr_drbg, buf_enc,
1337 buf_dec ) == 0 );
1338
1339 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1340 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1341 }
1342 }
1343
Hanno Beckerc77ab892017-08-23 11:01:06 +01001344exit:
1345
Hanno Becker3f3ae852017-10-02 10:08:39 +01001346 mbedtls_free( buf_orig );
1347 mbedtls_free( buf_enc );
1348 mbedtls_free( buf_dec );
1349
Hanno Beckerc77ab892017-08-23 11:01:06 +01001350 mbedtls_rsa_free( &ctx );
1351
1352 mbedtls_ctr_drbg_free( &ctr_drbg );
1353 mbedtls_entropy_free( &entropy );
1354
1355}
1356/* END_CASE */
1357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001359void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001360{
Andres AG93012e82016-09-09 09:10:28 +01001361 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001362}
Paul Bakker33b43f12013-08-20 11:48:36 +02001363/* END_CASE */