blob: 4be23589c781ef46e5a1650cbdb85d271466a1cc [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"
Hanno Becker47deec42017-07-24 12:27:09 +01004
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +02005#include "legacy_or_psa.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02006/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +00007
Paul Bakker33b43f12013-08-20 11:48:36 +02008/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +020010 * END_DEPENDENCIES
11 */
Paul Bakker5690efc2011-05-26 13:16:06 +000012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* BEGIN_CASE */
Ronald Cronea7631b2021-06-03 18:51:59 +020014void rsa_invalid_param( )
15{
16 mbedtls_rsa_context ctx;
17 const int invalid_padding = 42;
18 const int invalid_hash_id = 0xff;
19
Ronald Cronc1905a12021-06-05 11:11:14 +020020 mbedtls_rsa_init( &ctx );
Ronald Cronea7631b2021-06-03 18:51:59 +020021
22 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
23 invalid_padding,
24 MBEDTLS_MD_NONE ),
25 MBEDTLS_ERR_RSA_INVALID_PADDING );
26
27 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
28 MBEDTLS_RSA_PKCS_V21,
29 invalid_hash_id ),
30 MBEDTLS_ERR_RSA_INVALID_PADDING );
31
Ronald Cron3a0375f2021-06-08 10:22:28 +020032#if !defined(MBEDTLS_PKCS1_V15)
33 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
34 MBEDTLS_RSA_PKCS_V15,
35 MBEDTLS_MD_NONE ),
36 MBEDTLS_ERR_RSA_INVALID_PADDING );
37#endif
38
39#if !defined(MBEDTLS_PKCS1_V21)
40 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
41 MBEDTLS_RSA_PKCS_V21,
42 MBEDTLS_MD_NONE ),
43 MBEDTLS_ERR_RSA_INVALID_PADDING );
44#endif
45
Ronald Cronea7631b2021-06-03 18:51:59 +020046exit:
47 mbedtls_rsa_free( &ctx );
48}
49/* END_CASE */
50
51/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +010052void rsa_init_free( int reinit )
53{
54 mbedtls_rsa_context ctx;
55
56 /* Double free is not explicitly documented to work, but we rely on it
57 * even inside the library so that you can call mbedtls_rsa_free()
58 * unconditionally on an error path without checking whether it has
59 * already been called in the success path. */
60
Ronald Cronc1905a12021-06-05 11:11:14 +020061 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +010062 mbedtls_rsa_free( &ctx );
63
64 if( reinit )
Ronald Cronc1905a12021-06-05 11:11:14 +020065 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +010066 mbedtls_rsa_free( &ctx );
67
68 /* This test case always succeeds, functionally speaking. A plausible
69 * bug might trigger an invalid pointer dereference or a memory leak. */
70 goto exit;
71}
72/* END_CASE */
73
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +020074/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010075void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Werner Lewis9802d362022-07-07 11:37:24 +010076 int digest, int mod, char * input_P,
Werner Lewisefda01f2022-07-06 13:03:36 +010077 char * input_Q, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +020078 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000079{
Ron Eldorfdc15bd2018-11-22 15:47:51 +020080 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010082 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +020083 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +000084
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010085 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
86 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +020087 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +020088 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,padding_mode,
89 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000090
Ron Eldorfdc15bd2018-11-22 15:47:51 +020091 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +020092 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +000093
Werner Lewis19b4cd82022-07-07 11:02:27 +010094 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
95 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
96 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
97 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000098
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010099 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
100 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100101 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000103
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200104 TEST_ASSERT( mbedtls_rsa_pkcs1_sign(
105 &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnarda4aa12f2022-07-16 08:20:26 +0200106 digest, message_str->len, message_str->x,
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200107 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200108 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000109 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000110
Ronald Cronac6ae352020-06-26 14:33:03 +0200111 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
112 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000113 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000114
Paul Bakkerbd51b262014-07-10 15:26:12 +0200115exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100116 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
117 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000119}
Paul Bakker33b43f12013-08-20 11:48:36 +0200120/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000121
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200122/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100123void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Werner Lewis9802d362022-07-07 11:37:24 +0100124 int digest, int mod,
125 char * input_N, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100126 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_rsa_context ctx;
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 );
Ronald Cron266b6d22021-06-08 10:03:49 +0200133 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
134 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000135
Werner Lewis19b4cd82022-07-07 11:02:27 +0100136 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
137 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100138 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
Manuel Pégourié-Gonnarda4aa12f2022-07-16 08:20:26 +0200142 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, digest, message_str->len, message_str->x, result_str->x ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100143
Paul Bakkerbd51b262014-07-10 15:26:12 +0200144exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100145 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000147}
Paul Bakker33b43f12013-08-20 11:48:36 +0200148/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000149
Paul Bakker821fb082009-07-12 13:26:42 +0000150
Paul Bakker33b43f12013-08-20 11:48:36 +0200151/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100152void rsa_pkcs1_sign_raw( data_t * hash_result,
Werner Lewis9802d362022-07-07 11:37:24 +0100153 int padding_mode, int mod,
154 char * input_P, char * input_Q,
Werner Lewisefda01f2022-07-06 13:03:36 +0100155 char * input_N, char * input_E,
156 data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000157{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200158 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100160 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200161 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000162
Ronald Cronc1905a12021-06-05 11:11:14 +0200163 mbedtls_rsa_init( &ctx );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100164 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
165 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000166
Paul Elliotte57dd2d2021-06-25 11:13:24 +0100167 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
168 MBEDTLS_MD_NONE ) == 0 );
169
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200170 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200171 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000172
Werner Lewis19b4cd82022-07-07 11:02:27 +0100173 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
174 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
175 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
176 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000177
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100178 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
179 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100180 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000182
Paul Bakker821fb082009-07-12 13:26:42 +0000183
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200184 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100185 &rnd_info, MBEDTLS_MD_NONE,
186 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200187 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000188
Paul Bakker821fb082009-07-12 13:26:42 +0000189
Ronald Cronac6ae352020-06-26 14:33:03 +0200190 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
191 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000192
Paul Bakkerbd51b262014-07-10 15:26:12 +0200193exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100194 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
195 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000198}
Paul Bakker33b43f12013-08-20 11:48:36 +0200199/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000200
Paul Bakker33b43f12013-08-20 11:48:36 +0200201/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100202void rsa_pkcs1_verify_raw( data_t * hash_result,
Werner Lewis9802d362022-07-07 11:37:24 +0100203 int padding_mode, int mod,
204 char * input_N, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100205 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000206{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200207 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000209
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100210 mbedtls_mpi N, E;
211 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
212
Ronald Cronc1905a12021-06-05 11:11:14 +0200213 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200214 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
215 MBEDTLS_MD_NONE ) == 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100216 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000217
Werner Lewis19b4cd82022-07-07 11:02:27 +0100218 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
219 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000220
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100221 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
222 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000224
Paul Bakker821fb082009-07-12 13:26:42 +0000225
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100226 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 +0100227
Paul Bakkerbd51b262014-07-10 15:26:12 +0200228exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100229 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000231}
Paul Bakker33b43f12013-08-20 11:48:36 +0200232/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000233
Paul Bakker33b43f12013-08-20 11:48:36 +0200234/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100235void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Werner Lewisefda01f2022-07-06 13:03:36 +0100236 int mod, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200237 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000238{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200239 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200241 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000242
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100243 mbedtls_mpi N, E;
244 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
245
Ronald Cron351f0ee2020-06-10 12:12:18 +0200246 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000247
Ronald Cronc1905a12021-06-05 11:11:14 +0200248 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200249 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
250 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200251 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000252
Werner Lewis19b4cd82022-07-07 11:02:27 +0100253 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
254 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000255
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100256 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
257 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000259
Paul Bakker42a29bf2009-07-07 20:18:41 +0000260
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200261 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
262 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100263 &rnd_info, message_str->len,
264 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200265 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200266 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000267 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000268
Ronald Cronac6ae352020-06-26 14:33:03 +0200269 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
270 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000271 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100272
Paul Bakkerbd51b262014-07-10 15:26:12 +0200273exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100274 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000276}
Paul Bakker33b43f12013-08-20 11:48:36 +0200277/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000278
Paul Bakker33b43f12013-08-20 11:48:36 +0200279/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100280void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Werner Lewisefda01f2022-07-06 13:03:36 +0100281 int mod, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200282 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000283{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200284 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000286
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100287 mbedtls_mpi N, E;
288
289 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200290 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200291 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
292 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200293 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000294
Werner Lewis19b4cd82022-07-07 11:02:27 +0100295 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
296 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000297
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100298 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
299 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000301
Paul Bakkera6656852010-07-18 19:47:14 +0000302
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200303 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100304 NULL, message_str->len,
305 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200306 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200307 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000308 {
Paul Bakkera6656852010-07-18 19:47:14 +0000309
Ronald Cronac6ae352020-06-26 14:33:03 +0200310 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
311 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000312 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100313
Paul Bakkerbd51b262014-07-10 15:26:12 +0200314exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100315 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000317}
Paul Bakker33b43f12013-08-20 11:48:36 +0200318/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000319
Paul Bakker33b43f12013-08-20 11:48:36 +0200320/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100321void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Werner Lewis9802d362022-07-07 11:37:24 +0100322 int mod, char * input_P,
Werner Lewisefda01f2022-07-06 13:03:36 +0100323 char * input_Q, char * input_N,
324 char * input_E, int max_output,
325 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000326{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200327 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000329 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200330 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100331 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000332
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100333 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
334 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
335
Ronald Cronc1905a12021-06-05 11:11:14 +0200336 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200337 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
338 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000339
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200340 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200341 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000342
Paul Bakker42a29bf2009-07-07 20:18:41 +0000343
Werner Lewis19b4cd82022-07-07 11:02:27 +0100344 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
345 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
346 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
347 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000348
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100349 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
350 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100351 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000353
Paul Bakker69998dd2009-07-11 19:15:20 +0000354 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000355
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200356 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100357 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200358 &output_len, message_str->x, output,
359 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200360 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000361 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000362
Ronald Cronac6ae352020-06-26 14:33:03 +0200363 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200364 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200365 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000366 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000367
Paul Bakkerbd51b262014-07-10 15:26:12 +0200368exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100369 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
370 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000372}
Paul Bakker33b43f12013-08-20 11:48:36 +0200373/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000374
Paul Bakker33b43f12013-08-20 11:48:36 +0200375/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100376void mbedtls_rsa_public( data_t * message_str, int mod,
377 char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200378 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000379{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200380 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000382
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100383 mbedtls_mpi N, E;
384
385 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200386 mbedtls_rsa_init( &ctx );
387 mbedtls_rsa_init( &ctx2 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200388 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000389
Werner Lewis19b4cd82022-07-07 11:02:27 +0100390 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
391 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000392
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100393 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Gilles Peskine058d0092021-06-09 16:24:35 +0200394
395 /* Check test data consistency */
396 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100397 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000399
Azim Khand30ca132017-06-09 04:32:58 +0100400 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200401 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000402 {
Paul Bakker821fb082009-07-12 13:26:42 +0000403
Ronald Cronac6ae352020-06-26 14:33:03 +0200404 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
405 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000406 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100407
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100408 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200410 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100414
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200415 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100416 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100417 if( result == 0 )
418 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100419
Ronald Cronac6ae352020-06-26 14:33:03 +0200420 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
421 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100422 }
423
Paul Bakkerbd51b262014-07-10 15:26:12 +0200424exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100425 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_rsa_free( &ctx );
427 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000428}
Paul Bakker33b43f12013-08-20 11:48:36 +0200429/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000430
Paul Bakker33b43f12013-08-20 11:48:36 +0200431/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100432void mbedtls_rsa_private( data_t * message_str, int mod,
433 char * input_P, char * input_Q,
Werner Lewisefda01f2022-07-06 13:03:36 +0100434 char * input_N, char * input_E,
435 data_t * result_str, 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
Werner Lewis19b4cd82022-07-07 11:02:27 +0100450 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
451 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
452 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
453 TEST_ASSERT( mbedtls_test_read_mpi( &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 );
Gilles Peskine058d0092021-06-09 16:24:35 +0200456
457 /* Check test data consistency */
458 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100459 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100460 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000462
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200463 /* repeat three times to test updating of blinding values */
464 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000465 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200466 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200467 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
468 &rnd_info, message_str->x,
469 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200470 if( result == 0 )
471 {
Paul Bakker821fb082009-07-12 13:26:42 +0000472
Ronald Cronac6ae352020-06-26 14:33:03 +0200473 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200474 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200475 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200476 }
Paul Bakker821fb082009-07-12 13:26:42 +0000477 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000478
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100479 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200481 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100485
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200486 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200487 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
488 &rnd_info, message_str->x,
489 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100490 if( result == 0 )
491 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100492
Ronald Cronac6ae352020-06-26 14:33:03 +0200493 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200494 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200495 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100496 }
497
Paul Bakkerbd51b262014-07-10 15:26:12 +0200498exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100499 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
500 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000503}
Paul Bakker33b43f12013-08-20 11:48:36 +0200504/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000505
Paul Bakker33b43f12013-08-20 11:48:36 +0200506/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100507void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000508{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_rsa_context ctx;
510 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000513}
Paul Bakker33b43f12013-08-20 11:48:36 +0200514/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000515
Paul Bakker33b43f12013-08-20 11:48:36 +0200516/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100517void mbedtls_rsa_check_pubkey( char * input_N, char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000518{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100520 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000521
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100522 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200523 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000524
Paul Bakker33b43f12013-08-20 11:48:36 +0200525 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000526 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100527 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000528 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200529 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000530 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100531 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000532 }
533
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100534 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100536
Paul Bakkerbd51b262014-07-10 15:26:12 +0200537exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100538 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000540}
Paul Bakker33b43f12013-08-20 11:48:36 +0200541/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000542
Paul Bakker33b43f12013-08-20 11:48:36 +0200543/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100544void mbedtls_rsa_check_privkey( int mod, char * input_P, char * input_Q,
545 char * input_N, char * input_E, char * input_D,
546 char * input_DP, char * input_DQ, char * input_QP,
547 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000550
Ronald Cronc1905a12021-06-05 11:11:14 +0200551 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000552
Paul Bakker33b43f12013-08-20 11:48:36 +0200553 ctx.len = mod / 8;
554 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000555 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100556 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000557 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200558 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000559 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100560 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000561 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200562 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000563 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100564 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000565 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200566 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000567 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100568 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000569 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200570 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000571 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100572 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000573 }
Hanno Becker131134f2017-08-23 08:31:07 +0100574#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200575 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000576 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100577 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000578 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200579 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000580 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100581 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000582 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200583 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000584 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100585 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000586 }
Hanno Becker131134f2017-08-23 08:31:07 +0100587#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100588 ((void) input_DP);
589 ((void) input_DQ);
590 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100591#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100594
Paul Bakkerbd51b262014-07-10 15:26:12 +0200595exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000597}
Paul Bakker33b43f12013-08-20 11:48:36 +0200598/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000599
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100600/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100601void rsa_check_pubpriv( int mod, char * input_Npub, char * input_Epub,
602 char * input_P, char * input_Q, char * input_N,
603 char * input_E, char * input_D, char * input_DP,
604 char * input_DQ, char * input_QP, int result )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100605{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100607
Ronald Cronc1905a12021-06-05 11:11:14 +0200608 mbedtls_rsa_init( &pub );
609 mbedtls_rsa_init( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100610
611 pub.len = mod / 8;
612 prv.len = mod / 8;
613
614 if( strlen( input_Npub ) )
615 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100616 TEST_ASSERT( mbedtls_test_read_mpi( &pub.N, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100617 }
618 if( strlen( input_Epub ) )
619 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100620 TEST_ASSERT( mbedtls_test_read_mpi( &pub.E, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100621 }
622
623 if( strlen( input_P ) )
624 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100625 TEST_ASSERT( mbedtls_test_read_mpi( &prv.P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100626 }
627 if( strlen( input_Q ) )
628 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100629 TEST_ASSERT( mbedtls_test_read_mpi( &prv.Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100630 }
631 if( strlen( input_N ) )
632 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100633 TEST_ASSERT( mbedtls_test_read_mpi( &prv.N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100634 }
635 if( strlen( input_E ) )
636 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100637 TEST_ASSERT( mbedtls_test_read_mpi( &prv.E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100638 }
639 if( strlen( input_D ) )
640 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100641 TEST_ASSERT( mbedtls_test_read_mpi( &prv.D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100642 }
Hanno Becker131134f2017-08-23 08:31:07 +0100643#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100644 if( strlen( input_DP ) )
645 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100646 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100647 }
648 if( strlen( input_DQ ) )
649 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100650 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100651 }
652 if( strlen( input_QP ) )
653 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100654 TEST_ASSERT( mbedtls_test_read_mpi( &prv.QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100655 }
Hanno Becker131134f2017-08-23 08:31:07 +0100656#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100657 ((void) input_DP);
658 ((void) input_DQ);
659 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100660#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100663
664exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_rsa_free( &pub );
666 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100667}
668/* END_CASE */
669
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200670/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000672{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_rsa_context ctx;
Ronald Cronc1905a12021-06-05 11:11:14 +0200674 mbedtls_rsa_init ( &ctx );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000675
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200676 /* This test uses an insecure RNG, suitable only for testing.
677 * In production, always use a cryptographically strong RNG! */
678 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_test_rnd_std_rand, NULL, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200679 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100682 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000683 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100684
Paul Bakkerbd51b262014-07-10 15:26:12 +0200685exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000687}
Paul Bakker33b43f12013-08-20 11:48:36 +0200688/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000689
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +0200690/* BEGIN_CASE */
Hanno Becker0f65e0c2017-10-03 14:39:16 +0100691void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100692 int radix_D, char *input_D,
693 int radix_E, char *input_E,
694 int radix_P, char *output_P,
695 int radix_Q, char *output_Q,
696 int corrupt, int result )
697{
698 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
699
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100700 mbedtls_mpi_init( &N );
701 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
702 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
703 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
704
Werner Lewis19b4cd82022-07-07 11:02:27 +0100705 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
706 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
707 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
708 TEST_ASSERT( mbedtls_test_read_mpi( &Qp, output_P ) == 0 );
709 TEST_ASSERT( mbedtls_test_read_mpi( &Pp, output_Q ) == 0 );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100710
711 if( corrupt )
712 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
713
714 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100715 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100716
717 if( !corrupt )
718 {
719 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
720 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
721 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
722 }
723
724exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100725 mbedtls_mpi_free( &N );
726 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
727 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
728 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100729}
730/* END_CASE */
731
Hanno Becker6b4ce492017-08-23 11:00:21 +0100732/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100733void mbedtls_rsa_deduce_private_exponent( char *input_P,
734 char *input_Q,
735 char *input_E,
736 char *output_D,
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100737 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +0100738{
739 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
740
741 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
742 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
743 mbedtls_mpi_init( &E );
744 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
745
Werner Lewis19b4cd82022-07-07 11:02:27 +0100746 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
747 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
748 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
749 TEST_ASSERT( mbedtls_test_read_mpi( &Dp, output_D ) == 0 );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100750
751 if( corrupt )
752 {
753 /* Make E even */
754 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
755 }
756
757 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100758 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
759 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100760
761 if( !corrupt )
762 {
763 /*
764 * Check that D and Dp agree modulo LCM(P-1, Q-1).
765 */
766
767 /* Replace P,Q by P-1, Q-1 */
768 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
769 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
770
771 /* Check D == Dp modulo P-1 */
772 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
773 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
774 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
775
776 /* Check D == Dp modulo Q-1 */
777 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
778 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
779 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
780 }
781
782exit:
783
784 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
785 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
786 mbedtls_mpi_free( &E );
787 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
788}
789/* END_CASE */
790
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200791/* BEGIN_CASE */
Hanno Beckerc77ab892017-08-23 11:01:06 +0100792void mbedtls_rsa_import( int radix_N, char *input_N,
793 int radix_P, char *input_P,
794 int radix_Q, char *input_Q,
795 int radix_D, char *input_D,
796 int radix_E, char *input_E,
797 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +0100798 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +0100799 int res_check,
800 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100801{
802 mbedtls_mpi N, P, Q, D, E;
803 mbedtls_rsa_context ctx;
804
Hanno Beckere1582a82017-09-29 11:51:05 +0100805 /* Buffers used for encryption-decryption test */
806 unsigned char *buf_orig = NULL;
807 unsigned char *buf_enc = NULL;
808 unsigned char *buf_dec = NULL;
809
Hanno Becker4d6e8342017-09-29 11:50:18 +0100810 const int have_N = ( strlen( input_N ) > 0 );
811 const int have_P = ( strlen( input_P ) > 0 );
812 const int have_Q = ( strlen( input_Q ) > 0 );
813 const int have_D = ( strlen( input_D ) > 0 );
814 const int have_E = ( strlen( input_E ) > 0 );
815
Ronald Cronc1905a12021-06-05 11:11:14 +0200816 mbedtls_rsa_init( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100817
818 mbedtls_mpi_init( &N );
819 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
820 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
821
Hanno Becker4d6e8342017-09-29 11:50:18 +0100822 if( have_N )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100823 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100824
Hanno Becker4d6e8342017-09-29 11:50:18 +0100825 if( have_P )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100826 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100827
Hanno Becker4d6e8342017-09-29 11:50:18 +0100828 if( have_Q )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100829 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100830
Hanno Becker4d6e8342017-09-29 11:50:18 +0100831 if( have_D )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100832 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100833
Hanno Becker4d6e8342017-09-29 11:50:18 +0100834 if( have_E )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100835 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100836
837 if( !successive )
838 {
839 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100840 have_N ? &N : NULL,
841 have_P ? &P : NULL,
842 have_Q ? &Q : NULL,
843 have_D ? &D : NULL,
844 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100845 }
846 else
847 {
848 /* Import N, P, Q, D, E separately.
849 * This should make no functional difference. */
850
851 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100852 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100853 NULL, NULL, NULL, NULL ) == 0 );
854
855 TEST_ASSERT( mbedtls_rsa_import( &ctx,
856 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100857 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100858 NULL, NULL, NULL ) == 0 );
859
860 TEST_ASSERT( mbedtls_rsa_import( &ctx,
861 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100862 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100863 NULL, NULL ) == 0 );
864
865 TEST_ASSERT( mbedtls_rsa_import( &ctx,
866 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100867 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100868 NULL ) == 0 );
869
870 TEST_ASSERT( mbedtls_rsa_import( &ctx,
871 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100872 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100873 }
874
Hanno Becker04877a42017-10-11 10:01:33 +0100875 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100876
Hanno Beckere1582a82017-09-29 11:51:05 +0100877 /* On expected success, perform some public and private
878 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +0100879 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +0100880 {
Hanno Beckere1582a82017-09-29 11:51:05 +0100881 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +0100882 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
883 else
884 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
885
886 if( res_check != 0 )
887 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +0100888
889 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
890 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
891 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
892 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
893 goto exit;
894
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200895 /* This test uses an insecure RNG, suitable only for testing.
896 * In production, always use a cryptographically strong RNG! */
897 TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL,
Hanno Beckere1582a82017-09-29 11:51:05 +0100898 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
899
900 /* Make sure the number we're generating is smaller than the modulus */
901 buf_orig[0] = 0x00;
902
903 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
904
905 if( is_priv )
906 {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200907 /* This test uses an insecure RNG, suitable only for testing.
908 * In production, always use a cryptographically strong RNG! */
909 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_std_rand,
910 NULL, buf_enc,
Hanno Beckere1582a82017-09-29 11:51:05 +0100911 buf_dec ) == 0 );
912
913 TEST_ASSERT( memcmp( buf_orig, buf_dec,
914 mbedtls_rsa_get_len( &ctx ) ) == 0 );
915 }
916 }
917
Hanno Beckerc77ab892017-08-23 11:01:06 +0100918exit:
919
Hanno Beckere1582a82017-09-29 11:51:05 +0100920 mbedtls_free( buf_orig );
921 mbedtls_free( buf_enc );
922 mbedtls_free( buf_dec );
923
Hanno Beckerc77ab892017-08-23 11:01:06 +0100924 mbedtls_rsa_free( &ctx );
925
Hanno Beckerc77ab892017-08-23 11:01:06 +0100926 mbedtls_mpi_free( &N );
927 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
928 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
929}
930/* END_CASE */
931
Hanno Becker417f2d62017-08-23 11:44:51 +0100932/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100933void mbedtls_rsa_export( char *input_N,
934 char *input_P,
935 char *input_Q,
936 char *input_D,
937 char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +0100938 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +0100939 int successive )
940{
941 /* Original MPI's with which we set up the RSA context */
942 mbedtls_mpi N, P, Q, D, E;
943
944 /* Exported MPI's */
945 mbedtls_mpi Ne, Pe, Qe, De, Ee;
946
947 const int have_N = ( strlen( input_N ) > 0 );
948 const int have_P = ( strlen( input_P ) > 0 );
949 const int have_Q = ( strlen( input_Q ) > 0 );
950 const int have_D = ( strlen( input_D ) > 0 );
951 const int have_E = ( strlen( input_E ) > 0 );
952
Hanno Becker417f2d62017-08-23 11:44:51 +0100953 mbedtls_rsa_context ctx;
954
Ronald Cronc1905a12021-06-05 11:11:14 +0200955 mbedtls_rsa_init( &ctx );
Hanno Becker417f2d62017-08-23 11:44:51 +0100956
957 mbedtls_mpi_init( &N );
958 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
959 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
960
961 mbedtls_mpi_init( &Ne );
962 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
963 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
964
965 /* Setup RSA context */
966
967 if( have_N )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100968 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +0100969
970 if( have_P )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100971 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +0100972
973 if( have_Q )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100974 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +0100975
976 if( have_D )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100977 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +0100978
979 if( have_E )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100980 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +0100981
982 TEST_ASSERT( mbedtls_rsa_import( &ctx,
983 strlen( input_N ) ? &N : NULL,
984 strlen( input_P ) ? &P : NULL,
985 strlen( input_Q ) ? &Q : NULL,
986 strlen( input_D ) ? &D : NULL,
987 strlen( input_E ) ? &E : NULL ) == 0 );
988
Hanno Becker7f25f852017-10-10 16:56:22 +0100989 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +0100990
991 /*
992 * Export parameters and compare to original ones.
993 */
994
995 /* N and E must always be present. */
996 if( !successive )
997 {
998 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
999 }
1000 else
1001 {
1002 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1003 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1004 }
1005 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1006 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1007
1008 /* If we were providing enough information to setup a complete private context,
1009 * we expect to be able to export all core parameters. */
1010
1011 if( is_priv )
1012 {
1013 if( !successive )
1014 {
1015 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1016 &De, NULL ) == 0 );
1017 }
1018 else
1019 {
1020 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1021 NULL, NULL ) == 0 );
1022 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1023 NULL, NULL ) == 0 );
1024 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1025 &De, NULL ) == 0 );
1026 }
1027
1028 if( have_P )
1029 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1030
1031 if( have_Q )
1032 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1033
1034 if( have_D )
1035 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1036
1037 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001038 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1039 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001040 }
1041
1042exit:
1043
1044 mbedtls_rsa_free( &ctx );
1045
1046 mbedtls_mpi_free( &N );
1047 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1048 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1049
1050 mbedtls_mpi_free( &Ne );
1051 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1052 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1053}
1054/* END_CASE */
1055
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001056/* BEGIN_CASE */
Hanno Becker750e8b42017-08-25 07:54:27 +01001057void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1058 int radix_P, char *input_P,
1059 int radix_Q, char *input_Q,
1060 int radix_D, char *input_D,
1061 int radix_E, char *input_E,
1062 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001063{
1064 /* Original MPI's with which we set up the RSA context */
1065 mbedtls_mpi N, P, Q, D, E;
1066
1067 const int have_N = ( strlen( input_N ) > 0 );
1068 const int have_P = ( strlen( input_P ) > 0 );
1069 const int have_Q = ( strlen( input_Q ) > 0 );
1070 const int have_D = ( strlen( input_D ) > 0 );
1071 const int have_E = ( strlen( input_E ) > 0 );
1072
Hanno Beckerce002632017-08-23 13:22:36 +01001073 mbedtls_mpi_init( &N );
1074 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1075 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1076
Hanno Beckerce002632017-08-23 13:22:36 +01001077 if( have_N )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001078 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001079
1080 if( have_P )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001081 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001082
1083 if( have_Q )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001084 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001085
1086 if( have_D )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001087 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001088
1089 if( have_E )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001090 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001091
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001092 /* This test uses an insecure RNG, suitable only for testing.
1093 * In production, always use a cryptographically strong RNG! */
Hanno Becker750e8b42017-08-25 07:54:27 +01001094 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1095 have_P ? &P : NULL,
1096 have_Q ? &Q : NULL,
1097 have_D ? &D : NULL,
1098 have_E ? &E : NULL,
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001099 prng ? mbedtls_test_rnd_std_rand : NULL,
1100 prng ? NULL : NULL ) == result );
1101
Hanno Beckerce002632017-08-23 13:22:36 +01001102exit:
Hanno Beckerce002632017-08-23 13:22:36 +01001103 mbedtls_mpi_free( &N );
1104 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1105 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1106}
1107/* END_CASE */
1108
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +02001109/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001110void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1111 data_t *input_Q, data_t *input_D,
1112 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001113 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001114{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001115 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001116 unsigned char bufNe[256];
1117 unsigned char bufPe[128];
1118 unsigned char bufQe[128];
1119 unsigned char bufDe[256];
1120 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001121
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001122 mbedtls_rsa_context ctx;
1123
Ronald Cronc1905a12021-06-05 11:11:14 +02001124 mbedtls_rsa_init( &ctx );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001125
1126 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001127 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001128 input_N->len ? input_N->x : NULL, input_N->len,
1129 input_P->len ? input_P->x : NULL, input_P->len,
1130 input_Q->len ? input_Q->x : NULL, input_Q->len,
1131 input_D->len ? input_D->x : NULL, input_D->len,
1132 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001133
Hanno Becker7f25f852017-10-10 16:56:22 +01001134 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001135
1136 /*
1137 * Export parameters and compare to original ones.
1138 */
1139
1140 /* N and E must always be present. */
1141 if( !successive )
1142 {
Azim Khand30ca132017-06-09 04:32:58 +01001143 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001144 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001145 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001146 }
1147 else
1148 {
Azim Khand30ca132017-06-09 04:32:58 +01001149 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001150 NULL, 0, NULL, 0, NULL, 0,
1151 NULL, 0 ) == 0 );
1152 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1153 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001154 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001155 }
Azim Khand30ca132017-06-09 04:32:58 +01001156 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1157 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001158
1159 /* If we were providing enough information to setup a complete private context,
1160 * we expect to be able to export all core parameters. */
1161
1162 if( is_priv )
1163 {
1164 if( !successive )
1165 {
1166 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001167 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1168 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1169 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001170 NULL, 0 ) == 0 );
1171 }
1172 else
1173 {
1174 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001175 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001176 NULL, 0, NULL, 0,
1177 NULL, 0 ) == 0 );
1178
1179 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001180 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001181 NULL, 0, NULL, 0 ) == 0 );
1182
Azim Khand30ca132017-06-09 04:32:58 +01001183 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1184 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001185 NULL, 0 ) == 0 );
1186 }
1187
Azim Khand30ca132017-06-09 04:32:58 +01001188 if( input_P->len )
1189 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001190
Azim Khand30ca132017-06-09 04:32:58 +01001191 if( input_Q->len )
1192 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001193
Azim Khand30ca132017-06-09 04:32:58 +01001194 if( input_D->len )
1195 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001196
1197 }
1198
1199exit:
1200 mbedtls_rsa_free( &ctx );
1201}
1202/* END_CASE */
1203
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001204/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001205void mbedtls_rsa_import_raw( data_t *input_N,
1206 data_t *input_P, data_t *input_Q,
1207 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001208 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001209 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001210 int res_check,
1211 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001212{
Hanno Beckere1582a82017-09-29 11:51:05 +01001213 /* Buffers used for encryption-decryption test */
1214 unsigned char *buf_orig = NULL;
1215 unsigned char *buf_enc = NULL;
1216 unsigned char *buf_dec = NULL;
1217
Hanno Beckerc77ab892017-08-23 11:01:06 +01001218 mbedtls_rsa_context ctx;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001219
Ronald Cronc1905a12021-06-05 11:11:14 +02001220 mbedtls_rsa_init( &ctx );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001221
Hanno Beckerc77ab892017-08-23 11:01:06 +01001222 if( !successive )
1223 {
1224 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001225 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1226 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1227 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1228 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1229 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001230 }
1231 else
1232 {
1233 /* Import N, P, Q, D, E separately.
1234 * This should make no functional difference. */
1235
1236 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001237 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001238 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1239
1240 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1241 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001242 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001243 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1244
1245 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1246 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001247 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001248 NULL, 0, NULL, 0 ) == 0 );
1249
1250 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1251 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001252 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001253 NULL, 0 ) == 0 );
1254
1255 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1256 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001257 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001258 }
1259
Hanno Becker04877a42017-10-11 10:01:33 +01001260 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001261
Hanno Beckere1582a82017-09-29 11:51:05 +01001262 /* On expected success, perform some public and private
1263 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001264 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001265 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001266 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001267 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1268 else
1269 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1270
1271 if( res_check != 0 )
1272 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001273
1274 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1275 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1276 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1277 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1278 goto exit;
1279
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001280 /* This test uses an insecure RNG, suitable only for testing.
1281 * In production, always use a cryptographically strong RNG! */
1282 TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL,
Hanno Beckere1582a82017-09-29 11:51:05 +01001283 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1284
1285 /* Make sure the number we're generating is smaller than the modulus */
1286 buf_orig[0] = 0x00;
1287
1288 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1289
1290 if( is_priv )
1291 {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001292 /* This test uses an insecure RNG, suitable only for testing.
1293 * In production, always use a cryptographically strong RNG! */
1294 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_std_rand,
1295 NULL, buf_enc,
Hanno Beckere1582a82017-09-29 11:51:05 +01001296 buf_dec ) == 0 );
1297
1298 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1299 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1300 }
1301 }
1302
Hanno Beckerc77ab892017-08-23 11:01:06 +01001303exit:
1304
Hanno Becker3f3ae852017-10-02 10:08:39 +01001305 mbedtls_free( buf_orig );
1306 mbedtls_free( buf_enc );
1307 mbedtls_free( buf_dec );
1308
Hanno Beckerc77ab892017-08-23 11:01:06 +01001309 mbedtls_rsa_free( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001310}
1311/* END_CASE */
1312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001314void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001315{
Andres AG93012e82016-09-09 09:10:28 +01001316 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001317}
Paul Bakker33b43f12013-08-20 11:48:36 +02001318/* END_CASE */