blob: 63163a679ec4345804134ac215a3f5468d031299 [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,
Azim Khand30ca132017-06-09 04:32:58 +010076 int digest, int mod, int radix_P, char * input_P,
77 int radix_Q, char * input_Q, int radix_N,
78 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +020079 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000080{
Ron Eldorfdc15bd2018-11-22 15:47:51 +020081 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010083 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +020084 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +000085
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010086 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
87 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +020088 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +020089 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,padding_mode,
90 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000091
Ron Eldorfdc15bd2018-11-22 15:47:51 +020092 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +020093 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +000094
Gilles Peskine20edee72021-06-10 23:18:39 +020095 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
96 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
97 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
98 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000099
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100100 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
101 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100102 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000104
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200105 TEST_ASSERT( mbedtls_rsa_pkcs1_sign(
106 &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnarda4aa12f2022-07-16 08:20:26 +0200107 digest, message_str->len, message_str->x,
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200108 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200109 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000110 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000111
Ronald Cronac6ae352020-06-26 14:33:03 +0200112 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
113 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000114 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000115
Paul Bakkerbd51b262014-07-10 15:26:12 +0200116exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100117 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
118 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000120}
Paul Bakker33b43f12013-08-20 11:48:36 +0200121/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000122
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200123/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100124void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100125 int digest, int mod, int radix_N,
126 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100127 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100130 mbedtls_mpi N, E;
131
132 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200133 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200134 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
135 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000136
Gilles Peskine20edee72021-06-10 23:18:39 +0200137 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
138 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100139 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
140 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000142
Manuel Pégourié-Gonnarda4aa12f2022-07-16 08:20:26 +0200143 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 +0100144
Paul Bakkerbd51b262014-07-10 15:26:12 +0200145exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100146 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000148}
Paul Bakker33b43f12013-08-20 11:48:36 +0200149/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000150
Paul Bakker821fb082009-07-12 13:26:42 +0000151
Paul Bakker33b43f12013-08-20 11:48:36 +0200152/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100153void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100154 int padding_mode, int mod, int radix_P,
155 char * input_P, int radix_Q, char * input_Q,
156 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200157 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000158{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200159 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100161 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200162 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000163
Ronald Cronc1905a12021-06-05 11:11:14 +0200164 mbedtls_rsa_init( &ctx );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100165 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
166 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000167
Paul Elliotte57dd2d2021-06-25 11:13:24 +0100168 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
169 MBEDTLS_MD_NONE ) == 0 );
170
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200171 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200172 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000173
Gilles Peskine20edee72021-06-10 23:18:39 +0200174 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
175 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
176 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
177 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000178
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100179 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
180 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100181 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000183
Paul Bakker821fb082009-07-12 13:26:42 +0000184
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200185 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100186 &rnd_info, MBEDTLS_MD_NONE,
187 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200188 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000189
Paul Bakker821fb082009-07-12 13:26:42 +0000190
Ronald Cronac6ae352020-06-26 14:33:03 +0200191 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
192 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000193
Paul Bakkerbd51b262014-07-10 15:26:12 +0200194exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100195 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
196 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000199}
Paul Bakker33b43f12013-08-20 11:48:36 +0200200/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000201
Paul Bakker33b43f12013-08-20 11:48:36 +0200202/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100203void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200204 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100205 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100206 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000207{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200208 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000210
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100211 mbedtls_mpi N, E;
212 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
213
Ronald Cronc1905a12021-06-05 11:11:14 +0200214 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200215 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
216 MBEDTLS_MD_NONE ) == 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100217 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000218
Gilles Peskine20edee72021-06-10 23:18:39 +0200219 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
220 TEST_ASSERT( mbedtls_test_read_mpi( &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 );
Ronald Cron266b6d22021-06-08 10:03:49 +0200251 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
252 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200253 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000254
Gilles Peskine20edee72021-06-10 23:18:39 +0200255 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
256 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000257
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100258 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
259 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000261
Paul Bakker42a29bf2009-07-07 20:18:41 +0000262
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200263 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
264 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100265 &rnd_info, message_str->len,
266 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200267 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200268 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000269 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000270
Ronald Cronac6ae352020-06-26 14:33:03 +0200271 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
272 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000273 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100274
Paul Bakkerbd51b262014-07-10 15:26:12 +0200275exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100276 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000278}
Paul Bakker33b43f12013-08-20 11:48:36 +0200279/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000280
Paul Bakker33b43f12013-08-20 11:48:36 +0200281/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100282void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100283 int mod, int radix_N, char * input_N,
284 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200285 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000286{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200287 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000289
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100290 mbedtls_mpi N, E;
291
292 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200293 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200294 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
295 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200296 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000297
Gilles Peskine20edee72021-06-10 23:18:39 +0200298 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
299 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000300
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100301 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
302 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000304
Paul Bakkera6656852010-07-18 19:47:14 +0000305
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200306 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100307 NULL, message_str->len,
308 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200309 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200310 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000311 {
Paul Bakkera6656852010-07-18 19:47:14 +0000312
Ronald Cronac6ae352020-06-26 14:33:03 +0200313 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
314 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000315 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100316
Paul Bakkerbd51b262014-07-10 15:26:12 +0200317exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100318 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000320}
Paul Bakker33b43f12013-08-20 11:48:36 +0200321/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000322
Paul Bakker33b43f12013-08-20 11:48:36 +0200323/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100324void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100325 int mod, int radix_P, char * input_P,
326 int radix_Q, char * input_Q, int radix_N,
327 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200328 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100329 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000330{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200331 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000333 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200334 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100335 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000336
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100337 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
338 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
339
Ronald Cronc1905a12021-06-05 11:11:14 +0200340 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200341 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
342 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000343
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200344 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200345 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000346
Paul Bakker42a29bf2009-07-07 20:18:41 +0000347
Gilles Peskine20edee72021-06-10 23:18:39 +0200348 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
349 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
350 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
351 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000352
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100353 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
354 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100355 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000357
Paul Bakker69998dd2009-07-11 19:15:20 +0000358 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000359
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200360 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100361 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200362 &output_len, message_str->x, output,
363 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200364 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000365 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000366
Ronald Cronac6ae352020-06-26 14:33:03 +0200367 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200368 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200369 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000370 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000371
Paul Bakkerbd51b262014-07-10 15:26:12 +0200372exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100373 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
374 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000376}
Paul Bakker33b43f12013-08-20 11:48:36 +0200377/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000378
Paul Bakker33b43f12013-08-20 11:48:36 +0200379/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100380void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100381 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200382 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000383{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200384 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000386
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100387 mbedtls_mpi N, E;
388
389 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200390 mbedtls_rsa_init( &ctx );
391 mbedtls_rsa_init( &ctx2 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200392 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000393
Gilles Peskine20edee72021-06-10 23:18:39 +0200394 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
395 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000396
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100397 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Gilles Peskine058d0092021-06-09 16:24:35 +0200398
399 /* Check test data consistency */
400 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100401 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000403
Azim Khand30ca132017-06-09 04:32:58 +0100404 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200405 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000406 {
Paul Bakker821fb082009-07-12 13:26:42 +0000407
Ronald Cronac6ae352020-06-26 14:33:03 +0200408 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
409 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000410 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100411
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100412 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200414 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100418
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200419 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100420 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100421 if( result == 0 )
422 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100423
Ronald Cronac6ae352020-06-26 14:33:03 +0200424 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
425 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100426 }
427
Paul Bakkerbd51b262014-07-10 15:26:12 +0200428exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100429 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_rsa_free( &ctx );
431 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000432}
Paul Bakker33b43f12013-08-20 11:48:36 +0200433/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000434
Paul Bakker33b43f12013-08-20 11:48:36 +0200435/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100436void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100437 char * input_P, int radix_Q, char * input_Q,
438 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200439 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100440 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000441{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200442 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100444 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200445 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200446 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000447
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100448 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
449 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200450 mbedtls_rsa_init( &ctx );
451 mbedtls_rsa_init( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000452
Ronald Cron351f0ee2020-06-10 12:12:18 +0200453 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000454
Gilles Peskine20edee72021-06-10 23:18:39 +0200455 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
456 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
457 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
458 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000459
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100460 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
Gilles Peskine058d0092021-06-09 16:24:35 +0200461
462 /* Check test data consistency */
463 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100464 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100465 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000467
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200468 /* repeat three times to test updating of blinding values */
469 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000470 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200471 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200472 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
473 &rnd_info, message_str->x,
474 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200475 if( result == 0 )
476 {
Paul Bakker821fb082009-07-12 13:26:42 +0000477
Ronald Cronac6ae352020-06-26 14:33:03 +0200478 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200479 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200480 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200481 }
Paul Bakker821fb082009-07-12 13:26:42 +0000482 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000483
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100484 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200486 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100490
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200491 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200492 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
493 &rnd_info, message_str->x,
494 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100495 if( result == 0 )
496 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100497
Ronald Cronac6ae352020-06-26 14:33:03 +0200498 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200499 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200500 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100501 }
502
Paul Bakkerbd51b262014-07-10 15:26:12 +0200503exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100504 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
505 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000508}
Paul Bakker33b43f12013-08-20 11:48:36 +0200509/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000510
Paul Bakker33b43f12013-08-20 11:48:36 +0200511/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100512void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000513{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_rsa_context ctx;
515 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000518}
Paul Bakker33b43f12013-08-20 11:48:36 +0200519/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000520
Paul Bakker33b43f12013-08-20 11:48:36 +0200521/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100522void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
523 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000524{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100526 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000527
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100528 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200529 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000530
Paul Bakker33b43f12013-08-20 11:48:36 +0200531 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000532 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200533 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000534 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200535 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000536 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200537 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000538 }
539
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100540 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100542
Paul Bakkerbd51b262014-07-10 15:26:12 +0200543exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100544 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000546}
Paul Bakker33b43f12013-08-20 11:48:36 +0200547/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000548
Paul Bakker33b43f12013-08-20 11:48:36 +0200549/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100550void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
551 int radix_Q, char * input_Q, int radix_N,
552 char * input_N, int radix_E, char * input_E,
553 int radix_D, char * input_D, int radix_DP,
554 char * input_DP, int radix_DQ,
555 char * input_DQ, int radix_QP,
556 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000557{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000559
Ronald Cronc1905a12021-06-05 11:11:14 +0200560 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000561
Paul Bakker33b43f12013-08-20 11:48:36 +0200562 ctx.len = mod / 8;
563 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000564 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200565 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000566 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200567 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000568 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200569 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000570 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200571 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000572 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200573 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000574 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200575 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000576 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200577 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000578 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200579 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000580 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200581 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000582 }
Hanno Becker131134f2017-08-23 08:31:07 +0100583#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200584 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000585 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200586 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000587 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200588 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000589 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200590 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000591 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200592 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000593 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200594 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000595 }
Hanno Becker131134f2017-08-23 08:31:07 +0100596#else
597 ((void) radix_DP); ((void) input_DP);
598 ((void) radix_DQ); ((void) input_DQ);
599 ((void) radix_QP); ((void) input_QP);
600#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100603
Paul Bakkerbd51b262014-07-10 15:26:12 +0200604exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000606}
Paul Bakker33b43f12013-08-20 11:48:36 +0200607/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000608
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100609/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100610void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
611 int radix_Epub, char * input_Epub, int radix_P,
612 char * input_P, int radix_Q, char * input_Q,
613 int radix_N, char * input_N, int radix_E,
614 char * input_E, int radix_D, char * input_D,
615 int radix_DP, char * input_DP, int radix_DQ,
616 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100617 int result )
618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100620
Ronald Cronc1905a12021-06-05 11:11:14 +0200621 mbedtls_rsa_init( &pub );
622 mbedtls_rsa_init( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100623
624 pub.len = mod / 8;
625 prv.len = mod / 8;
626
627 if( strlen( input_Npub ) )
628 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200629 TEST_ASSERT( mbedtls_test_read_mpi( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100630 }
631 if( strlen( input_Epub ) )
632 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200633 TEST_ASSERT( mbedtls_test_read_mpi( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100634 }
635
636 if( strlen( input_P ) )
637 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200638 TEST_ASSERT( mbedtls_test_read_mpi( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100639 }
640 if( strlen( input_Q ) )
641 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200642 TEST_ASSERT( mbedtls_test_read_mpi( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100643 }
644 if( strlen( input_N ) )
645 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200646 TEST_ASSERT( mbedtls_test_read_mpi( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100647 }
648 if( strlen( input_E ) )
649 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200650 TEST_ASSERT( mbedtls_test_read_mpi( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100651 }
652 if( strlen( input_D ) )
653 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200654 TEST_ASSERT( mbedtls_test_read_mpi( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100655 }
Hanno Becker131134f2017-08-23 08:31:07 +0100656#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100657 if( strlen( input_DP ) )
658 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200659 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100660 }
661 if( strlen( input_DQ ) )
662 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200663 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100664 }
665 if( strlen( input_QP ) )
666 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200667 TEST_ASSERT( mbedtls_test_read_mpi( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100668 }
Hanno Becker131134f2017-08-23 08:31:07 +0100669#else
670 ((void) radix_DP); ((void) input_DP);
671 ((void) radix_DQ); ((void) input_DQ);
672 ((void) radix_QP); ((void) input_QP);
673#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100676
677exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_rsa_free( &pub );
679 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100680}
681/* END_CASE */
682
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200683/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000685{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_rsa_context ctx;
Ronald Cronc1905a12021-06-05 11:11:14 +0200687 mbedtls_rsa_init ( &ctx );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000688
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200689 /* This test uses an insecure RNG, suitable only for testing.
690 * In production, always use a cryptographically strong RNG! */
691 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_test_rnd_std_rand, NULL, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200692 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100695 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000696 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100697
Paul Bakkerbd51b262014-07-10 15:26:12 +0200698exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000700}
Paul Bakker33b43f12013-08-20 11:48:36 +0200701/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000702
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +0200703/* BEGIN_CASE */
Hanno Becker0f65e0c2017-10-03 14:39:16 +0100704void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100705 int radix_D, char *input_D,
706 int radix_E, char *input_E,
707 int radix_P, char *output_P,
708 int radix_Q, char *output_Q,
709 int corrupt, int result )
710{
711 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
712
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100713 mbedtls_mpi_init( &N );
714 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
715 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
716 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
717
Gilles Peskine20edee72021-06-10 23:18:39 +0200718 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
719 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 );
720 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
721 TEST_ASSERT( mbedtls_test_read_mpi( &Qp, radix_P, output_P ) == 0 );
722 TEST_ASSERT( mbedtls_test_read_mpi( &Pp, radix_Q, output_Q ) == 0 );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100723
724 if( corrupt )
725 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
726
727 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100728 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100729
730 if( !corrupt )
731 {
732 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
733 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
734 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
735 }
736
737exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100738 mbedtls_mpi_free( &N );
739 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
740 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
741 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100742}
743/* END_CASE */
744
Hanno Becker6b4ce492017-08-23 11:00:21 +0100745/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100746void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
747 int radix_Q, char *input_Q,
748 int radix_E, char *input_E,
749 int radix_D, char *output_D,
750 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +0100751{
752 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
753
754 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
755 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
756 mbedtls_mpi_init( &E );
757 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
758
Gilles Peskine20edee72021-06-10 23:18:39 +0200759 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
760 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
761 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
762 TEST_ASSERT( mbedtls_test_read_mpi( &Dp, radix_D, output_D ) == 0 );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100763
764 if( corrupt )
765 {
766 /* Make E even */
767 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
768 }
769
770 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100771 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
772 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100773
774 if( !corrupt )
775 {
776 /*
777 * Check that D and Dp agree modulo LCM(P-1, Q-1).
778 */
779
780 /* Replace P,Q by P-1, Q-1 */
781 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
782 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
783
784 /* Check D == Dp modulo P-1 */
785 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
786 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
787 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
788
789 /* Check D == Dp modulo Q-1 */
790 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
791 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
792 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
793 }
794
795exit:
796
797 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
798 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
799 mbedtls_mpi_free( &E );
800 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
801}
802/* END_CASE */
803
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200804/* BEGIN_CASE */
Hanno Beckerc77ab892017-08-23 11:01:06 +0100805void mbedtls_rsa_import( int radix_N, char *input_N,
806 int radix_P, char *input_P,
807 int radix_Q, char *input_Q,
808 int radix_D, char *input_D,
809 int radix_E, char *input_E,
810 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +0100811 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +0100812 int res_check,
813 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100814{
815 mbedtls_mpi N, P, Q, D, E;
816 mbedtls_rsa_context ctx;
817
Hanno Beckere1582a82017-09-29 11:51:05 +0100818 /* Buffers used for encryption-decryption test */
819 unsigned char *buf_orig = NULL;
820 unsigned char *buf_enc = NULL;
821 unsigned char *buf_dec = NULL;
822
Hanno Becker4d6e8342017-09-29 11:50:18 +0100823 const int have_N = ( strlen( input_N ) > 0 );
824 const int have_P = ( strlen( input_P ) > 0 );
825 const int have_Q = ( strlen( input_Q ) > 0 );
826 const int have_D = ( strlen( input_D ) > 0 );
827 const int have_E = ( strlen( input_E ) > 0 );
828
Ronald Cronc1905a12021-06-05 11:11:14 +0200829 mbedtls_rsa_init( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100830
831 mbedtls_mpi_init( &N );
832 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
833 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
834
Hanno Becker4d6e8342017-09-29 11:50:18 +0100835 if( have_N )
Gilles Peskine20edee72021-06-10 23:18:39 +0200836 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100837
Hanno Becker4d6e8342017-09-29 11:50:18 +0100838 if( have_P )
Gilles Peskine20edee72021-06-10 23:18:39 +0200839 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100840
Hanno Becker4d6e8342017-09-29 11:50:18 +0100841 if( have_Q )
Gilles Peskine20edee72021-06-10 23:18:39 +0200842 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100843
Hanno Becker4d6e8342017-09-29 11:50:18 +0100844 if( have_D )
Gilles Peskine20edee72021-06-10 23:18:39 +0200845 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100846
Hanno Becker4d6e8342017-09-29 11:50:18 +0100847 if( have_E )
Gilles Peskine20edee72021-06-10 23:18:39 +0200848 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100849
850 if( !successive )
851 {
852 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100853 have_N ? &N : NULL,
854 have_P ? &P : NULL,
855 have_Q ? &Q : NULL,
856 have_D ? &D : NULL,
857 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100858 }
859 else
860 {
861 /* Import N, P, Q, D, E separately.
862 * This should make no functional difference. */
863
864 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100865 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100866 NULL, NULL, NULL, NULL ) == 0 );
867
868 TEST_ASSERT( mbedtls_rsa_import( &ctx,
869 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100870 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100871 NULL, NULL, NULL ) == 0 );
872
873 TEST_ASSERT( mbedtls_rsa_import( &ctx,
874 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100875 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100876 NULL, NULL ) == 0 );
877
878 TEST_ASSERT( mbedtls_rsa_import( &ctx,
879 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100880 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100881 NULL ) == 0 );
882
883 TEST_ASSERT( mbedtls_rsa_import( &ctx,
884 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100885 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100886 }
887
Hanno Becker04877a42017-10-11 10:01:33 +0100888 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100889
Hanno Beckere1582a82017-09-29 11:51:05 +0100890 /* On expected success, perform some public and private
891 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +0100892 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +0100893 {
Hanno Beckere1582a82017-09-29 11:51:05 +0100894 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +0100895 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
896 else
897 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
898
899 if( res_check != 0 )
900 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +0100901
902 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
903 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
904 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
905 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
906 goto exit;
907
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200908 /* This test uses an insecure RNG, suitable only for testing.
909 * In production, always use a cryptographically strong RNG! */
910 TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL,
Hanno Beckere1582a82017-09-29 11:51:05 +0100911 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
912
913 /* Make sure the number we're generating is smaller than the modulus */
914 buf_orig[0] = 0x00;
915
916 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
917
918 if( is_priv )
919 {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200920 /* This test uses an insecure RNG, suitable only for testing.
921 * In production, always use a cryptographically strong RNG! */
922 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_std_rand,
923 NULL, buf_enc,
Hanno Beckere1582a82017-09-29 11:51:05 +0100924 buf_dec ) == 0 );
925
926 TEST_ASSERT( memcmp( buf_orig, buf_dec,
927 mbedtls_rsa_get_len( &ctx ) ) == 0 );
928 }
929 }
930
Hanno Beckerc77ab892017-08-23 11:01:06 +0100931exit:
932
Hanno Beckere1582a82017-09-29 11:51:05 +0100933 mbedtls_free( buf_orig );
934 mbedtls_free( buf_enc );
935 mbedtls_free( buf_dec );
936
Hanno Beckerc77ab892017-08-23 11:01:06 +0100937 mbedtls_rsa_free( &ctx );
938
Hanno Beckerc77ab892017-08-23 11:01:06 +0100939 mbedtls_mpi_free( &N );
940 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
941 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
942}
943/* END_CASE */
944
Hanno Becker417f2d62017-08-23 11:44:51 +0100945/* BEGIN_CASE */
946void mbedtls_rsa_export( int radix_N, char *input_N,
947 int radix_P, char *input_P,
948 int radix_Q, char *input_Q,
949 int radix_D, char *input_D,
950 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +0100951 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +0100952 int successive )
953{
954 /* Original MPI's with which we set up the RSA context */
955 mbedtls_mpi N, P, Q, D, E;
956
957 /* Exported MPI's */
958 mbedtls_mpi Ne, Pe, Qe, De, Ee;
959
960 const int have_N = ( strlen( input_N ) > 0 );
961 const int have_P = ( strlen( input_P ) > 0 );
962 const int have_Q = ( strlen( input_Q ) > 0 );
963 const int have_D = ( strlen( input_D ) > 0 );
964 const int have_E = ( strlen( input_E ) > 0 );
965
Hanno Becker417f2d62017-08-23 11:44:51 +0100966 mbedtls_rsa_context ctx;
967
Ronald Cronc1905a12021-06-05 11:11:14 +0200968 mbedtls_rsa_init( &ctx );
Hanno Becker417f2d62017-08-23 11:44:51 +0100969
970 mbedtls_mpi_init( &N );
971 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
972 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
973
974 mbedtls_mpi_init( &Ne );
975 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
976 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
977
978 /* Setup RSA context */
979
980 if( have_N )
Gilles Peskine20edee72021-06-10 23:18:39 +0200981 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +0100982
983 if( have_P )
Gilles Peskine20edee72021-06-10 23:18:39 +0200984 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +0100985
986 if( have_Q )
Gilles Peskine20edee72021-06-10 23:18:39 +0200987 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +0100988
989 if( have_D )
Gilles Peskine20edee72021-06-10 23:18:39 +0200990 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +0100991
992 if( have_E )
Gilles Peskine20edee72021-06-10 23:18:39 +0200993 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +0100994
995 TEST_ASSERT( mbedtls_rsa_import( &ctx,
996 strlen( input_N ) ? &N : NULL,
997 strlen( input_P ) ? &P : NULL,
998 strlen( input_Q ) ? &Q : NULL,
999 strlen( input_D ) ? &D : NULL,
1000 strlen( input_E ) ? &E : NULL ) == 0 );
1001
Hanno Becker7f25f852017-10-10 16:56:22 +01001002 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001003
1004 /*
1005 * Export parameters and compare to original ones.
1006 */
1007
1008 /* N and E must always be present. */
1009 if( !successive )
1010 {
1011 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1012 }
1013 else
1014 {
1015 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1016 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1017 }
1018 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1019 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1020
1021 /* If we were providing enough information to setup a complete private context,
1022 * we expect to be able to export all core parameters. */
1023
1024 if( is_priv )
1025 {
1026 if( !successive )
1027 {
1028 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1029 &De, NULL ) == 0 );
1030 }
1031 else
1032 {
1033 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1034 NULL, NULL ) == 0 );
1035 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1036 NULL, NULL ) == 0 );
1037 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1038 &De, NULL ) == 0 );
1039 }
1040
1041 if( have_P )
1042 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1043
1044 if( have_Q )
1045 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1046
1047 if( have_D )
1048 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1049
1050 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001051 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1052 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001053 }
1054
1055exit:
1056
1057 mbedtls_rsa_free( &ctx );
1058
1059 mbedtls_mpi_free( &N );
1060 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1061 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1062
1063 mbedtls_mpi_free( &Ne );
1064 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1065 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1066}
1067/* END_CASE */
1068
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001069/* BEGIN_CASE */
Hanno Becker750e8b42017-08-25 07:54:27 +01001070void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1071 int radix_P, char *input_P,
1072 int radix_Q, char *input_Q,
1073 int radix_D, char *input_D,
1074 int radix_E, char *input_E,
1075 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001076{
1077 /* Original MPI's with which we set up the RSA context */
1078 mbedtls_mpi N, P, Q, D, E;
1079
1080 const int have_N = ( strlen( input_N ) > 0 );
1081 const int have_P = ( strlen( input_P ) > 0 );
1082 const int have_Q = ( strlen( input_Q ) > 0 );
1083 const int have_D = ( strlen( input_D ) > 0 );
1084 const int have_E = ( strlen( input_E ) > 0 );
1085
Hanno Beckerce002632017-08-23 13:22:36 +01001086 mbedtls_mpi_init( &N );
1087 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1088 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1089
Hanno Beckerce002632017-08-23 13:22:36 +01001090 if( have_N )
Gilles Peskine20edee72021-06-10 23:18:39 +02001091 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001092
1093 if( have_P )
Gilles Peskine20edee72021-06-10 23:18:39 +02001094 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001095
1096 if( have_Q )
Gilles Peskine20edee72021-06-10 23:18:39 +02001097 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001098
1099 if( have_D )
Gilles Peskine20edee72021-06-10 23:18:39 +02001100 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001101
1102 if( have_E )
Gilles Peskine20edee72021-06-10 23:18:39 +02001103 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001104
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001105 /* This test uses an insecure RNG, suitable only for testing.
1106 * In production, always use a cryptographically strong RNG! */
Hanno Becker750e8b42017-08-25 07:54:27 +01001107 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1108 have_P ? &P : NULL,
1109 have_Q ? &Q : NULL,
1110 have_D ? &D : NULL,
1111 have_E ? &E : NULL,
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001112 prng ? mbedtls_test_rnd_std_rand : NULL,
1113 prng ? NULL : NULL ) == result );
1114
Hanno Beckerce002632017-08-23 13:22:36 +01001115exit:
Hanno Beckerce002632017-08-23 13:22:36 +01001116 mbedtls_mpi_free( &N );
1117 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1118 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1119}
1120/* END_CASE */
1121
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +02001122/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001123void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1124 data_t *input_Q, data_t *input_D,
1125 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001126 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001127{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001128 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001129 unsigned char bufNe[256];
1130 unsigned char bufPe[128];
1131 unsigned char bufQe[128];
1132 unsigned char bufDe[256];
1133 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001134
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001135 mbedtls_rsa_context ctx;
1136
Ronald Cronc1905a12021-06-05 11:11:14 +02001137 mbedtls_rsa_init( &ctx );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001138
1139 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001140 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001141 input_N->len ? input_N->x : NULL, input_N->len,
1142 input_P->len ? input_P->x : NULL, input_P->len,
1143 input_Q->len ? input_Q->x : NULL, input_Q->len,
1144 input_D->len ? input_D->x : NULL, input_D->len,
1145 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001146
Hanno Becker7f25f852017-10-10 16:56:22 +01001147 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001148
1149 /*
1150 * Export parameters and compare to original ones.
1151 */
1152
1153 /* N and E must always be present. */
1154 if( !successive )
1155 {
Azim Khand30ca132017-06-09 04:32:58 +01001156 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001157 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001158 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001159 }
1160 else
1161 {
Azim Khand30ca132017-06-09 04:32:58 +01001162 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001163 NULL, 0, NULL, 0, NULL, 0,
1164 NULL, 0 ) == 0 );
1165 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1166 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001167 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001168 }
Azim Khand30ca132017-06-09 04:32:58 +01001169 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1170 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001171
1172 /* If we were providing enough information to setup a complete private context,
1173 * we expect to be able to export all core parameters. */
1174
1175 if( is_priv )
1176 {
1177 if( !successive )
1178 {
1179 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001180 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1181 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1182 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001183 NULL, 0 ) == 0 );
1184 }
1185 else
1186 {
1187 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001188 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001189 NULL, 0, NULL, 0,
1190 NULL, 0 ) == 0 );
1191
1192 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001193 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001194 NULL, 0, NULL, 0 ) == 0 );
1195
Azim Khand30ca132017-06-09 04:32:58 +01001196 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1197 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001198 NULL, 0 ) == 0 );
1199 }
1200
Azim Khand30ca132017-06-09 04:32:58 +01001201 if( input_P->len )
1202 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001203
Azim Khand30ca132017-06-09 04:32:58 +01001204 if( input_Q->len )
1205 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001206
Azim Khand30ca132017-06-09 04:32:58 +01001207 if( input_D->len )
1208 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001209
1210 }
1211
1212exit:
1213 mbedtls_rsa_free( &ctx );
1214}
1215/* END_CASE */
1216
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001217/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001218void mbedtls_rsa_import_raw( data_t *input_N,
1219 data_t *input_P, data_t *input_Q,
1220 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001221 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001222 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001223 int res_check,
1224 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001225{
Hanno Beckere1582a82017-09-29 11:51:05 +01001226 /* Buffers used for encryption-decryption test */
1227 unsigned char *buf_orig = NULL;
1228 unsigned char *buf_enc = NULL;
1229 unsigned char *buf_dec = NULL;
1230
Hanno Beckerc77ab892017-08-23 11:01:06 +01001231 mbedtls_rsa_context ctx;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001232
Ronald Cronc1905a12021-06-05 11:11:14 +02001233 mbedtls_rsa_init( &ctx );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001234
Hanno Beckerc77ab892017-08-23 11:01:06 +01001235 if( !successive )
1236 {
1237 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001238 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1239 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1240 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1241 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1242 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001243 }
1244 else
1245 {
1246 /* Import N, P, Q, D, E separately.
1247 * This should make no functional difference. */
1248
1249 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001250 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001251 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1252
1253 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1254 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001255 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001256 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1257
1258 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1259 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001260 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001261 NULL, 0, NULL, 0 ) == 0 );
1262
1263 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1264 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001265 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001266 NULL, 0 ) == 0 );
1267
1268 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1269 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001270 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001271 }
1272
Hanno Becker04877a42017-10-11 10:01:33 +01001273 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001274
Hanno Beckere1582a82017-09-29 11:51:05 +01001275 /* On expected success, perform some public and private
1276 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001277 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001278 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001279 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001280 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1281 else
1282 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1283
1284 if( res_check != 0 )
1285 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001286
1287 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1288 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1289 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1290 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1291 goto exit;
1292
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001293 /* This test uses an insecure RNG, suitable only for testing.
1294 * In production, always use a cryptographically strong RNG! */
1295 TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL,
Hanno Beckere1582a82017-09-29 11:51:05 +01001296 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1297
1298 /* Make sure the number we're generating is smaller than the modulus */
1299 buf_orig[0] = 0x00;
1300
1301 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1302
1303 if( is_priv )
1304 {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001305 /* This test uses an insecure RNG, suitable only for testing.
1306 * In production, always use a cryptographically strong RNG! */
1307 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_std_rand,
1308 NULL, buf_enc,
Hanno Beckere1582a82017-09-29 11:51:05 +01001309 buf_dec ) == 0 );
1310
1311 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1312 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1313 }
1314 }
1315
Hanno Beckerc77ab892017-08-23 11:01:06 +01001316exit:
1317
Hanno Becker3f3ae852017-10-02 10:08:39 +01001318 mbedtls_free( buf_orig );
1319 mbedtls_free( buf_enc );
1320 mbedtls_free( buf_dec );
1321
Hanno Beckerc77ab892017-08-23 11:01:06 +01001322 mbedtls_rsa_free( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001323}
1324/* END_CASE */
1325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001327void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001328{
Andres AG93012e82016-09-09 09:10:28 +01001329 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001330}
Paul Bakker33b43f12013-08-20 11:48:36 +02001331/* END_CASE */