blob: 1aea673a5e7e598b32797fd162c4ab0442058252 [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;
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010019 const mbedtls_md_type_t md_alg_none = MBEDTLS_MD_NONE;
20 unsigned char buf[] = {0x00,0x01,0x02,0x03,0x04,0x05};
21 size_t buf_len = sizeof( buf );
Ronald Cronea7631b2021-06-03 18:51:59 +020022
Ronald Cronc1905a12021-06-05 11:11:14 +020023 mbedtls_rsa_init( &ctx );
Ronald Cronea7631b2021-06-03 18:51:59 +020024
25 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
26 invalid_padding,
27 MBEDTLS_MD_NONE ),
28 MBEDTLS_ERR_RSA_INVALID_PADDING );
29
30 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
31 MBEDTLS_RSA_PKCS_V21,
32 invalid_hash_id ),
33 MBEDTLS_ERR_RSA_INVALID_PADDING );
34
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010035 TEST_EQUAL( mbedtls_rsa_pkcs1_sign(&ctx, NULL,
36 NULL, md_alg_none,
37 buf_len,
38 NULL, buf),
39 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
40
41 TEST_EQUAL( mbedtls_rsa_pkcs1_verify(&ctx, md_alg_none,
42 buf_len,
43 NULL, buf),
44 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
45
Ronald Cron3a0375f2021-06-08 10:22:28 +020046#if !defined(MBEDTLS_PKCS1_V15)
47 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
48 MBEDTLS_RSA_PKCS_V15,
49 MBEDTLS_MD_NONE ),
50 MBEDTLS_ERR_RSA_INVALID_PADDING );
51#endif
52
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010053#if defined( MBEDTLS_PKCS1_V15 )
54 TEST_EQUAL( mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
55 NULL, md_alg_none,
56 buf_len,
57 NULL, buf),
58 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
59
60 TEST_EQUAL( mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, md_alg_none,
61 buf_len,
62 NULL, buf),
63 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
64
65#endif
66
Ronald Cron3a0375f2021-06-08 10:22:28 +020067#if !defined(MBEDTLS_PKCS1_V21)
68 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
69 MBEDTLS_RSA_PKCS_V21,
70 MBEDTLS_MD_NONE ),
71 MBEDTLS_ERR_RSA_INVALID_PADDING );
72#endif
73
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010074#if defined(MBEDTLS_PKCS1_V21)
75 TEST_EQUAL( mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
76 md_alg_none, buf_len,
77 NULL, buf_len,
78 buf ),
79 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
80
81 TEST_EQUAL( mbedtls_rsa_rsassa_pss_verify_ext(&ctx, md_alg_none,
82 buf_len, NULL,
83 md_alg_none,
84 buf_len, buf),
85 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
86
87 TEST_EQUAL( mbedtls_rsa_rsassa_pss_verify(&ctx, md_alg_none,
88 buf_len,
89 NULL, buf),
90 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
91
92#endif
93
Ronald Cronea7631b2021-06-03 18:51:59 +020094exit:
95 mbedtls_rsa_free( &ctx );
96}
97/* END_CASE */
98
99/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100100void rsa_init_free( int reinit )
101{
102 mbedtls_rsa_context ctx;
103
104 /* Double free is not explicitly documented to work, but we rely on it
105 * even inside the library so that you can call mbedtls_rsa_free()
106 * unconditionally on an error path without checking whether it has
107 * already been called in the success path. */
108
Ronald Cronc1905a12021-06-05 11:11:14 +0200109 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +0100110 mbedtls_rsa_free( &ctx );
111
112 if( reinit )
Ronald Cronc1905a12021-06-05 11:11:14 +0200113 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +0100114 mbedtls_rsa_free( &ctx );
115
116 /* This test case always succeeds, functionally speaking. A plausible
117 * bug might trigger an invalid pointer dereference or a memory leak. */
118 goto exit;
119}
120/* END_CASE */
121
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200122/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100123void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Werner Lewis9802d362022-07-07 11:37:24 +0100124 int digest, int mod, char * input_P,
Werner Lewisefda01f2022-07-06 13:03:36 +0100125 char * input_Q, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200126 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000127{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200128 unsigned char output[256];
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, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200131 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000132
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100133 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
134 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200135 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200136 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,padding_mode,
137 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000138
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200139 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200140 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000141
Werner Lewis19b4cd82022-07-07 11:02:27 +0100142 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
143 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
144 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
145 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000146
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100147 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
148 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100149 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000151
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200152 TEST_ASSERT( mbedtls_rsa_pkcs1_sign(
153 &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnarda4aa12f2022-07-16 08:20:26 +0200154 digest, message_str->len, message_str->x,
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200155 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200156 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000157 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000158
Ronald Cronac6ae352020-06-26 14:33:03 +0200159 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
160 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000161 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000162
Paul Bakkerbd51b262014-07-10 15:26:12 +0200163exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100164 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
165 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000167}
Paul Bakker33b43f12013-08-20 11:48:36 +0200168/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000169
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200170/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100171void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Werner Lewis9802d362022-07-07 11:37:24 +0100172 int digest, int mod,
173 char * input_N, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100174 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000175{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100177 mbedtls_mpi N, E;
178
179 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200180 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200181 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
182 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000183
Werner Lewis19b4cd82022-07-07 11:02:27 +0100184 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
185 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100186 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
187 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000189
Manuel Pégourié-Gonnarda4aa12f2022-07-16 08:20:26 +0200190 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 +0100191
Paul Bakkerbd51b262014-07-10 15:26:12 +0200192exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100193 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000195}
Paul Bakker33b43f12013-08-20 11:48:36 +0200196/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000197
Paul Bakker821fb082009-07-12 13:26:42 +0000198
Paul Bakker33b43f12013-08-20 11:48:36 +0200199/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100200void rsa_pkcs1_sign_raw( data_t * hash_result,
Werner Lewis9802d362022-07-07 11:37:24 +0100201 int padding_mode, int mod,
202 char * input_P, char * input_Q,
Werner Lewisefda01f2022-07-06 13:03:36 +0100203 char * input_N, char * input_E,
204 data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000205{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200206 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100208 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200209 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000210
Ronald Cronc1905a12021-06-05 11:11:14 +0200211 mbedtls_rsa_init( &ctx );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100212 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
213 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000214
Paul Elliotte57dd2d2021-06-25 11:13:24 +0100215 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
216 MBEDTLS_MD_NONE ) == 0 );
217
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200218 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200219 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000220
Werner Lewis19b4cd82022-07-07 11:02:27 +0100221 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
222 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
223 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
224 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000225
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100226 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
227 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100228 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000230
Paul Bakker821fb082009-07-12 13:26:42 +0000231
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200232 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100233 &rnd_info, MBEDTLS_MD_NONE,
234 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200235 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000236
Paul Bakker821fb082009-07-12 13:26:42 +0000237
Ronald Cronac6ae352020-06-26 14:33:03 +0200238 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
239 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000240
Paul Bakkerbd51b262014-07-10 15:26:12 +0200241exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100242 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
243 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000246}
Paul Bakker33b43f12013-08-20 11:48:36 +0200247/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000248
Paul Bakker33b43f12013-08-20 11:48:36 +0200249/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100250void rsa_pkcs1_verify_raw( data_t * hash_result,
Werner Lewis9802d362022-07-07 11:37:24 +0100251 int padding_mode, int mod,
252 char * input_N, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100253 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000254{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200255 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000257
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100258 mbedtls_mpi N, E;
259 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
260
Ronald Cronc1905a12021-06-05 11:11:14 +0200261 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200262 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
263 MBEDTLS_MD_NONE ) == 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100264 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000265
Werner Lewis19b4cd82022-07-07 11:02:27 +0100266 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
267 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000268
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100269 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
270 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000272
Paul Bakker821fb082009-07-12 13:26:42 +0000273
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100274 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 +0100275
Paul Bakkerbd51b262014-07-10 15:26:12 +0200276exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100277 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000279}
Paul Bakker33b43f12013-08-20 11:48:36 +0200280/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000281
Paul Bakker33b43f12013-08-20 11:48:36 +0200282/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100283void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Werner Lewisefda01f2022-07-06 13:03:36 +0100284 int mod, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200285 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +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;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200289 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000290
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100291 mbedtls_mpi N, E;
292 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
293
Ronald Cron351f0ee2020-06-10 12:12:18 +0200294 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000295
Ronald Cronc1905a12021-06-05 11:11:14 +0200296 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200297 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
298 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200299 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000300
Werner Lewis19b4cd82022-07-07 11:02:27 +0100301 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
302 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000303
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100304 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
305 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000307
Paul Bakker42a29bf2009-07-07 20:18:41 +0000308
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200309 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
310 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100311 &rnd_info, message_str->len,
312 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200313 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200314 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000315 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000316
Ronald Cronac6ae352020-06-26 14:33:03 +0200317 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
318 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000319 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100320
Paul Bakkerbd51b262014-07-10 15:26:12 +0200321exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100322 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000324}
Paul Bakker33b43f12013-08-20 11:48:36 +0200325/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000326
Paul Bakker33b43f12013-08-20 11:48:36 +0200327/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100328void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Werner Lewisefda01f2022-07-06 13:03:36 +0100329 int mod, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200330 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000331{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200332 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000334
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100335 mbedtls_mpi N, E;
336
337 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200338 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200339 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
340 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200341 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000342
Werner Lewis19b4cd82022-07-07 11:02:27 +0100343 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
344 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000345
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100346 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
347 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000349
Paul Bakkera6656852010-07-18 19:47:14 +0000350
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200351 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100352 NULL, message_str->len,
353 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200354 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200355 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000356 {
Paul Bakkera6656852010-07-18 19:47:14 +0000357
Ronald Cronac6ae352020-06-26 14:33:03 +0200358 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
359 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000360 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100361
Paul Bakkerbd51b262014-07-10 15:26:12 +0200362exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100363 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000365}
Paul Bakker33b43f12013-08-20 11:48:36 +0200366/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000367
Paul Bakker33b43f12013-08-20 11:48:36 +0200368/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100369void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Werner Lewis9802d362022-07-07 11:37:24 +0100370 int mod, char * input_P,
Werner Lewisefda01f2022-07-06 13:03:36 +0100371 char * input_Q, char * input_N,
372 char * input_E, int max_output,
373 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000374{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200375 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000377 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200378 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100379 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000380
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100381 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
382 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
383
Ronald Cronc1905a12021-06-05 11:11:14 +0200384 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200385 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
386 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000387
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200388 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200389 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000390
Paul Bakker42a29bf2009-07-07 20:18:41 +0000391
Werner Lewis19b4cd82022-07-07 11:02:27 +0100392 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
393 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
394 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
395 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000396
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100397 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
398 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100399 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000401
Paul Bakker69998dd2009-07-11 19:15:20 +0000402 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000403
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200404 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100405 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200406 &output_len, message_str->x, output,
407 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200408 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000409 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000410
Ronald Cronac6ae352020-06-26 14:33:03 +0200411 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200412 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200413 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000414 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000415
Paul Bakkerbd51b262014-07-10 15:26:12 +0200416exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100417 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
418 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000420}
Paul Bakker33b43f12013-08-20 11:48:36 +0200421/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000422
Paul Bakker33b43f12013-08-20 11:48:36 +0200423/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100424void mbedtls_rsa_public( data_t * message_str, int mod,
425 char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200426 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000427{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200428 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000430
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100431 mbedtls_mpi N, E;
432
433 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200434 mbedtls_rsa_init( &ctx );
435 mbedtls_rsa_init( &ctx2 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200436 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000437
Werner Lewis19b4cd82022-07-07 11:02:27 +0100438 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
439 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000440
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100441 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Gilles Peskine058d0092021-06-09 16:24:35 +0200442
443 /* Check test data consistency */
444 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100445 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000447
Azim Khand30ca132017-06-09 04:32:58 +0100448 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200449 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000450 {
Paul Bakker821fb082009-07-12 13:26:42 +0000451
Ronald Cronac6ae352020-06-26 14:33:03 +0200452 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
453 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000454 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100455
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100456 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200458 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100462
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200463 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100464 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100465 if( result == 0 )
466 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100467
Ronald Cronac6ae352020-06-26 14:33:03 +0200468 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
469 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100470 }
471
Paul Bakkerbd51b262014-07-10 15:26:12 +0200472exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100473 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_rsa_free( &ctx );
475 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000476}
Paul Bakker33b43f12013-08-20 11:48:36 +0200477/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000478
Paul Bakker33b43f12013-08-20 11:48:36 +0200479/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100480void mbedtls_rsa_private( data_t * message_str, int mod,
481 char * input_P, char * input_Q,
Werner Lewisefda01f2022-07-06 13:03:36 +0100482 char * input_N, char * input_E,
483 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000484{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200485 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100487 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200488 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200489 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000490
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100491 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
492 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200493 mbedtls_rsa_init( &ctx );
494 mbedtls_rsa_init( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000495
Ronald Cron351f0ee2020-06-10 12:12:18 +0200496 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000497
Werner Lewis19b4cd82022-07-07 11:02:27 +0100498 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
499 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
500 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
501 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000502
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100503 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
Gilles Peskine058d0092021-06-09 16:24:35 +0200504
505 /* Check test data consistency */
506 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100507 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100508 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000510
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200511 /* repeat three times to test updating of blinding values */
512 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000513 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200514 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200515 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
516 &rnd_info, message_str->x,
517 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200518 if( result == 0 )
519 {
Paul Bakker821fb082009-07-12 13:26:42 +0000520
Ronald Cronac6ae352020-06-26 14:33:03 +0200521 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200522 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200523 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200524 }
Paul Bakker821fb082009-07-12 13:26:42 +0000525 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000526
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100527 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200529 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100533
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200534 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200535 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
536 &rnd_info, message_str->x,
537 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100538 if( result == 0 )
539 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100540
Ronald Cronac6ae352020-06-26 14:33:03 +0200541 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200542 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200543 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100544 }
545
Paul Bakkerbd51b262014-07-10 15:26:12 +0200546exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100547 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
548 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000551}
Paul Bakker33b43f12013-08-20 11:48:36 +0200552/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000553
Paul Bakker33b43f12013-08-20 11:48:36 +0200554/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100555void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000556{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_rsa_context ctx;
558 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000561}
Paul Bakker33b43f12013-08-20 11:48:36 +0200562/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000563
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100565void mbedtls_rsa_check_pubkey( char * input_N, char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100568 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000569
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100570 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200571 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000572
Paul Bakker33b43f12013-08-20 11:48:36 +0200573 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000574 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100575 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000576 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200577 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000578 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100579 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000580 }
581
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100582 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100584
Paul Bakkerbd51b262014-07-10 15:26:12 +0200585exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100586 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000588}
Paul Bakker33b43f12013-08-20 11:48:36 +0200589/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000590
Paul Bakker33b43f12013-08-20 11:48:36 +0200591/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100592void mbedtls_rsa_check_privkey( int mod, char * input_P, char * input_Q,
593 char * input_N, char * input_E, char * input_D,
594 char * input_DP, char * input_DQ, char * input_QP,
595 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000596{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000598
Ronald Cronc1905a12021-06-05 11:11:14 +0200599 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000600
Paul Bakker33b43f12013-08-20 11:48:36 +0200601 ctx.len = mod / 8;
602 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000603 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100604 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000605 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200606 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000607 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100608 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000609 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200610 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000611 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100612 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000613 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200614 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000615 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100616 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000617 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200618 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000619 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100620 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000621 }
Hanno Becker131134f2017-08-23 08:31:07 +0100622#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200623 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000624 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100625 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000626 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200627 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000628 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100629 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000630 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200631 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000632 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100633 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000634 }
Hanno Becker131134f2017-08-23 08:31:07 +0100635#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100636 ((void) input_DP);
637 ((void) input_DQ);
638 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100639#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100642
Paul Bakkerbd51b262014-07-10 15:26:12 +0200643exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000645}
Paul Bakker33b43f12013-08-20 11:48:36 +0200646/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000647
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100648/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100649void rsa_check_pubpriv( int mod, char * input_Npub, char * input_Epub,
650 char * input_P, char * input_Q, char * input_N,
651 char * input_E, char * input_D, char * input_DP,
652 char * input_DQ, char * input_QP, int result )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100653{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100655
Ronald Cronc1905a12021-06-05 11:11:14 +0200656 mbedtls_rsa_init( &pub );
657 mbedtls_rsa_init( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100658
659 pub.len = mod / 8;
660 prv.len = mod / 8;
661
662 if( strlen( input_Npub ) )
663 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100664 TEST_ASSERT( mbedtls_test_read_mpi( &pub.N, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100665 }
666 if( strlen( input_Epub ) )
667 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100668 TEST_ASSERT( mbedtls_test_read_mpi( &pub.E, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100669 }
670
671 if( strlen( input_P ) )
672 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100673 TEST_ASSERT( mbedtls_test_read_mpi( &prv.P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100674 }
675 if( strlen( input_Q ) )
676 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100677 TEST_ASSERT( mbedtls_test_read_mpi( &prv.Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100678 }
679 if( strlen( input_N ) )
680 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100681 TEST_ASSERT( mbedtls_test_read_mpi( &prv.N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100682 }
683 if( strlen( input_E ) )
684 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100685 TEST_ASSERT( mbedtls_test_read_mpi( &prv.E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100686 }
687 if( strlen( input_D ) )
688 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100689 TEST_ASSERT( mbedtls_test_read_mpi( &prv.D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100690 }
Hanno Becker131134f2017-08-23 08:31:07 +0100691#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100692 if( strlen( input_DP ) )
693 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100694 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100695 }
696 if( strlen( input_DQ ) )
697 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100698 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100699 }
700 if( strlen( input_QP ) )
701 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100702 TEST_ASSERT( mbedtls_test_read_mpi( &prv.QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100703 }
Hanno Becker131134f2017-08-23 08:31:07 +0100704#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100705 ((void) input_DP);
706 ((void) input_DQ);
707 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100708#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100711
712exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_rsa_free( &pub );
714 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100715}
716/* END_CASE */
717
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200718/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000720{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_rsa_context ctx;
Ronald Cronc1905a12021-06-05 11:11:14 +0200722 mbedtls_rsa_init ( &ctx );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000723
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200724 /* This test uses an insecure RNG, suitable only for testing.
725 * In production, always use a cryptographically strong RNG! */
726 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_test_rnd_std_rand, NULL, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200727 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100730 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000731 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100732
Paul Bakkerbd51b262014-07-10 15:26:12 +0200733exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000735}
Paul Bakker33b43f12013-08-20 11:48:36 +0200736/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000737
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +0200738/* BEGIN_CASE */
Werner Lewis3ccc1162022-08-01 15:11:48 +0100739void mbedtls_rsa_deduce_primes( char *input_N,
740 char *input_D,
741 char *input_E,
742 char *output_P,
743 char *output_Q,
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100744 int corrupt, int result )
745{
746 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
747
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100748 mbedtls_mpi_init( &N );
749 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
750 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
751 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
752
Werner Lewis19b4cd82022-07-07 11:02:27 +0100753 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
754 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
755 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
756 TEST_ASSERT( mbedtls_test_read_mpi( &Qp, output_P ) == 0 );
757 TEST_ASSERT( mbedtls_test_read_mpi( &Pp, output_Q ) == 0 );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100758
759 if( corrupt )
760 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
761
762 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100763 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100764
765 if( !corrupt )
766 {
767 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
768 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
769 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
770 }
771
772exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100773 mbedtls_mpi_free( &N );
774 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
775 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
776 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100777}
778/* END_CASE */
779
Hanno Becker6b4ce492017-08-23 11:00:21 +0100780/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100781void mbedtls_rsa_deduce_private_exponent( char *input_P,
782 char *input_Q,
783 char *input_E,
784 char *output_D,
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100785 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +0100786{
787 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
788
789 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
790 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
791 mbedtls_mpi_init( &E );
792 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
793
Werner Lewis19b4cd82022-07-07 11:02:27 +0100794 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
795 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
796 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
797 TEST_ASSERT( mbedtls_test_read_mpi( &Dp, output_D ) == 0 );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100798
799 if( corrupt )
800 {
801 /* Make E even */
802 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
803 }
804
805 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100806 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
807 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100808
809 if( !corrupt )
810 {
811 /*
812 * Check that D and Dp agree modulo LCM(P-1, Q-1).
813 */
814
815 /* Replace P,Q by P-1, Q-1 */
816 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
817 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
818
819 /* Check D == Dp modulo P-1 */
820 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
821 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
822 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
823
824 /* Check D == Dp modulo Q-1 */
825 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
826 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
827 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
828 }
829
830exit:
831
832 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
833 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
834 mbedtls_mpi_free( &E );
835 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
836}
837/* END_CASE */
838
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200839/* BEGIN_CASE */
Werner Lewis3ccc1162022-08-01 15:11:48 +0100840void mbedtls_rsa_import( char *input_N,
841 char *input_P,
842 char *input_Q,
843 char *input_D,
844 char *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100845 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +0100846 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +0100847 int res_check,
848 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100849{
850 mbedtls_mpi N, P, Q, D, E;
851 mbedtls_rsa_context ctx;
852
Hanno Beckere1582a82017-09-29 11:51:05 +0100853 /* Buffers used for encryption-decryption test */
854 unsigned char *buf_orig = NULL;
855 unsigned char *buf_enc = NULL;
856 unsigned char *buf_dec = NULL;
857
Hanno Becker4d6e8342017-09-29 11:50:18 +0100858 const int have_N = ( strlen( input_N ) > 0 );
859 const int have_P = ( strlen( input_P ) > 0 );
860 const int have_Q = ( strlen( input_Q ) > 0 );
861 const int have_D = ( strlen( input_D ) > 0 );
862 const int have_E = ( strlen( input_E ) > 0 );
863
Ronald Cronc1905a12021-06-05 11:11:14 +0200864 mbedtls_rsa_init( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100865
866 mbedtls_mpi_init( &N );
867 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
868 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
869
Hanno Becker4d6e8342017-09-29 11:50:18 +0100870 if( have_N )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100871 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100872
Hanno Becker4d6e8342017-09-29 11:50:18 +0100873 if( have_P )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100874 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100875
Hanno Becker4d6e8342017-09-29 11:50:18 +0100876 if( have_Q )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100877 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100878
Hanno Becker4d6e8342017-09-29 11:50:18 +0100879 if( have_D )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100880 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100881
Hanno Becker4d6e8342017-09-29 11:50:18 +0100882 if( have_E )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100883 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100884
885 if( !successive )
886 {
887 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100888 have_N ? &N : NULL,
889 have_P ? &P : NULL,
890 have_Q ? &Q : NULL,
891 have_D ? &D : NULL,
892 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100893 }
894 else
895 {
896 /* Import N, P, Q, D, E separately.
897 * This should make no functional difference. */
898
899 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100900 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100901 NULL, NULL, NULL, NULL ) == 0 );
902
903 TEST_ASSERT( mbedtls_rsa_import( &ctx,
904 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100905 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100906 NULL, NULL, NULL ) == 0 );
907
908 TEST_ASSERT( mbedtls_rsa_import( &ctx,
909 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100910 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100911 NULL, NULL ) == 0 );
912
913 TEST_ASSERT( mbedtls_rsa_import( &ctx,
914 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100915 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100916 NULL ) == 0 );
917
918 TEST_ASSERT( mbedtls_rsa_import( &ctx,
919 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100920 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100921 }
922
Hanno Becker04877a42017-10-11 10:01:33 +0100923 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100924
Hanno Beckere1582a82017-09-29 11:51:05 +0100925 /* On expected success, perform some public and private
926 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +0100927 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +0100928 {
Hanno Beckere1582a82017-09-29 11:51:05 +0100929 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +0100930 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
931 else
932 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
933
934 if( res_check != 0 )
935 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +0100936
937 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
938 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
939 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
940 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
941 goto exit;
942
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200943 /* This test uses an insecure RNG, suitable only for testing.
944 * In production, always use a cryptographically strong RNG! */
945 TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL,
Hanno Beckere1582a82017-09-29 11:51:05 +0100946 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
947
948 /* Make sure the number we're generating is smaller than the modulus */
949 buf_orig[0] = 0x00;
950
951 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
952
953 if( is_priv )
954 {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200955 /* This test uses an insecure RNG, suitable only for testing.
956 * In production, always use a cryptographically strong RNG! */
957 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_std_rand,
958 NULL, buf_enc,
Hanno Beckere1582a82017-09-29 11:51:05 +0100959 buf_dec ) == 0 );
960
961 TEST_ASSERT( memcmp( buf_orig, buf_dec,
962 mbedtls_rsa_get_len( &ctx ) ) == 0 );
963 }
964 }
965
Hanno Beckerc77ab892017-08-23 11:01:06 +0100966exit:
967
Hanno Beckere1582a82017-09-29 11:51:05 +0100968 mbedtls_free( buf_orig );
969 mbedtls_free( buf_enc );
970 mbedtls_free( buf_dec );
971
Hanno Beckerc77ab892017-08-23 11:01:06 +0100972 mbedtls_rsa_free( &ctx );
973
Hanno Beckerc77ab892017-08-23 11:01:06 +0100974 mbedtls_mpi_free( &N );
975 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
976 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
977}
978/* END_CASE */
979
Hanno Becker417f2d62017-08-23 11:44:51 +0100980/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100981void mbedtls_rsa_export( char *input_N,
982 char *input_P,
983 char *input_Q,
984 char *input_D,
985 char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +0100986 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +0100987 int successive )
988{
989 /* Original MPI's with which we set up the RSA context */
990 mbedtls_mpi N, P, Q, D, E;
991
992 /* Exported MPI's */
993 mbedtls_mpi Ne, Pe, Qe, De, Ee;
994
995 const int have_N = ( strlen( input_N ) > 0 );
996 const int have_P = ( strlen( input_P ) > 0 );
997 const int have_Q = ( strlen( input_Q ) > 0 );
998 const int have_D = ( strlen( input_D ) > 0 );
999 const int have_E = ( strlen( input_E ) > 0 );
1000
Hanno Becker417f2d62017-08-23 11:44:51 +01001001 mbedtls_rsa_context ctx;
1002
Ronald Cronc1905a12021-06-05 11:11:14 +02001003 mbedtls_rsa_init( &ctx );
Hanno Becker417f2d62017-08-23 11:44:51 +01001004
1005 mbedtls_mpi_init( &N );
1006 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1007 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1008
1009 mbedtls_mpi_init( &Ne );
1010 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1011 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1012
1013 /* Setup RSA context */
1014
1015 if( have_N )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001016 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001017
1018 if( have_P )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001019 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001020
1021 if( have_Q )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001022 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001023
1024 if( have_D )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001025 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001026
1027 if( have_E )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001028 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001029
1030 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1031 strlen( input_N ) ? &N : NULL,
1032 strlen( input_P ) ? &P : NULL,
1033 strlen( input_Q ) ? &Q : NULL,
1034 strlen( input_D ) ? &D : NULL,
1035 strlen( input_E ) ? &E : NULL ) == 0 );
1036
Hanno Becker7f25f852017-10-10 16:56:22 +01001037 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001038
1039 /*
1040 * Export parameters and compare to original ones.
1041 */
1042
1043 /* N and E must always be present. */
1044 if( !successive )
1045 {
1046 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1047 }
1048 else
1049 {
1050 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1051 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1052 }
1053 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1054 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1055
1056 /* If we were providing enough information to setup a complete private context,
1057 * we expect to be able to export all core parameters. */
1058
1059 if( is_priv )
1060 {
1061 if( !successive )
1062 {
1063 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1064 &De, NULL ) == 0 );
1065 }
1066 else
1067 {
1068 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1069 NULL, NULL ) == 0 );
1070 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1071 NULL, NULL ) == 0 );
1072 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1073 &De, NULL ) == 0 );
1074 }
1075
1076 if( have_P )
1077 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1078
1079 if( have_Q )
1080 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1081
1082 if( have_D )
1083 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1084
1085 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001086 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1087 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001088 }
1089
1090exit:
1091
1092 mbedtls_rsa_free( &ctx );
1093
1094 mbedtls_mpi_free( &N );
1095 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1096 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1097
1098 mbedtls_mpi_free( &Ne );
1099 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1100 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1101}
1102/* END_CASE */
1103
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001104/* BEGIN_CASE */
Werner Lewis3ccc1162022-08-01 15:11:48 +01001105void mbedtls_rsa_validate_params( char *input_N,
1106 char *input_P,
1107 char *input_Q,
1108 char *input_D,
1109 char *input_E,
Hanno Becker750e8b42017-08-25 07:54:27 +01001110 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001111{
1112 /* Original MPI's with which we set up the RSA context */
1113 mbedtls_mpi N, P, Q, D, E;
1114
1115 const int have_N = ( strlen( input_N ) > 0 );
1116 const int have_P = ( strlen( input_P ) > 0 );
1117 const int have_Q = ( strlen( input_Q ) > 0 );
1118 const int have_D = ( strlen( input_D ) > 0 );
1119 const int have_E = ( strlen( input_E ) > 0 );
1120
Hanno Beckerce002632017-08-23 13:22:36 +01001121 mbedtls_mpi_init( &N );
1122 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1123 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1124
Hanno Beckerce002632017-08-23 13:22:36 +01001125 if( have_N )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001126 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001127
1128 if( have_P )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001129 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001130
1131 if( have_Q )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001132 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001133
1134 if( have_D )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001135 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001136
1137 if( have_E )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001138 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001139
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001140 /* This test uses an insecure RNG, suitable only for testing.
1141 * In production, always use a cryptographically strong RNG! */
Hanno Becker750e8b42017-08-25 07:54:27 +01001142 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1143 have_P ? &P : NULL,
1144 have_Q ? &Q : NULL,
1145 have_D ? &D : NULL,
1146 have_E ? &E : NULL,
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001147 prng ? mbedtls_test_rnd_std_rand : NULL,
1148 prng ? NULL : NULL ) == result );
1149
Hanno Beckerce002632017-08-23 13:22:36 +01001150exit:
Hanno Beckerce002632017-08-23 13:22:36 +01001151 mbedtls_mpi_free( &N );
1152 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1153 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1154}
1155/* END_CASE */
1156
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +02001157/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001158void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1159 data_t *input_Q, data_t *input_D,
1160 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001161 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001162{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001163 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001164 unsigned char bufNe[256];
1165 unsigned char bufPe[128];
1166 unsigned char bufQe[128];
1167 unsigned char bufDe[256];
1168 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001169
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001170 mbedtls_rsa_context ctx;
1171
Ronald Cronc1905a12021-06-05 11:11:14 +02001172 mbedtls_rsa_init( &ctx );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001173
1174 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001175 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001176 input_N->len ? input_N->x : NULL, input_N->len,
1177 input_P->len ? input_P->x : NULL, input_P->len,
1178 input_Q->len ? input_Q->x : NULL, input_Q->len,
1179 input_D->len ? input_D->x : NULL, input_D->len,
1180 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001181
Hanno Becker7f25f852017-10-10 16:56:22 +01001182 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001183
1184 /*
1185 * Export parameters and compare to original ones.
1186 */
1187
1188 /* N and E must always be present. */
1189 if( !successive )
1190 {
Azim Khand30ca132017-06-09 04:32:58 +01001191 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001192 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001193 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001194 }
1195 else
1196 {
Azim Khand30ca132017-06-09 04:32:58 +01001197 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001198 NULL, 0, NULL, 0, NULL, 0,
1199 NULL, 0 ) == 0 );
1200 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1201 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001202 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001203 }
Azim Khand30ca132017-06-09 04:32:58 +01001204 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1205 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001206
1207 /* If we were providing enough information to setup a complete private context,
1208 * we expect to be able to export all core parameters. */
1209
1210 if( is_priv )
1211 {
1212 if( !successive )
1213 {
1214 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001215 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1216 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1217 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001218 NULL, 0 ) == 0 );
1219 }
1220 else
1221 {
1222 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001223 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001224 NULL, 0, NULL, 0,
1225 NULL, 0 ) == 0 );
1226
1227 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001228 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001229 NULL, 0, NULL, 0 ) == 0 );
1230
Azim Khand30ca132017-06-09 04:32:58 +01001231 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1232 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001233 NULL, 0 ) == 0 );
1234 }
1235
Azim Khand30ca132017-06-09 04:32:58 +01001236 if( input_P->len )
1237 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001238
Azim Khand30ca132017-06-09 04:32:58 +01001239 if( input_Q->len )
1240 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001241
Azim Khand30ca132017-06-09 04:32:58 +01001242 if( input_D->len )
1243 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001244
1245 }
1246
1247exit:
1248 mbedtls_rsa_free( &ctx );
1249}
1250/* END_CASE */
1251
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001252/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001253void mbedtls_rsa_import_raw( data_t *input_N,
1254 data_t *input_P, data_t *input_Q,
1255 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001256 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001257 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001258 int res_check,
1259 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001260{
Hanno Beckere1582a82017-09-29 11:51:05 +01001261 /* Buffers used for encryption-decryption test */
1262 unsigned char *buf_orig = NULL;
1263 unsigned char *buf_enc = NULL;
1264 unsigned char *buf_dec = NULL;
1265
Hanno Beckerc77ab892017-08-23 11:01:06 +01001266 mbedtls_rsa_context ctx;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001267
Ronald Cronc1905a12021-06-05 11:11:14 +02001268 mbedtls_rsa_init( &ctx );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001269
Hanno Beckerc77ab892017-08-23 11:01:06 +01001270 if( !successive )
1271 {
1272 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001273 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1274 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1275 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1276 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1277 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001278 }
1279 else
1280 {
1281 /* Import N, P, Q, D, E separately.
1282 * This should make no functional difference. */
1283
1284 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001285 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001286 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1287
1288 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1289 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001290 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001291 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1292
1293 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1294 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001295 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001296 NULL, 0, NULL, 0 ) == 0 );
1297
1298 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1299 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001300 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001301 NULL, 0 ) == 0 );
1302
1303 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1304 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001305 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001306 }
1307
Hanno Becker04877a42017-10-11 10:01:33 +01001308 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001309
Hanno Beckere1582a82017-09-29 11:51:05 +01001310 /* On expected success, perform some public and private
1311 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001312 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001313 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001314 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001315 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1316 else
1317 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1318
1319 if( res_check != 0 )
1320 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001321
1322 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1323 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1324 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1325 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1326 goto exit;
1327
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001328 /* This test uses an insecure RNG, suitable only for testing.
1329 * In production, always use a cryptographically strong RNG! */
1330 TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL,
Hanno Beckere1582a82017-09-29 11:51:05 +01001331 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1332
1333 /* Make sure the number we're generating is smaller than the modulus */
1334 buf_orig[0] = 0x00;
1335
1336 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1337
1338 if( is_priv )
1339 {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001340 /* This test uses an insecure RNG, suitable only for testing.
1341 * In production, always use a cryptographically strong RNG! */
1342 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_std_rand,
1343 NULL, buf_enc,
Hanno Beckere1582a82017-09-29 11:51:05 +01001344 buf_dec ) == 0 );
1345
1346 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1347 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1348 }
1349 }
1350
Hanno Beckerc77ab892017-08-23 11:01:06 +01001351exit:
1352
Hanno Becker3f3ae852017-10-02 10:08:39 +01001353 mbedtls_free( buf_orig );
1354 mbedtls_free( buf_enc );
1355 mbedtls_free( buf_dec );
1356
Hanno Beckerc77ab892017-08-23 11:01:06 +01001357 mbedtls_rsa_free( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001358}
1359/* END_CASE */
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001362void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001363{
Andres AG93012e82016-09-09 09:10:28 +01001364 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001365}
Paul Bakker33b43f12013-08-20 11:48:36 +02001366/* END_CASE */