blob: 65731ed93beb8e3d199820334e59648d0feb29a4 [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é-Gonnard07018f92022-09-15 11:29:35 +02005#include "mbedtls/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 unsigned char buf[] = {0x00,0x01,0x02,0x03,0x04,0x05};
20 size_t buf_len = sizeof( buf );
Ronald Cronea7631b2021-06-03 18:51:59 +020021
Ronald Cronc1905a12021-06-05 11:11:14 +020022 mbedtls_rsa_init( &ctx );
Ronald Cronea7631b2021-06-03 18:51:59 +020023
24 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
25 invalid_padding,
26 MBEDTLS_MD_NONE ),
27 MBEDTLS_ERR_RSA_INVALID_PADDING );
28
29 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
30 MBEDTLS_RSA_PKCS_V21,
31 invalid_hash_id ),
32 MBEDTLS_ERR_RSA_INVALID_PADDING );
33
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010034 TEST_EQUAL( mbedtls_rsa_pkcs1_sign(&ctx, NULL,
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010035 NULL, MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010036 buf_len,
37 NULL, buf),
38 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
39
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010040 TEST_EQUAL( mbedtls_rsa_pkcs1_sign(&ctx, NULL,
41 NULL, MBEDTLS_MD_SHA256,
42 0,
43 NULL, buf),
44 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
45
46 TEST_EQUAL( mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010047 buf_len,
48 NULL, buf),
49 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
50
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010051 TEST_EQUAL( mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_SHA256,
52 0,
53 NULL, buf),
54 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
55
Ronald Cron3a0375f2021-06-08 10:22:28 +020056#if !defined(MBEDTLS_PKCS1_V15)
57 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
58 MBEDTLS_RSA_PKCS_V15,
59 MBEDTLS_MD_NONE ),
60 MBEDTLS_ERR_RSA_INVALID_PADDING );
61#endif
62
Tuvshinzaya Erdenekhuufe7524d2022-09-01 16:07:18 +010063#if defined(MBEDTLS_PKCS1_V15)
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010064 TEST_EQUAL( mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010065 NULL, MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010066 buf_len,
67 NULL, buf),
68 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
69
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010070 TEST_EQUAL( mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
71 NULL, MBEDTLS_MD_SHA256,
72 0,
73 NULL, buf),
74 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
75
76 TEST_EQUAL( mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010077 buf_len,
78 NULL, buf),
79 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
80
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010081 TEST_EQUAL( mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_SHA256,
82 0,
83 NULL, buf),
84 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
85
86
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010087#endif
88
Ronald Cron3a0375f2021-06-08 10:22:28 +020089#if !defined(MBEDTLS_PKCS1_V21)
90 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
91 MBEDTLS_RSA_PKCS_V21,
92 MBEDTLS_MD_NONE ),
93 MBEDTLS_ERR_RSA_INVALID_PADDING );
94#endif
95
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010096#if defined(MBEDTLS_PKCS1_V21)
97 TEST_EQUAL( mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010098 MBEDTLS_MD_NONE, buf_len,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010099 NULL, buf_len,
100 buf ),
101 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
102
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100103 TEST_EQUAL( mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
104 MBEDTLS_MD_SHA256, 0,
105 NULL, buf_len,
106 buf ),
107 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
108
109 TEST_EQUAL( mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100110 buf_len, NULL,
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100111 MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100112 buf_len, buf),
113 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
114
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100115 TEST_EQUAL( mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_SHA256,
116 0, NULL,
117 MBEDTLS_MD_NONE,
118 buf_len, buf),
119 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
120
121 TEST_EQUAL( mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_NONE,
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100122 buf_len,
123 NULL, buf),
124 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
125
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100126 TEST_EQUAL( mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_SHA256,
127 0,
128 NULL, buf),
129 MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100130#endif
131
Ronald Cronea7631b2021-06-03 18:51:59 +0200132exit:
133 mbedtls_rsa_free( &ctx );
134}
135/* END_CASE */
136
137/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100138void rsa_init_free( int reinit )
139{
140 mbedtls_rsa_context ctx;
141
142 /* Double free is not explicitly documented to work, but we rely on it
143 * even inside the library so that you can call mbedtls_rsa_free()
144 * unconditionally on an error path without checking whether it has
145 * already been called in the success path. */
146
Ronald Cronc1905a12021-06-05 11:11:14 +0200147 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +0100148 mbedtls_rsa_free( &ctx );
149
150 if( reinit )
Ronald Cronc1905a12021-06-05 11:11:14 +0200151 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +0100152 mbedtls_rsa_free( &ctx );
153
154 /* This test case always succeeds, functionally speaking. A plausible
155 * bug might trigger an invalid pointer dereference or a memory leak. */
156 goto exit;
157}
158/* END_CASE */
159
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200160/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100161void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Werner Lewis9802d362022-07-07 11:37:24 +0100162 int digest, int mod, char * input_P,
Werner Lewisefda01f2022-07-06 13:03:36 +0100163 char * input_Q, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200164 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000165{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200166 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100168 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200169 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000170
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100171 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
172 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200173 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200174 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,padding_mode,
175 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000176
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200177 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200178 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000179
Werner Lewis19b4cd82022-07-07 11:02:27 +0100180 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
181 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
182 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
183 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000184
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100185 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
186 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100187 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000189
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200190 TEST_ASSERT( mbedtls_rsa_pkcs1_sign(
191 &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnarda4aa12f2022-07-16 08:20:26 +0200192 digest, message_str->len, message_str->x,
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200193 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200194 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000195 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000196
Ronald Cronac6ae352020-06-26 14:33:03 +0200197 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
198 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000199 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000200
Paul Bakkerbd51b262014-07-10 15:26:12 +0200201exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100202 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
203 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000205}
Paul Bakker33b43f12013-08-20 11:48:36 +0200206/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000207
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200208/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100209void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Werner Lewis9802d362022-07-07 11:37:24 +0100210 int digest, int mod,
211 char * input_N, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100212 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000213{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100215 mbedtls_mpi N, E;
216
217 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200218 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200219 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
220 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000221
Werner Lewis19b4cd82022-07-07 11:02:27 +0100222 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
223 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100224 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
225 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000227
Manuel Pégourié-Gonnarda4aa12f2022-07-16 08:20:26 +0200228 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 +0100229
Paul Bakkerbd51b262014-07-10 15:26:12 +0200230exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100231 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000233}
Paul Bakker33b43f12013-08-20 11:48:36 +0200234/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000235
Paul Bakker821fb082009-07-12 13:26:42 +0000236
Paul Bakker33b43f12013-08-20 11:48:36 +0200237/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100238void rsa_pkcs1_sign_raw( data_t * hash_result,
Werner Lewis9802d362022-07-07 11:37:24 +0100239 int padding_mode, int mod,
240 char * input_P, char * input_Q,
Werner Lewisefda01f2022-07-06 13:03:36 +0100241 char * input_N, char * input_E,
242 data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000243{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200244 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100246 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200247 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000248
Ronald Cronc1905a12021-06-05 11:11:14 +0200249 mbedtls_rsa_init( &ctx );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100250 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
251 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000252
Paul Elliotte57dd2d2021-06-25 11:13:24 +0100253 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
254 MBEDTLS_MD_NONE ) == 0 );
255
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200256 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200257 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000258
Werner Lewis19b4cd82022-07-07 11:02:27 +0100259 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
260 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
261 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
262 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000263
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100264 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
265 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100266 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000268
Paul Bakker821fb082009-07-12 13:26:42 +0000269
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200270 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100271 &rnd_info, MBEDTLS_MD_NONE,
272 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200273 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000274
Paul Bakker821fb082009-07-12 13:26:42 +0000275
Ronald Cronac6ae352020-06-26 14:33:03 +0200276 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
277 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000278
Paul Bakkerbd51b262014-07-10 15:26:12 +0200279exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100280 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
281 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000284}
Paul Bakker33b43f12013-08-20 11:48:36 +0200285/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000286
Paul Bakker33b43f12013-08-20 11:48:36 +0200287/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100288void rsa_pkcs1_verify_raw( data_t * hash_result,
Werner Lewis9802d362022-07-07 11:37:24 +0100289 int padding_mode, int mod,
290 char * input_N, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100291 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000292{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200293 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000295
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100296 mbedtls_mpi N, E;
297 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
298
Ronald Cronc1905a12021-06-05 11:11:14 +0200299 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200300 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
301 MBEDTLS_MD_NONE ) == 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100302 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000303
Werner Lewis19b4cd82022-07-07 11:02:27 +0100304 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
305 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000306
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100307 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
308 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000310
Paul Bakker821fb082009-07-12 13:26:42 +0000311
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100312 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 +0100313
Paul Bakkerbd51b262014-07-10 15:26:12 +0200314exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100315 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000317}
Paul Bakker33b43f12013-08-20 11:48:36 +0200318/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000319
Paul Bakker33b43f12013-08-20 11:48:36 +0200320/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100321void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Werner Lewisefda01f2022-07-06 13:03:36 +0100322 int mod, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200323 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000324{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200325 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200327 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000328
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100329 mbedtls_mpi N, E;
330 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
331
Ronald Cron351f0ee2020-06-10 12:12:18 +0200332 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000333
Ronald Cronc1905a12021-06-05 11:11:14 +0200334 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200335 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
336 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200337 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000338
Werner Lewis19b4cd82022-07-07 11:02:27 +0100339 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
340 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000341
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100342 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
343 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000345
Paul Bakker42a29bf2009-07-07 20:18:41 +0000346
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200347 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
348 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100349 &rnd_info, message_str->len,
350 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200351 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200352 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000353 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000354
Ronald Cronac6ae352020-06-26 14:33:03 +0200355 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
356 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000357 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100358
Paul Bakkerbd51b262014-07-10 15:26:12 +0200359exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100360 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000362}
Paul Bakker33b43f12013-08-20 11:48:36 +0200363/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000364
Paul Bakker33b43f12013-08-20 11:48:36 +0200365/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100366void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Werner Lewisefda01f2022-07-06 13:03:36 +0100367 int mod, char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200368 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000369{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200370 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000372
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100373 mbedtls_mpi N, E;
374
375 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200376 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200377 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
378 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200379 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000380
Werner Lewis19b4cd82022-07-07 11:02:27 +0100381 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
382 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000383
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100384 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
385 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000387
Paul Bakkera6656852010-07-18 19:47:14 +0000388
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200389 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100390 NULL, message_str->len,
391 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200392 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200393 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000394 {
Paul Bakkera6656852010-07-18 19:47:14 +0000395
Ronald Cronac6ae352020-06-26 14:33:03 +0200396 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
397 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000398 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100399
Paul Bakkerbd51b262014-07-10 15:26:12 +0200400exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100401 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000403}
Paul Bakker33b43f12013-08-20 11:48:36 +0200404/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000405
Paul Bakker33b43f12013-08-20 11:48:36 +0200406/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100407void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Werner Lewis9802d362022-07-07 11:37:24 +0100408 int mod, char * input_P,
Werner Lewisefda01f2022-07-06 13:03:36 +0100409 char * input_Q, char * input_N,
410 char * input_E, int max_output,
411 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000412{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200413 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000415 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200416 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100417 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000418
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100419 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
420 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
421
Ronald Cronc1905a12021-06-05 11:11:14 +0200422 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200423 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
424 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000425
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200426 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200427 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000428
Paul Bakker42a29bf2009-07-07 20:18:41 +0000429
Werner Lewis19b4cd82022-07-07 11:02:27 +0100430 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
431 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
432 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
433 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000434
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100435 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
436 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100437 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000439
Paul Bakker69998dd2009-07-11 19:15:20 +0000440 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000441
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200442 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100443 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200444 &output_len, message_str->x, output,
445 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200446 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000447 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000448
Ronald Cronac6ae352020-06-26 14:33:03 +0200449 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200450 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200451 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000452 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000453
Paul Bakkerbd51b262014-07-10 15:26:12 +0200454exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100455 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
456 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000458}
Paul Bakker33b43f12013-08-20 11:48:36 +0200459/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000460
Paul Bakker33b43f12013-08-20 11:48:36 +0200461/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100462void mbedtls_rsa_public( data_t * message_str, int mod,
463 char * input_N, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200464 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000465{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200466 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000468
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100469 mbedtls_mpi N, E;
470
471 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200472 mbedtls_rsa_init( &ctx );
473 mbedtls_rsa_init( &ctx2 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200474 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000475
Werner Lewis19b4cd82022-07-07 11:02:27 +0100476 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
477 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000478
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100479 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Gilles Peskine058d0092021-06-09 16:24:35 +0200480
481 /* Check test data consistency */
482 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100483 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000485
Azim Khand30ca132017-06-09 04:32:58 +0100486 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200487 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000488 {
Paul Bakker821fb082009-07-12 13:26:42 +0000489
Ronald Cronac6ae352020-06-26 14:33:03 +0200490 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
491 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000492 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100493
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100494 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200496 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100500
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200501 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100502 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100503 if( result == 0 )
504 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100505
Ronald Cronac6ae352020-06-26 14:33:03 +0200506 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
507 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100508 }
509
Paul Bakkerbd51b262014-07-10 15:26:12 +0200510exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100511 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_rsa_free( &ctx );
513 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000514}
Paul Bakker33b43f12013-08-20 11:48:36 +0200515/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000516
Paul Bakker33b43f12013-08-20 11:48:36 +0200517/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100518void mbedtls_rsa_private( data_t * message_str, int mod,
519 char * input_P, char * input_Q,
Werner Lewisefda01f2022-07-06 13:03:36 +0100520 char * input_N, char * input_E,
521 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000522{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200523 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100525 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200526 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200527 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000528
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100529 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
530 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200531 mbedtls_rsa_init( &ctx );
532 mbedtls_rsa_init( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000533
Ronald Cron351f0ee2020-06-10 12:12:18 +0200534 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000535
Werner Lewis19b4cd82022-07-07 11:02:27 +0100536 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
537 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
538 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
539 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000540
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100541 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
Gilles Peskine058d0092021-06-09 16:24:35 +0200542
543 /* Check test data consistency */
544 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100545 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100546 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000548
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200549 /* repeat three times to test updating of blinding values */
550 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000551 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200552 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200553 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
554 &rnd_info, message_str->x,
555 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200556 if( result == 0 )
557 {
Paul Bakker821fb082009-07-12 13:26:42 +0000558
Ronald Cronac6ae352020-06-26 14:33:03 +0200559 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200560 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200561 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200562 }
Paul Bakker821fb082009-07-12 13:26:42 +0000563 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000564
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100565 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200567 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100571
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200572 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200573 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
574 &rnd_info, message_str->x,
575 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100576 if( result == 0 )
577 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100578
Ronald Cronac6ae352020-06-26 14:33:03 +0200579 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200580 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200581 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100582 }
583
Paul Bakkerbd51b262014-07-10 15:26:12 +0200584exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100585 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
586 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000589}
Paul Bakker33b43f12013-08-20 11:48:36 +0200590/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000591
Paul Bakker33b43f12013-08-20 11:48:36 +0200592/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100593void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000594{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_rsa_context ctx;
596 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000599}
Paul Bakker33b43f12013-08-20 11:48:36 +0200600/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000601
Paul Bakker33b43f12013-08-20 11:48:36 +0200602/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100603void mbedtls_rsa_check_pubkey( char * input_N, char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100606 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000607
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100608 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200609 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000610
Paul Bakker33b43f12013-08-20 11:48:36 +0200611 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000612 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100613 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000614 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200615 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000616 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100617 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000618 }
619
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100620 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100622
Paul Bakkerbd51b262014-07-10 15:26:12 +0200623exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100624 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000626}
Paul Bakker33b43f12013-08-20 11:48:36 +0200627/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000628
Paul Bakker33b43f12013-08-20 11:48:36 +0200629/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100630void mbedtls_rsa_check_privkey( int mod, char * input_P, char * input_Q,
631 char * input_N, char * input_E, char * input_D,
632 char * input_DP, char * input_DQ, char * input_QP,
633 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000636
Ronald Cronc1905a12021-06-05 11:11:14 +0200637 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000638
Paul Bakker33b43f12013-08-20 11:48:36 +0200639 ctx.len = mod / 8;
640 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000641 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100642 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000643 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200644 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000645 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100646 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000647 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200648 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000649 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100650 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000651 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200652 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000653 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100654 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000655 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200656 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000657 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100658 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000659 }
Hanno Becker131134f2017-08-23 08:31:07 +0100660#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200661 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000662 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100663 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000664 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200665 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000666 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100667 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000668 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200669 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000670 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100671 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000672 }
Hanno Becker131134f2017-08-23 08:31:07 +0100673#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100674 ((void) input_DP);
675 ((void) input_DQ);
676 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100677#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100680
Paul Bakkerbd51b262014-07-10 15:26:12 +0200681exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000683}
Paul Bakker33b43f12013-08-20 11:48:36 +0200684/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000685
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100686/* BEGIN_CASE */
Werner Lewisefda01f2022-07-06 13:03:36 +0100687void rsa_check_pubpriv( int mod, char * input_Npub, char * input_Epub,
688 char * input_P, char * input_Q, char * input_N,
689 char * input_E, char * input_D, char * input_DP,
690 char * input_DQ, char * input_QP, int result )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100691{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100693
Ronald Cronc1905a12021-06-05 11:11:14 +0200694 mbedtls_rsa_init( &pub );
695 mbedtls_rsa_init( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696
697 pub.len = mod / 8;
698 prv.len = mod / 8;
699
700 if( strlen( input_Npub ) )
701 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100702 TEST_ASSERT( mbedtls_test_read_mpi( &pub.N, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100703 }
704 if( strlen( input_Epub ) )
705 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100706 TEST_ASSERT( mbedtls_test_read_mpi( &pub.E, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100707 }
708
709 if( strlen( input_P ) )
710 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100711 TEST_ASSERT( mbedtls_test_read_mpi( &prv.P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100712 }
713 if( strlen( input_Q ) )
714 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100715 TEST_ASSERT( mbedtls_test_read_mpi( &prv.Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100716 }
717 if( strlen( input_N ) )
718 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100719 TEST_ASSERT( mbedtls_test_read_mpi( &prv.N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100720 }
721 if( strlen( input_E ) )
722 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100723 TEST_ASSERT( mbedtls_test_read_mpi( &prv.E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100724 }
725 if( strlen( input_D ) )
726 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100727 TEST_ASSERT( mbedtls_test_read_mpi( &prv.D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100728 }
Hanno Becker131134f2017-08-23 08:31:07 +0100729#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100730 if( strlen( input_DP ) )
731 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100732 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100733 }
734 if( strlen( input_DQ ) )
735 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100736 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100737 }
738 if( strlen( input_QP ) )
739 {
Werner Lewis19b4cd82022-07-07 11:02:27 +0100740 TEST_ASSERT( mbedtls_test_read_mpi( &prv.QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100741 }
Hanno Becker131134f2017-08-23 08:31:07 +0100742#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100743 ((void) input_DP);
744 ((void) input_DQ);
745 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100746#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100749
750exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_rsa_free( &pub );
752 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100753}
754/* END_CASE */
755
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200756/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000758{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_rsa_context ctx;
Ronald Cronc1905a12021-06-05 11:11:14 +0200760 mbedtls_rsa_init ( &ctx );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000761
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200762 /* This test uses an insecure RNG, suitable only for testing.
763 * In production, always use a cryptographically strong RNG! */
764 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_test_rnd_std_rand, NULL, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200765 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100768 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000769 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100770
Paul Bakkerbd51b262014-07-10 15:26:12 +0200771exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000773}
Paul Bakker33b43f12013-08-20 11:48:36 +0200774/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000775
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +0200776/* BEGIN_CASE */
Werner Lewis3ccc1162022-08-01 15:11:48 +0100777void mbedtls_rsa_deduce_primes( char *input_N,
778 char *input_D,
779 char *input_E,
780 char *output_P,
781 char *output_Q,
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100782 int corrupt, int result )
783{
784 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
785
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100786 mbedtls_mpi_init( &N );
787 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
788 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
789 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
790
Werner Lewis19b4cd82022-07-07 11:02:27 +0100791 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
792 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
793 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
794 TEST_ASSERT( mbedtls_test_read_mpi( &Qp, output_P ) == 0 );
795 TEST_ASSERT( mbedtls_test_read_mpi( &Pp, output_Q ) == 0 );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100796
797 if( corrupt )
798 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
799
800 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100801 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100802
803 if( !corrupt )
804 {
805 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
806 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
807 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
808 }
809
810exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100811 mbedtls_mpi_free( &N );
812 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
813 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
814 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100815}
816/* END_CASE */
817
Hanno Becker6b4ce492017-08-23 11:00:21 +0100818/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +0100819void mbedtls_rsa_deduce_private_exponent( char *input_P,
820 char *input_Q,
821 char *input_E,
822 char *output_D,
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100823 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +0100824{
825 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
826
827 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
828 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
829 mbedtls_mpi_init( &E );
830 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
831
Werner Lewis19b4cd82022-07-07 11:02:27 +0100832 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
833 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
834 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
835 TEST_ASSERT( mbedtls_test_read_mpi( &Dp, output_D ) == 0 );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100836
837 if( corrupt )
838 {
839 /* Make E even */
840 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
841 }
842
843 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100844 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
845 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100846
847 if( !corrupt )
848 {
849 /*
850 * Check that D and Dp agree modulo LCM(P-1, Q-1).
851 */
852
853 /* Replace P,Q by P-1, Q-1 */
854 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
855 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
856
857 /* Check D == Dp modulo P-1 */
858 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
859 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
860 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
861
862 /* Check D == Dp modulo Q-1 */
863 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
864 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
865 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
866 }
867
868exit:
869
870 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
871 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
872 mbedtls_mpi_free( &E );
873 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
874}
875/* END_CASE */
876
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200877/* BEGIN_CASE */
Werner Lewis3ccc1162022-08-01 15:11:48 +0100878void mbedtls_rsa_import( char *input_N,
879 char *input_P,
880 char *input_Q,
881 char *input_D,
882 char *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100883 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +0100884 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +0100885 int res_check,
886 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100887{
888 mbedtls_mpi N, P, Q, D, E;
889 mbedtls_rsa_context ctx;
890
Hanno Beckere1582a82017-09-29 11:51:05 +0100891 /* Buffers used for encryption-decryption test */
892 unsigned char *buf_orig = NULL;
893 unsigned char *buf_enc = NULL;
894 unsigned char *buf_dec = NULL;
895
Hanno Becker4d6e8342017-09-29 11:50:18 +0100896 const int have_N = ( strlen( input_N ) > 0 );
897 const int have_P = ( strlen( input_P ) > 0 );
898 const int have_Q = ( strlen( input_Q ) > 0 );
899 const int have_D = ( strlen( input_D ) > 0 );
900 const int have_E = ( strlen( input_E ) > 0 );
901
Ronald Cronc1905a12021-06-05 11:11:14 +0200902 mbedtls_rsa_init( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100903
904 mbedtls_mpi_init( &N );
905 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
906 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
907
Hanno Becker4d6e8342017-09-29 11:50:18 +0100908 if( have_N )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100909 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100910
Hanno Becker4d6e8342017-09-29 11:50:18 +0100911 if( have_P )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100912 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100913
Hanno Becker4d6e8342017-09-29 11:50:18 +0100914 if( have_Q )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100915 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100916
Hanno Becker4d6e8342017-09-29 11:50:18 +0100917 if( have_D )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100918 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100919
Hanno Becker4d6e8342017-09-29 11:50:18 +0100920 if( have_E )
Werner Lewis19b4cd82022-07-07 11:02:27 +0100921 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100922
923 if( !successive )
924 {
925 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100926 have_N ? &N : NULL,
927 have_P ? &P : NULL,
928 have_Q ? &Q : NULL,
929 have_D ? &D : NULL,
930 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100931 }
932 else
933 {
934 /* Import N, P, Q, D, E separately.
935 * This should make no functional difference. */
936
937 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100938 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100939 NULL, NULL, NULL, NULL ) == 0 );
940
941 TEST_ASSERT( mbedtls_rsa_import( &ctx,
942 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100943 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100944 NULL, NULL, NULL ) == 0 );
945
946 TEST_ASSERT( mbedtls_rsa_import( &ctx,
947 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100948 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100949 NULL, NULL ) == 0 );
950
951 TEST_ASSERT( mbedtls_rsa_import( &ctx,
952 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100953 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100954 NULL ) == 0 );
955
956 TEST_ASSERT( mbedtls_rsa_import( &ctx,
957 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100958 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100959 }
960
Hanno Becker04877a42017-10-11 10:01:33 +0100961 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100962
Hanno Beckere1582a82017-09-29 11:51:05 +0100963 /* On expected success, perform some public and private
964 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +0100965 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +0100966 {
Hanno Beckere1582a82017-09-29 11:51:05 +0100967 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +0100968 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
969 else
970 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
971
972 if( res_check != 0 )
973 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +0100974
975 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
976 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
977 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
978 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
979 goto exit;
980
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200981 /* This test uses an insecure RNG, suitable only for testing.
982 * In production, always use a cryptographically strong RNG! */
983 TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL,
Hanno Beckere1582a82017-09-29 11:51:05 +0100984 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
985
986 /* Make sure the number we're generating is smaller than the modulus */
987 buf_orig[0] = 0x00;
988
989 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
990
991 if( is_priv )
992 {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200993 /* This test uses an insecure RNG, suitable only for testing.
994 * In production, always use a cryptographically strong RNG! */
995 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_std_rand,
996 NULL, buf_enc,
Hanno Beckere1582a82017-09-29 11:51:05 +0100997 buf_dec ) == 0 );
998
999 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1000 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1001 }
1002 }
1003
Hanno Beckerc77ab892017-08-23 11:01:06 +01001004exit:
1005
Hanno Beckere1582a82017-09-29 11:51:05 +01001006 mbedtls_free( buf_orig );
1007 mbedtls_free( buf_enc );
1008 mbedtls_free( buf_dec );
1009
Hanno Beckerc77ab892017-08-23 11:01:06 +01001010 mbedtls_rsa_free( &ctx );
1011
Hanno Beckerc77ab892017-08-23 11:01:06 +01001012 mbedtls_mpi_free( &N );
1013 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1014 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1015}
1016/* END_CASE */
1017
Hanno Becker417f2d62017-08-23 11:44:51 +01001018/* BEGIN_CASE */
Werner Lewis9802d362022-07-07 11:37:24 +01001019void mbedtls_rsa_export( char *input_N,
1020 char *input_P,
1021 char *input_Q,
1022 char *input_D,
1023 char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001024 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001025 int successive )
1026{
1027 /* Original MPI's with which we set up the RSA context */
1028 mbedtls_mpi N, P, Q, D, E;
1029
1030 /* Exported MPI's */
1031 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1032
1033 const int have_N = ( strlen( input_N ) > 0 );
1034 const int have_P = ( strlen( input_P ) > 0 );
1035 const int have_Q = ( strlen( input_Q ) > 0 );
1036 const int have_D = ( strlen( input_D ) > 0 );
1037 const int have_E = ( strlen( input_E ) > 0 );
1038
Hanno Becker417f2d62017-08-23 11:44:51 +01001039 mbedtls_rsa_context ctx;
1040
Ronald Cronc1905a12021-06-05 11:11:14 +02001041 mbedtls_rsa_init( &ctx );
Hanno Becker417f2d62017-08-23 11:44:51 +01001042
1043 mbedtls_mpi_init( &N );
1044 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1045 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1046
1047 mbedtls_mpi_init( &Ne );
1048 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1049 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1050
1051 /* Setup RSA context */
1052
1053 if( have_N )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001054 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001055
1056 if( have_P )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001057 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001058
1059 if( have_Q )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001060 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001061
1062 if( have_D )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001063 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001064
1065 if( have_E )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001066 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001067
1068 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1069 strlen( input_N ) ? &N : NULL,
1070 strlen( input_P ) ? &P : NULL,
1071 strlen( input_Q ) ? &Q : NULL,
1072 strlen( input_D ) ? &D : NULL,
1073 strlen( input_E ) ? &E : NULL ) == 0 );
1074
Hanno Becker7f25f852017-10-10 16:56:22 +01001075 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001076
1077 /*
1078 * Export parameters and compare to original ones.
1079 */
1080
1081 /* N and E must always be present. */
1082 if( !successive )
1083 {
1084 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1085 }
1086 else
1087 {
1088 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1089 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1090 }
1091 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1092 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1093
1094 /* If we were providing enough information to setup a complete private context,
1095 * we expect to be able to export all core parameters. */
1096
1097 if( is_priv )
1098 {
1099 if( !successive )
1100 {
1101 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1102 &De, NULL ) == 0 );
1103 }
1104 else
1105 {
1106 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1107 NULL, NULL ) == 0 );
1108 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1109 NULL, NULL ) == 0 );
1110 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1111 &De, NULL ) == 0 );
1112 }
1113
1114 if( have_P )
1115 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1116
1117 if( have_Q )
1118 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1119
1120 if( have_D )
1121 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1122
1123 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001124 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1125 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001126 }
1127
1128exit:
1129
1130 mbedtls_rsa_free( &ctx );
1131
1132 mbedtls_mpi_free( &N );
1133 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1134 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1135
1136 mbedtls_mpi_free( &Ne );
1137 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1138 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1139}
1140/* END_CASE */
1141
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001142/* BEGIN_CASE */
Werner Lewis3ccc1162022-08-01 15:11:48 +01001143void mbedtls_rsa_validate_params( char *input_N,
1144 char *input_P,
1145 char *input_Q,
1146 char *input_D,
1147 char *input_E,
Hanno Becker750e8b42017-08-25 07:54:27 +01001148 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001149{
1150 /* Original MPI's with which we set up the RSA context */
1151 mbedtls_mpi N, P, Q, D, E;
1152
1153 const int have_N = ( strlen( input_N ) > 0 );
1154 const int have_P = ( strlen( input_P ) > 0 );
1155 const int have_Q = ( strlen( input_Q ) > 0 );
1156 const int have_D = ( strlen( input_D ) > 0 );
1157 const int have_E = ( strlen( input_E ) > 0 );
1158
Hanno Beckerce002632017-08-23 13:22:36 +01001159 mbedtls_mpi_init( &N );
1160 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1161 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1162
Hanno Beckerce002632017-08-23 13:22:36 +01001163 if( have_N )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001164 TEST_ASSERT( mbedtls_test_read_mpi( &N, input_N ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001165
1166 if( have_P )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001167 TEST_ASSERT( mbedtls_test_read_mpi( &P, input_P ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001168
1169 if( have_Q )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001170 TEST_ASSERT( mbedtls_test_read_mpi( &Q, input_Q ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001171
1172 if( have_D )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001173 TEST_ASSERT( mbedtls_test_read_mpi( &D, input_D ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001174
1175 if( have_E )
Werner Lewis19b4cd82022-07-07 11:02:27 +01001176 TEST_ASSERT( mbedtls_test_read_mpi( &E, input_E ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001177
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001178 /* This test uses an insecure RNG, suitable only for testing.
1179 * In production, always use a cryptographically strong RNG! */
Hanno Becker750e8b42017-08-25 07:54:27 +01001180 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1181 have_P ? &P : NULL,
1182 have_Q ? &Q : NULL,
1183 have_D ? &D : NULL,
1184 have_E ? &E : NULL,
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001185 prng ? mbedtls_test_rnd_std_rand : NULL,
1186 prng ? NULL : NULL ) == result );
1187
Hanno Beckerce002632017-08-23 13:22:36 +01001188exit:
Hanno Beckerce002632017-08-23 13:22:36 +01001189 mbedtls_mpi_free( &N );
1190 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1191 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1192}
1193/* END_CASE */
1194
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +02001195/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001196void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1197 data_t *input_Q, data_t *input_D,
1198 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001199 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001200{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001201 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001202 unsigned char bufNe[256];
1203 unsigned char bufPe[128];
1204 unsigned char bufQe[128];
1205 unsigned char bufDe[256];
1206 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001207
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001208 mbedtls_rsa_context ctx;
1209
Ronald Cronc1905a12021-06-05 11:11:14 +02001210 mbedtls_rsa_init( &ctx );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001211
1212 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001213 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001214 input_N->len ? input_N->x : NULL, input_N->len,
1215 input_P->len ? input_P->x : NULL, input_P->len,
1216 input_Q->len ? input_Q->x : NULL, input_Q->len,
1217 input_D->len ? input_D->x : NULL, input_D->len,
1218 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001219
Hanno Becker7f25f852017-10-10 16:56:22 +01001220 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001221
1222 /*
1223 * Export parameters and compare to original ones.
1224 */
1225
1226 /* N and E must always be present. */
1227 if( !successive )
1228 {
Azim Khand30ca132017-06-09 04:32:58 +01001229 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001230 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001231 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001232 }
1233 else
1234 {
Azim Khand30ca132017-06-09 04:32:58 +01001235 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001236 NULL, 0, NULL, 0, NULL, 0,
1237 NULL, 0 ) == 0 );
1238 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1239 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001240 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001241 }
Azim Khand30ca132017-06-09 04:32:58 +01001242 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1243 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001244
1245 /* If we were providing enough information to setup a complete private context,
1246 * we expect to be able to export all core parameters. */
1247
1248 if( is_priv )
1249 {
1250 if( !successive )
1251 {
1252 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001253 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1254 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1255 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001256 NULL, 0 ) == 0 );
1257 }
1258 else
1259 {
1260 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001261 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001262 NULL, 0, NULL, 0,
1263 NULL, 0 ) == 0 );
1264
1265 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001266 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001267 NULL, 0, NULL, 0 ) == 0 );
1268
Azim Khand30ca132017-06-09 04:32:58 +01001269 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1270 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001271 NULL, 0 ) == 0 );
1272 }
1273
Azim Khand30ca132017-06-09 04:32:58 +01001274 if( input_P->len )
1275 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001276
Azim Khand30ca132017-06-09 04:32:58 +01001277 if( input_Q->len )
1278 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001279
Azim Khand30ca132017-06-09 04:32:58 +01001280 if( input_D->len )
1281 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001282
1283 }
1284
1285exit:
1286 mbedtls_rsa_free( &ctx );
1287}
1288/* END_CASE */
1289
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001290/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001291void mbedtls_rsa_import_raw( data_t *input_N,
1292 data_t *input_P, data_t *input_Q,
1293 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001294 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001295 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001296 int res_check,
1297 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001298{
Hanno Beckere1582a82017-09-29 11:51:05 +01001299 /* Buffers used for encryption-decryption test */
1300 unsigned char *buf_orig = NULL;
1301 unsigned char *buf_enc = NULL;
1302 unsigned char *buf_dec = NULL;
1303
Hanno Beckerc77ab892017-08-23 11:01:06 +01001304 mbedtls_rsa_context ctx;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001305
Ronald Cronc1905a12021-06-05 11:11:14 +02001306 mbedtls_rsa_init( &ctx );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001307
Hanno Beckerc77ab892017-08-23 11:01:06 +01001308 if( !successive )
1309 {
1310 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001311 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1312 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1313 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1314 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1315 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001316 }
1317 else
1318 {
1319 /* Import N, P, Q, D, E separately.
1320 * This should make no functional difference. */
1321
1322 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001323 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001324 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1325
1326 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1327 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001328 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001329 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1330
1331 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1332 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001333 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001334 NULL, 0, NULL, 0 ) == 0 );
1335
1336 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1337 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001338 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001339 NULL, 0 ) == 0 );
1340
1341 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1342 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001343 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001344 }
1345
Hanno Becker04877a42017-10-11 10:01:33 +01001346 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001347
Hanno Beckere1582a82017-09-29 11:51:05 +01001348 /* On expected success, perform some public and private
1349 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001350 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001351 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001352 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001353 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1354 else
1355 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1356
1357 if( res_check != 0 )
1358 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001359
1360 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1361 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1362 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1363 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1364 goto exit;
1365
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001366 /* This test uses an insecure RNG, suitable only for testing.
1367 * In production, always use a cryptographically strong RNG! */
1368 TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL,
Hanno Beckere1582a82017-09-29 11:51:05 +01001369 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1370
1371 /* Make sure the number we're generating is smaller than the modulus */
1372 buf_orig[0] = 0x00;
1373
1374 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1375
1376 if( is_priv )
1377 {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001378 /* This test uses an insecure RNG, suitable only for testing.
1379 * In production, always use a cryptographically strong RNG! */
1380 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_std_rand,
1381 NULL, buf_enc,
Hanno Beckere1582a82017-09-29 11:51:05 +01001382 buf_dec ) == 0 );
1383
1384 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1385 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1386 }
1387 }
1388
Hanno Beckerc77ab892017-08-23 11:01:06 +01001389exit:
1390
Hanno Becker3f3ae852017-10-02 10:08:39 +01001391 mbedtls_free( buf_orig );
1392 mbedtls_free( buf_enc );
1393 mbedtls_free( buf_dec );
1394
Hanno Beckerc77ab892017-08-23 11:01:06 +01001395 mbedtls_rsa_free( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001396}
1397/* END_CASE */
1398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001400void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001401{
Andres AG93012e82016-09-09 09:10:28 +01001402 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001403}
Paul Bakker33b43f12013-08-20 11:48:36 +02001404/* END_CASE */