blob: ad52e98cd4e81d39e3b429eeacc9d209470c3a7b [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,
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010036 NULL, MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010037 buf_len,
38 NULL, buf),
39 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
40
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010041 TEST_EQUAL( mbedtls_rsa_pkcs1_sign(&ctx, NULL,
42 NULL, MBEDTLS_MD_SHA256,
43 0,
44 NULL, buf),
45 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
46
47 TEST_EQUAL( mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010048 buf_len,
49 NULL, buf),
50 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
51
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010052 TEST_EQUAL( mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_SHA256,
53 0,
54 NULL, buf),
55 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
56
Ronald Cron3a0375f2021-06-08 10:22:28 +020057#if !defined(MBEDTLS_PKCS1_V15)
58 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
59 MBEDTLS_RSA_PKCS_V15,
60 MBEDTLS_MD_NONE ),
61 MBEDTLS_ERR_RSA_INVALID_PADDING );
62#endif
63
Tuvshinzaya Erdenekhuufe7524d2022-09-01 16:07:18 +010064#if defined(MBEDTLS_PKCS1_V15)
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010065 TEST_EQUAL( mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010066 NULL, MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010067 buf_len,
68 NULL, buf),
69 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
70
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010071 TEST_EQUAL( mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
72 NULL, MBEDTLS_MD_SHA256,
73 0,
74 NULL, buf),
75 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
76
77 TEST_EQUAL( mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010078 buf_len,
79 NULL, buf),
80 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
81
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010082 TEST_EQUAL( mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_SHA256,
83 0,
84 NULL, buf),
85 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
86
87
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010088#endif
89
Ronald Cron3a0375f2021-06-08 10:22:28 +020090#if !defined(MBEDTLS_PKCS1_V21)
91 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
92 MBEDTLS_RSA_PKCS_V21,
93 MBEDTLS_MD_NONE ),
94 MBEDTLS_ERR_RSA_INVALID_PADDING );
95#endif
96
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010097#if defined(MBEDTLS_PKCS1_V21)
98 TEST_EQUAL( mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010099 MBEDTLS_MD_NONE, buf_len,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100100 NULL, buf_len,
101 buf ),
102 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
103
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100104 TEST_EQUAL( mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
105 MBEDTLS_MD_SHA256, 0,
106 NULL, buf_len,
107 buf ),
108 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
109
110 TEST_EQUAL( mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100111 buf_len, NULL,
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100112 MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100113 buf_len, buf),
114 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
115
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100116 TEST_EQUAL( mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_SHA256,
117 0, NULL,
118 MBEDTLS_MD_NONE,
119 buf_len, buf),
120 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
121
122 TEST_EQUAL( mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100123 buf_len,
124 NULL, buf),
125 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
126
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100127 TEST_EQUAL( mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_SHA256,
128 0,
129 NULL, buf),
130 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100131#endif
132
Ronald Cronea7631b2021-06-03 18:51:59 +0200133exit:
134 mbedtls_rsa_free( &ctx );
135}
136/* END_CASE */
137
138/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100139void rsa_init_free( int reinit )
140{
141 mbedtls_rsa_context ctx;
142
143 /* Double free is not explicitly documented to work, but we rely on it
144 * even inside the library so that you can call mbedtls_rsa_free()
145 * unconditionally on an error path without checking whether it has
146 * already been called in the success path. */
147
Ronald Cronc1905a12021-06-05 11:11:14 +0200148 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +0100149 mbedtls_rsa_free( &ctx );
150
151 if( reinit )
Ronald Cronc1905a12021-06-05 11:11:14 +0200152 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +0100153 mbedtls_rsa_free( &ctx );
154
155 /* This test case always succeeds, functionally speaking. A plausible
156 * bug might trigger an invalid pointer dereference or a memory leak. */
157 goto exit;
158}
159/* END_CASE */
160
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200161/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100162void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Werner Lewis9802d362022-07-07 11:37:24 +0100163 int digest, int mod, char * input_P,
Werner Lewisefda01f2022-07-06 13:03:36 +0100164 char * input_Q, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200165 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000166{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200167 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100169 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200170 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000171
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100172 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
173 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200174 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200175 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,padding_mode,
176 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000177
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200178 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200179 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000180
Werner Lewis19b4cd82022-07-07 11:02:27 +0100181 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
182 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
183 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
184 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000185
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100186 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
187 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100188 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000190
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200191 TEST_ASSERT( mbedtls_rsa_pkcs1_sign(
192 &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnarda4aa12f2022-07-16 08:20:26 +0200193 digest, message_str->len, message_str->x,
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200194 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200195 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000196 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000197
Ronald Cronac6ae352020-06-26 14:33:03 +0200198 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
199 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000200 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000201
Paul Bakkerbd51b262014-07-10 15:26:12 +0200202exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100203 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
204 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000206}
Paul Bakker33b43f12013-08-20 11:48:36 +0200207/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000208
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200209/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100210void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Werner Lewis9802d362022-07-07 11:37:24 +0100211 int digest, int mod,
212 char * input_N, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100213 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000214{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100216 mbedtls_mpi N, E;
217
218 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200219 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200220 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
221 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000222
Werner Lewis19b4cd82022-07-07 11:02:27 +0100223 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
224 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100225 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
226 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000228
Manuel Pégourié-Gonnarda4aa12f2022-07-16 08:20:26 +0200229 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 +0100230
Paul Bakkerbd51b262014-07-10 15:26:12 +0200231exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100232 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000234}
Paul Bakker33b43f12013-08-20 11:48:36 +0200235/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000236
Paul Bakker821fb082009-07-12 13:26:42 +0000237
Paul Bakker33b43f12013-08-20 11:48:36 +0200238/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100239void rsa_pkcs1_sign_raw( data_t * hash_result,
Werner Lewis9802d362022-07-07 11:37:24 +0100240 int padding_mode, int mod,
241 char * input_P, char * input_Q,
Werner Lewisefda01f2022-07-06 13:03:36 +0100242 char * input_N, char * input_E,
243 data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000244{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200245 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100247 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200248 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000249
Ronald Cronc1905a12021-06-05 11:11:14 +0200250 mbedtls_rsa_init( &ctx );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100251 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
252 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000253
Paul Elliotte57dd2d2021-06-25 11:13:24 +0100254 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
255 MBEDTLS_MD_NONE ) == 0 );
256
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200257 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200258 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000259
Werner Lewis19b4cd82022-07-07 11:02:27 +0100260 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
261 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
262 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
263 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000264
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100265 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
266 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100267 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000269
Paul Bakker821fb082009-07-12 13:26:42 +0000270
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200271 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100272 &rnd_info, MBEDTLS_MD_NONE,
273 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200274 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000275
Paul Bakker821fb082009-07-12 13:26:42 +0000276
Ronald Cronac6ae352020-06-26 14:33:03 +0200277 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
278 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000279
Paul Bakkerbd51b262014-07-10 15:26:12 +0200280exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100281 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
282 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000285}
Paul Bakker33b43f12013-08-20 11:48:36 +0200286/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000287
Paul Bakker33b43f12013-08-20 11:48:36 +0200288/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100289void rsa_pkcs1_verify_raw( data_t * hash_result,
Werner Lewis9802d362022-07-07 11:37:24 +0100290 int padding_mode, int mod,
291 char * input_N, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100292 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000293{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200294 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000296
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100297 mbedtls_mpi N, E;
298 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
299
Ronald Cronc1905a12021-06-05 11:11:14 +0200300 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200301 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
302 MBEDTLS_MD_NONE ) == 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100303 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000304
Werner Lewis19b4cd82022-07-07 11:02:27 +0100305 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
306 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000307
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100308 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
309 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000311
Paul Bakker821fb082009-07-12 13:26:42 +0000312
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100313 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 +0100314
Paul Bakkerbd51b262014-07-10 15:26:12 +0200315exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100316 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000318}
Paul Bakker33b43f12013-08-20 11:48:36 +0200319/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000320
Paul Bakker33b43f12013-08-20 11:48:36 +0200321/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100322void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Werner Lewisefda01f2022-07-06 13:03:36 +0100323 int mod, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200324 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000325{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200326 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200328 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000329
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100330 mbedtls_mpi N, E;
331 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
332
Ronald Cron351f0ee2020-06-10 12:12:18 +0200333 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000334
Ronald Cronc1905a12021-06-05 11:11:14 +0200335 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200336 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
337 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200338 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000339
Werner Lewis19b4cd82022-07-07 11:02:27 +0100340 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
341 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000342
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100343 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
344 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000346
Paul Bakker42a29bf2009-07-07 20:18:41 +0000347
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200348 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
349 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100350 &rnd_info, message_str->len,
351 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200352 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200353 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000354 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000355
Ronald Cronac6ae352020-06-26 14:33:03 +0200356 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
357 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000358 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100359
Paul Bakkerbd51b262014-07-10 15:26:12 +0200360exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100361 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000363}
Paul Bakker33b43f12013-08-20 11:48:36 +0200364/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000365
Paul Bakker33b43f12013-08-20 11:48:36 +0200366/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100367void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Werner Lewisefda01f2022-07-06 13:03:36 +0100368 int mod, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200369 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000370{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200371 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000373
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100374 mbedtls_mpi N, E;
375
376 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200377 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200378 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
379 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200380 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000381
Werner Lewis19b4cd82022-07-07 11:02:27 +0100382 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
383 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000384
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100385 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
386 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000388
Paul Bakkera6656852010-07-18 19:47:14 +0000389
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200390 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100391 NULL, message_str->len,
392 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200393 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200394 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000395 {
Paul Bakkera6656852010-07-18 19:47:14 +0000396
Ronald Cronac6ae352020-06-26 14:33:03 +0200397 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
398 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000399 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100400
Paul Bakkerbd51b262014-07-10 15:26:12 +0200401exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100402 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000404}
Paul Bakker33b43f12013-08-20 11:48:36 +0200405/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000406
Paul Bakker33b43f12013-08-20 11:48:36 +0200407/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100408void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Werner Lewis9802d362022-07-07 11:37:24 +0100409 int mod, char * input_P,
Werner Lewisefda01f2022-07-06 13:03:36 +0100410 char * input_Q, char * input_N,
411 char * input_E, int max_output,
412 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000413{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200414 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000416 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200417 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100418 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000419
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100420 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
421 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
422
Ronald Cronc1905a12021-06-05 11:11:14 +0200423 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200424 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
425 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000426
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200427 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200428 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000429
Paul Bakker42a29bf2009-07-07 20:18:41 +0000430
Werner Lewis19b4cd82022-07-07 11:02:27 +0100431 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
432 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
433 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
434 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000435
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100436 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
437 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100438 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000440
Paul Bakker69998dd2009-07-11 19:15:20 +0000441 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000442
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200443 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100444 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200445 &output_len, message_str->x, output,
446 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200447 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000448 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000449
Ronald Cronac6ae352020-06-26 14:33:03 +0200450 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200451 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200452 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000453 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000454
Paul Bakkerbd51b262014-07-10 15:26:12 +0200455exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100456 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
457 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000459}
Paul Bakker33b43f12013-08-20 11:48:36 +0200460/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000461
Paul Bakker33b43f12013-08-20 11:48:36 +0200462/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100463void mbedtls_rsa_public( data_t * message_str, int mod,
464 char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200465 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000466{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200467 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000469
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100470 mbedtls_mpi N, E;
471
472 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200473 mbedtls_rsa_init( &ctx );
474 mbedtls_rsa_init( &ctx2 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200475 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000476
Werner Lewis19b4cd82022-07-07 11:02:27 +0100477 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
478 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000479
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100480 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Gilles Peskine058d0092021-06-09 16:24:35 +0200481
482 /* Check test data consistency */
483 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100484 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000486
Azim Khand30ca132017-06-09 04:32:58 +0100487 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200488 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000489 {
Paul Bakker821fb082009-07-12 13:26:42 +0000490
Ronald Cronac6ae352020-06-26 14:33:03 +0200491 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
492 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000493 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100494
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100495 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200497 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100501
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200502 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100503 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100504 if( result == 0 )
505 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100506
Ronald Cronac6ae352020-06-26 14:33:03 +0200507 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
508 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100509 }
510
Paul Bakkerbd51b262014-07-10 15:26:12 +0200511exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100512 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_rsa_free( &ctx );
514 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000515}
Paul Bakker33b43f12013-08-20 11:48:36 +0200516/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000517
Paul Bakker33b43f12013-08-20 11:48:36 +0200518/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100519void mbedtls_rsa_private( data_t * message_str, int mod,
520 char * input_P, char * input_Q,
Werner Lewisefda01f2022-07-06 13:03:36 +0100521 char * input_N, char * input_E,
522 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000523{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200524 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100526 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200527 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200528 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000529
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100530 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
531 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200532 mbedtls_rsa_init( &ctx );
533 mbedtls_rsa_init( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000534
Ronald Cron351f0ee2020-06-10 12:12:18 +0200535 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000536
Werner Lewis19b4cd82022-07-07 11:02:27 +0100537 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
538 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
539 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
540 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000541
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100542 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
Gilles Peskine058d0092021-06-09 16:24:35 +0200543
544 /* Check test data consistency */
545 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100546 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100547 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000549
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200550 /* repeat three times to test updating of blinding values */
551 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000552 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200553 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200554 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
555 &rnd_info, message_str->x,
556 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200557 if( result == 0 )
558 {
Paul Bakker821fb082009-07-12 13:26:42 +0000559
Ronald Cronac6ae352020-06-26 14:33:03 +0200560 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200561 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200562 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200563 }
Paul Bakker821fb082009-07-12 13:26:42 +0000564 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000565
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100566 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200568 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100572
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200573 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200574 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
575 &rnd_info, message_str->x,
576 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100577 if( result == 0 )
578 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100579
Ronald Cronac6ae352020-06-26 14:33:03 +0200580 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200581 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200582 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100583 }
584
Paul Bakkerbd51b262014-07-10 15:26:12 +0200585exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100586 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
587 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000590}
Paul Bakker33b43f12013-08-20 11:48:36 +0200591/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000592
Paul Bakker33b43f12013-08-20 11:48:36 +0200593/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100594void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_rsa_context ctx;
597 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000600}
Paul Bakker33b43f12013-08-20 11:48:36 +0200601/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000602
Paul Bakker33b43f12013-08-20 11:48:36 +0200603/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100604void mbedtls_rsa_check_pubkey( char * input_N, char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000605{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100607 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000608
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100609 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200610 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000611
Paul Bakker33b43f12013-08-20 11:48:36 +0200612 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000613 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100614 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000615 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200616 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000617 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100618 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000619 }
620
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100621 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100623
Paul Bakkerbd51b262014-07-10 15:26:12 +0200624exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100625 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000627}
Paul Bakker33b43f12013-08-20 11:48:36 +0200628/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000629
Paul Bakker33b43f12013-08-20 11:48:36 +0200630/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100631void mbedtls_rsa_check_privkey( int mod, char * input_P, char * input_Q,
632 char * input_N, char * input_E, char * input_D,
633 char * input_DP, char * input_DQ, char * input_QP,
634 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000635{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000637
Ronald Cronc1905a12021-06-05 11:11:14 +0200638 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000639
Paul Bakker33b43f12013-08-20 11:48:36 +0200640 ctx.len = mod / 8;
641 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000642 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100643 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000644 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200645 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000646 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100647 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000648 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200649 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000650 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100651 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000652 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200653 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000654 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100655 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000656 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200657 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000658 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100659 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000660 }
Hanno Becker131134f2017-08-23 08:31:07 +0100661#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200662 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000663 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100664 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000665 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200666 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000667 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100668 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000669 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200670 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000671 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100672 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000673 }
Hanno Becker131134f2017-08-23 08:31:07 +0100674#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100675 ((void) input_DP);
676 ((void) input_DQ);
677 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100678#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100681
Paul Bakkerbd51b262014-07-10 15:26:12 +0200682exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000684}
Paul Bakker33b43f12013-08-20 11:48:36 +0200685/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000686
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100687/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100688void rsa_check_pubpriv( int mod, char * input_Npub, char * input_Epub,
689 char * input_P, char * input_Q, char * input_N,
690 char * input_E, char * input_D, char * input_DP,
691 char * input_DQ, char * input_QP, int result )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100692{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100694
Ronald Cronc1905a12021-06-05 11:11:14 +0200695 mbedtls_rsa_init( &pub );
696 mbedtls_rsa_init( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100697
698 pub.len = mod / 8;
699 prv.len = mod / 8;
700
701 if( strlen( input_Npub ) )
702 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100703 TEST_ASSERT( mbedtls_test_read_mpi( &pub.N, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100704 }
705 if( strlen( input_Epub ) )
706 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100707 TEST_ASSERT( mbedtls_test_read_mpi( &pub.E, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708 }
709
710 if( strlen( input_P ) )
711 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100712 TEST_ASSERT( mbedtls_test_read_mpi( &prv.P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100713 }
714 if( strlen( input_Q ) )
715 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100716 TEST_ASSERT( mbedtls_test_read_mpi( &prv.Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100717 }
718 if( strlen( input_N ) )
719 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100720 TEST_ASSERT( mbedtls_test_read_mpi( &prv.N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100721 }
722 if( strlen( input_E ) )
723 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100724 TEST_ASSERT( mbedtls_test_read_mpi( &prv.E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100725 }
726 if( strlen( input_D ) )
727 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100728 TEST_ASSERT( mbedtls_test_read_mpi( &prv.D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100729 }
Hanno Becker131134f2017-08-23 08:31:07 +0100730#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100731 if( strlen( input_DP ) )
732 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100733 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100734 }
735 if( strlen( input_DQ ) )
736 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100737 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100738 }
739 if( strlen( input_QP ) )
740 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100741 TEST_ASSERT( mbedtls_test_read_mpi( &prv.QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100742 }
Hanno Becker131134f2017-08-23 08:31:07 +0100743#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100744 ((void) input_DP);
745 ((void) input_DQ);
746 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100747#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100750
751exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_rsa_free( &pub );
753 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100754}
755/* END_CASE */
756
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200757/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000759{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_rsa_context ctx;
Ronald Cronc1905a12021-06-05 11:11:14 +0200761 mbedtls_rsa_init ( &ctx );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000762
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200763 /* This test uses an insecure RNG, suitable only for testing.
764 * In production, always use a cryptographically strong RNG! */
765 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_test_rnd_std_rand, NULL, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200766 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100769 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000770 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100771
Paul Bakkerbd51b262014-07-10 15:26:12 +0200772exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000774}
Paul Bakker33b43f12013-08-20 11:48:36 +0200775/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000776
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +0200777/* BEGIN_CASE */
Werner Lewis3ccc1162022-08-01 15:11:48 +0100778void mbedtls_rsa_deduce_primes( char *input_N,
779 char *input_D,
780 char *input_E,
781 char *output_P,
782 char *output_Q,
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100783 int corrupt, int result )
784{
785 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
786
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100787 mbedtls_mpi_init( &N );
788 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
789 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
790 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
791
Werner Lewis19b4cd82022-07-07 11:02:27 +0100792 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
793 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
794 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
795 TEST_ASSERT( mbedtls_test_read_mpi( &Qp, output_P ) == 0 );
796 TEST_ASSERT( mbedtls_test_read_mpi( &Pp, output_Q ) == 0 );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100797
798 if( corrupt )
799 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
800
801 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100802 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100803
804 if( !corrupt )
805 {
806 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
807 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
808 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
809 }
810
811exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100812 mbedtls_mpi_free( &N );
813 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
814 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
815 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100816}
817/* END_CASE */
818
Hanno Becker6b4ce492017-08-23 11:00:21 +0100819/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100820void mbedtls_rsa_deduce_private_exponent( char *input_P,
821 char *input_Q,
822 char *input_E,
823 char *output_D,
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100824 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +0100825{
826 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
827
828 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
829 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
830 mbedtls_mpi_init( &E );
831 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
832
Werner Lewis19b4cd82022-07-07 11:02:27 +0100833 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
834 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
835 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
836 TEST_ASSERT( mbedtls_test_read_mpi( &Dp, output_D ) == 0 );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100837
838 if( corrupt )
839 {
840 /* Make E even */
841 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
842 }
843
844 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100845 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
846 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100847
848 if( !corrupt )
849 {
850 /*
851 * Check that D and Dp agree modulo LCM(P-1, Q-1).
852 */
853
854 /* Replace P,Q by P-1, Q-1 */
855 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
856 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
857
858 /* Check D == Dp modulo P-1 */
859 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
860 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
861 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
862
863 /* Check D == Dp modulo Q-1 */
864 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
865 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
866 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
867 }
868
869exit:
870
871 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
872 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
873 mbedtls_mpi_free( &E );
874 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
875}
876/* END_CASE */
877
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200878/* BEGIN_CASE */
Werner Lewis3ccc1162022-08-01 15:11:48 +0100879void mbedtls_rsa_import( char *input_N,
880 char *input_P,
881 char *input_Q,
882 char *input_D,
883 char *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100884 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +0100885 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +0100886 int res_check,
887 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100888{
889 mbedtls_mpi N, P, Q, D, E;
890 mbedtls_rsa_context ctx;
891
Hanno Beckere1582a82017-09-29 11:51:05 +0100892 /* Buffers used for encryption-decryption test */
893 unsigned char *buf_orig = NULL;
894 unsigned char *buf_enc = NULL;
895 unsigned char *buf_dec = NULL;
896
Hanno Becker4d6e8342017-09-29 11:50:18 +0100897 const int have_N = ( strlen( input_N ) > 0 );
898 const int have_P = ( strlen( input_P ) > 0 );
899 const int have_Q = ( strlen( input_Q ) > 0 );
900 const int have_D = ( strlen( input_D ) > 0 );
901 const int have_E = ( strlen( input_E ) > 0 );
902
Ronald Cronc1905a12021-06-05 11:11:14 +0200903 mbedtls_rsa_init( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100904
905 mbedtls_mpi_init( &N );
906 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
907 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
908
Hanno Becker4d6e8342017-09-29 11:50:18 +0100909 if( have_N )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100910 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100911
Hanno Becker4d6e8342017-09-29 11:50:18 +0100912 if( have_P )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100913 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100914
Hanno Becker4d6e8342017-09-29 11:50:18 +0100915 if( have_Q )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100916 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100917
Hanno Becker4d6e8342017-09-29 11:50:18 +0100918 if( have_D )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100919 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100920
Hanno Becker4d6e8342017-09-29 11:50:18 +0100921 if( have_E )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100922 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100923
924 if( !successive )
925 {
926 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100927 have_N ? &N : NULL,
928 have_P ? &P : NULL,
929 have_Q ? &Q : NULL,
930 have_D ? &D : NULL,
931 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100932 }
933 else
934 {
935 /* Import N, P, Q, D, E separately.
936 * This should make no functional difference. */
937
938 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100939 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100940 NULL, NULL, NULL, NULL ) == 0 );
941
942 TEST_ASSERT( mbedtls_rsa_import( &ctx,
943 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100944 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100945 NULL, NULL, NULL ) == 0 );
946
947 TEST_ASSERT( mbedtls_rsa_import( &ctx,
948 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100949 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100950 NULL, NULL ) == 0 );
951
952 TEST_ASSERT( mbedtls_rsa_import( &ctx,
953 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100954 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100955 NULL ) == 0 );
956
957 TEST_ASSERT( mbedtls_rsa_import( &ctx,
958 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100959 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100960 }
961
Hanno Becker04877a42017-10-11 10:01:33 +0100962 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100963
Hanno Beckere1582a82017-09-29 11:51:05 +0100964 /* On expected success, perform some public and private
965 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +0100966 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +0100967 {
Hanno Beckere1582a82017-09-29 11:51:05 +0100968 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +0100969 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
970 else
971 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
972
973 if( res_check != 0 )
974 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +0100975
976 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
977 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
978 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
979 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
980 goto exit;
981
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200982 /* This test uses an insecure RNG, suitable only for testing.
983 * In production, always use a cryptographically strong RNG! */
984 TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL,
Hanno Beckere1582a82017-09-29 11:51:05 +0100985 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
986
987 /* Make sure the number we're generating is smaller than the modulus */
988 buf_orig[0] = 0x00;
989
990 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
991
992 if( is_priv )
993 {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200994 /* This test uses an insecure RNG, suitable only for testing.
995 * In production, always use a cryptographically strong RNG! */
996 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_std_rand,
997 NULL, buf_enc,
Hanno Beckere1582a82017-09-29 11:51:05 +0100998 buf_dec ) == 0 );
999
1000 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1001 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1002 }
1003 }
1004
Hanno Beckerc77ab892017-08-23 11:01:06 +01001005exit:
1006
Hanno Beckere1582a82017-09-29 11:51:05 +01001007 mbedtls_free( buf_orig );
1008 mbedtls_free( buf_enc );
1009 mbedtls_free( buf_dec );
1010
Hanno Beckerc77ab892017-08-23 11:01:06 +01001011 mbedtls_rsa_free( &ctx );
1012
Hanno Beckerc77ab892017-08-23 11:01:06 +01001013 mbedtls_mpi_free( &N );
1014 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1015 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1016}
1017/* END_CASE */
1018
Hanno Becker417f2d62017-08-23 11:44:51 +01001019/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001020void mbedtls_rsa_export( char *input_N,
1021 char *input_P,
1022 char *input_Q,
1023 char *input_D,
1024 char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001025 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001026 int successive )
1027{
1028 /* Original MPI's with which we set up the RSA context */
1029 mbedtls_mpi N, P, Q, D, E;
1030
1031 /* Exported MPI's */
1032 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1033
1034 const int have_N = ( strlen( input_N ) > 0 );
1035 const int have_P = ( strlen( input_P ) > 0 );
1036 const int have_Q = ( strlen( input_Q ) > 0 );
1037 const int have_D = ( strlen( input_D ) > 0 );
1038 const int have_E = ( strlen( input_E ) > 0 );
1039
Hanno Becker417f2d62017-08-23 11:44:51 +01001040 mbedtls_rsa_context ctx;
1041
Ronald Cronc1905a12021-06-05 11:11:14 +02001042 mbedtls_rsa_init( &ctx );
Hanno Becker417f2d62017-08-23 11:44:51 +01001043
1044 mbedtls_mpi_init( &N );
1045 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1046 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1047
1048 mbedtls_mpi_init( &Ne );
1049 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1050 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1051
1052 /* Setup RSA context */
1053
1054 if( have_N )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001055 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001056
1057 if( have_P )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001058 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001059
1060 if( have_Q )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001061 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001062
1063 if( have_D )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001064 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001065
1066 if( have_E )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001067 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001068
1069 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1070 strlen( input_N ) ? &N : NULL,
1071 strlen( input_P ) ? &P : NULL,
1072 strlen( input_Q ) ? &Q : NULL,
1073 strlen( input_D ) ? &D : NULL,
1074 strlen( input_E ) ? &E : NULL ) == 0 );
1075
Hanno Becker7f25f852017-10-10 16:56:22 +01001076 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001077
1078 /*
1079 * Export parameters and compare to original ones.
1080 */
1081
1082 /* N and E must always be present. */
1083 if( !successive )
1084 {
1085 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1086 }
1087 else
1088 {
1089 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1090 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1091 }
1092 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1093 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1094
1095 /* If we were providing enough information to setup a complete private context,
1096 * we expect to be able to export all core parameters. */
1097
1098 if( is_priv )
1099 {
1100 if( !successive )
1101 {
1102 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1103 &De, NULL ) == 0 );
1104 }
1105 else
1106 {
1107 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1108 NULL, NULL ) == 0 );
1109 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1110 NULL, NULL ) == 0 );
1111 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1112 &De, NULL ) == 0 );
1113 }
1114
1115 if( have_P )
1116 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1117
1118 if( have_Q )
1119 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1120
1121 if( have_D )
1122 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1123
1124 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001125 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1126 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001127 }
1128
1129exit:
1130
1131 mbedtls_rsa_free( &ctx );
1132
1133 mbedtls_mpi_free( &N );
1134 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1135 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1136
1137 mbedtls_mpi_free( &Ne );
1138 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1139 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1140}
1141/* END_CASE */
1142
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001143/* BEGIN_CASE */
Werner Lewis3ccc1162022-08-01 15:11:48 +01001144void mbedtls_rsa_validate_params( char *input_N,
1145 char *input_P,
1146 char *input_Q,
1147 char *input_D,
1148 char *input_E,
Hanno Becker750e8b42017-08-25 07:54:27 +01001149 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001150{
1151 /* Original MPI's with which we set up the RSA context */
1152 mbedtls_mpi N, P, Q, D, E;
1153
1154 const int have_N = ( strlen( input_N ) > 0 );
1155 const int have_P = ( strlen( input_P ) > 0 );
1156 const int have_Q = ( strlen( input_Q ) > 0 );
1157 const int have_D = ( strlen( input_D ) > 0 );
1158 const int have_E = ( strlen( input_E ) > 0 );
1159
Hanno Beckerce002632017-08-23 13:22:36 +01001160 mbedtls_mpi_init( &N );
1161 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1162 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1163
Hanno Beckerce002632017-08-23 13:22:36 +01001164 if( have_N )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001165 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001166
1167 if( have_P )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001168 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001169
1170 if( have_Q )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001171 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001172
1173 if( have_D )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001174 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001175
1176 if( have_E )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001177 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001178
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001179 /* This test uses an insecure RNG, suitable only for testing.
1180 * In production, always use a cryptographically strong RNG! */
Hanno Becker750e8b42017-08-25 07:54:27 +01001181 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1182 have_P ? &P : NULL,
1183 have_Q ? &Q : NULL,
1184 have_D ? &D : NULL,
1185 have_E ? &E : NULL,
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001186 prng ? mbedtls_test_rnd_std_rand : NULL,
1187 prng ? NULL : NULL ) == result );
1188
Hanno Beckerce002632017-08-23 13:22:36 +01001189exit:
Hanno Beckerce002632017-08-23 13:22:36 +01001190 mbedtls_mpi_free( &N );
1191 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1192 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1193}
1194/* END_CASE */
1195
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +02001196/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001197void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1198 data_t *input_Q, data_t *input_D,
1199 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001200 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001201{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001202 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001203 unsigned char bufNe[256];
1204 unsigned char bufPe[128];
1205 unsigned char bufQe[128];
1206 unsigned char bufDe[256];
1207 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001208
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001209 mbedtls_rsa_context ctx;
1210
Ronald Cronc1905a12021-06-05 11:11:14 +02001211 mbedtls_rsa_init( &ctx );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001212
1213 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001214 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001215 input_N->len ? input_N->x : NULL, input_N->len,
1216 input_P->len ? input_P->x : NULL, input_P->len,
1217 input_Q->len ? input_Q->x : NULL, input_Q->len,
1218 input_D->len ? input_D->x : NULL, input_D->len,
1219 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001220
Hanno Becker7f25f852017-10-10 16:56:22 +01001221 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001222
1223 /*
1224 * Export parameters and compare to original ones.
1225 */
1226
1227 /* N and E must always be present. */
1228 if( !successive )
1229 {
Azim Khand30ca132017-06-09 04:32:58 +01001230 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001231 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001232 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001233 }
1234 else
1235 {
Azim Khand30ca132017-06-09 04:32:58 +01001236 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001237 NULL, 0, NULL, 0, NULL, 0,
1238 NULL, 0 ) == 0 );
1239 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1240 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001241 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001242 }
Azim Khand30ca132017-06-09 04:32:58 +01001243 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1244 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001245
1246 /* If we were providing enough information to setup a complete private context,
1247 * we expect to be able to export all core parameters. */
1248
1249 if( is_priv )
1250 {
1251 if( !successive )
1252 {
1253 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001254 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1255 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1256 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001257 NULL, 0 ) == 0 );
1258 }
1259 else
1260 {
1261 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001262 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001263 NULL, 0, NULL, 0,
1264 NULL, 0 ) == 0 );
1265
1266 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001267 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001268 NULL, 0, NULL, 0 ) == 0 );
1269
Azim Khand30ca132017-06-09 04:32:58 +01001270 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1271 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001272 NULL, 0 ) == 0 );
1273 }
1274
Azim Khand30ca132017-06-09 04:32:58 +01001275 if( input_P->len )
1276 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001277
Azim Khand30ca132017-06-09 04:32:58 +01001278 if( input_Q->len )
1279 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001280
Azim Khand30ca132017-06-09 04:32:58 +01001281 if( input_D->len )
1282 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001283
1284 }
1285
1286exit:
1287 mbedtls_rsa_free( &ctx );
1288}
1289/* END_CASE */
1290
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001291/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001292void mbedtls_rsa_import_raw( data_t *input_N,
1293 data_t *input_P, data_t *input_Q,
1294 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001295 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001296 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001297 int res_check,
1298 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001299{
Hanno Beckere1582a82017-09-29 11:51:05 +01001300 /* Buffers used for encryption-decryption test */
1301 unsigned char *buf_orig = NULL;
1302 unsigned char *buf_enc = NULL;
1303 unsigned char *buf_dec = NULL;
1304
Hanno Beckerc77ab892017-08-23 11:01:06 +01001305 mbedtls_rsa_context ctx;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001306
Ronald Cronc1905a12021-06-05 11:11:14 +02001307 mbedtls_rsa_init( &ctx );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001308
Hanno Beckerc77ab892017-08-23 11:01:06 +01001309 if( !successive )
1310 {
1311 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001312 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1313 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1314 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1315 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1316 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001317 }
1318 else
1319 {
1320 /* Import N, P, Q, D, E separately.
1321 * This should make no functional difference. */
1322
1323 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001324 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001325 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1326
1327 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1328 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001329 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001330 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1331
1332 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1333 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001334 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001335 NULL, 0, NULL, 0 ) == 0 );
1336
1337 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1338 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001339 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001340 NULL, 0 ) == 0 );
1341
1342 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1343 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001344 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001345 }
1346
Hanno Becker04877a42017-10-11 10:01:33 +01001347 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001348
Hanno Beckere1582a82017-09-29 11:51:05 +01001349 /* On expected success, perform some public and private
1350 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001351 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001352 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001353 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001354 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1355 else
1356 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1357
1358 if( res_check != 0 )
1359 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001360
1361 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1362 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1363 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1364 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1365 goto exit;
1366
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001367 /* This test uses an insecure RNG, suitable only for testing.
1368 * In production, always use a cryptographically strong RNG! */
1369 TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL,
Hanno Beckere1582a82017-09-29 11:51:05 +01001370 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1371
1372 /* Make sure the number we're generating is smaller than the modulus */
1373 buf_orig[0] = 0x00;
1374
1375 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1376
1377 if( is_priv )
1378 {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001379 /* This test uses an insecure RNG, suitable only for testing.
1380 * In production, always use a cryptographically strong RNG! */
1381 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_std_rand,
1382 NULL, buf_enc,
Hanno Beckere1582a82017-09-29 11:51:05 +01001383 buf_dec ) == 0 );
1384
1385 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1386 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1387 }
1388 }
1389
Hanno Beckerc77ab892017-08-23 11:01:06 +01001390exit:
1391
Hanno Becker3f3ae852017-10-02 10:08:39 +01001392 mbedtls_free( buf_orig );
1393 mbedtls_free( buf_enc );
1394 mbedtls_free( buf_dec );
1395
Hanno Beckerc77ab892017-08-23 11:01:06 +01001396 mbedtls_rsa_free( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001397}
1398/* END_CASE */
1399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001401void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001402{
Andres AG93012e82016-09-09 09:10:28 +01001403 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001404}
Paul Bakker33b43f12013-08-20 11:48:36 +02001405/* END_CASE */