blob: 6d1a0f03c1a4f19ffbb62199aca4ac43fc0fc493 [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"
Valerio Setti8e6093d2024-01-23 15:19:07 +01004#include "rsa_internal.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +00006
Paul Bakker33b43f12013-08-20 11:48:36 +02007/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +02009 * END_DEPENDENCIES
10 */
Paul Bakker5690efc2011-05-26 13:16:06 +000011
Paul Bakker33b43f12013-08-20 11:48:36 +020012/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010013void rsa_invalid_param()
Ronald Cronea7631b2021-06-03 18:51:59 +020014{
15 mbedtls_rsa_context ctx;
16 const int invalid_padding = 42;
17 const int invalid_hash_id = 0xff;
Gilles Peskine449bd832023-01-11 14:50:10 +010018 unsigned char buf[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
19 size_t buf_len = sizeof(buf);
Ronald Cronea7631b2021-06-03 18:51:59 +020020
Gilles Peskine449bd832023-01-11 14:50:10 +010021 mbedtls_rsa_init(&ctx);
Ronald Cronea7631b2021-06-03 18:51:59 +020022
Gilles Peskine449bd832023-01-11 14:50:10 +010023 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
24 invalid_padding,
25 MBEDTLS_MD_NONE),
26 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cronea7631b2021-06-03 18:51:59 +020027
Gilles Peskine449bd832023-01-11 14:50:10 +010028 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
29 MBEDTLS_RSA_PKCS_V21,
30 invalid_hash_id),
31 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cronea7631b2021-06-03 18:51:59 +020032
Gilles Peskine449bd832023-01-11 14:50:10 +010033 TEST_EQUAL(mbedtls_rsa_pkcs1_sign(&ctx, NULL,
34 NULL, MBEDTLS_MD_NONE,
35 buf_len,
36 NULL, buf),
37 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010038
Gilles Peskine449bd832023-01-11 14:50:10 +010039 TEST_EQUAL(mbedtls_rsa_pkcs1_sign(&ctx, NULL,
40 NULL, MBEDTLS_MD_SHA256,
41 0,
42 NULL, buf),
43 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010044
Gilles Peskine449bd832023-01-11 14:50:10 +010045 TEST_EQUAL(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE,
46 buf_len,
47 NULL, buf),
48 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 TEST_EQUAL(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_SHA256,
51 0,
52 NULL, buf),
53 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010054
Ronald Cron3a0375f2021-06-08 10:22:28 +020055#if !defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +010056 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
57 MBEDTLS_RSA_PKCS_V15,
58 MBEDTLS_MD_NONE),
59 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cron3a0375f2021-06-08 10:22:28 +020060#endif
61
Tuvshinzaya Erdenekhuufe7524d2022-09-01 16:07:18 +010062#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +010063 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
64 NULL, MBEDTLS_MD_NONE,
65 buf_len,
66 NULL, buf),
67 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
70 NULL, MBEDTLS_MD_SHA256,
71 0,
72 NULL, buf),
73 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010074
Gilles Peskine449bd832023-01-11 14:50:10 +010075 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_NONE,
76 buf_len,
77 NULL, buf),
78 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_SHA256,
81 0,
82 NULL, buf),
83 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010084
85
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010086#endif
87
Ronald Cron3a0375f2021-06-08 10:22:28 +020088#if !defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +010089 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
90 MBEDTLS_RSA_PKCS_V21,
91 MBEDTLS_MD_NONE),
92 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cron3a0375f2021-06-08 10:22:28 +020093#endif
94
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010095#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +010096 TEST_EQUAL(mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
97 MBEDTLS_MD_NONE, buf_len,
98 NULL, buf_len,
99 buf),
100 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 TEST_EQUAL(mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
103 MBEDTLS_MD_SHA256, 0,
104 NULL, buf_len,
105 buf),
106 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_NONE,
109 buf_len, NULL,
110 MBEDTLS_MD_NONE,
111 buf_len, buf),
112 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_SHA256,
115 0, NULL,
116 MBEDTLS_MD_NONE,
117 buf_len, buf),
118 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_NONE,
121 buf_len,
122 NULL, buf),
123 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_SHA256,
126 0,
127 NULL, buf),
128 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100129#endif
130
Ronald Cronea7631b2021-06-03 18:51:59 +0200131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 mbedtls_rsa_free(&ctx);
Ronald Cronea7631b2021-06-03 18:51:59 +0200133}
134/* END_CASE */
135
136/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137void rsa_init_free(int reinit)
Gilles Peskine914afe12021-02-01 17:55:24 +0100138{
139 mbedtls_rsa_context ctx;
140
141 /* Double free is not explicitly documented to work, but we rely on it
142 * even inside the library so that you can call mbedtls_rsa_free()
143 * unconditionally on an error path without checking whether it has
144 * already been called in the success path. */
145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 mbedtls_rsa_init(&ctx);
147 mbedtls_rsa_free(&ctx);
Gilles Peskine914afe12021-02-01 17:55:24 +0100148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (reinit) {
150 mbedtls_rsa_init(&ctx);
151 }
152 mbedtls_rsa_free(&ctx);
Gilles Peskine914afe12021-02-01 17:55:24 +0100153
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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161void mbedtls_rsa_pkcs1_sign(data_t *message_str, int padding_mode,
162 int digest, int mod, char *input_P,
163 char *input_Q, char *input_N, char *input_E,
164 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
172 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
173 mbedtls_rsa_init(&ctx);
174 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
175 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 memset(output, 0x00, sizeof(output));
178 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000179
Gilles Peskine449bd832023-01-11 14:50:10 +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
Gilles Peskine449bd832023-01-11 14:50:10 +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));
187 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
188 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 TEST_ASSERT(mbedtls_rsa_pkcs1_sign(
191 &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
192 digest, message_str->len, message_str->x,
193 output) == result);
194 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
197 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000198 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000199
Paul Bakkerbd51b262014-07-10 15:26:12 +0200200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
202 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
203 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000204}
Paul Bakker33b43f12013-08-20 11:48:36 +0200205/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000206
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200207/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100208void mbedtls_rsa_pkcs1_verify(data_t *message_str, int padding_mode,
209 int digest, int mod,
210 char *input_N, char *input_E,
211 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100214 mbedtls_mpi N, E;
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
217 mbedtls_rsa_init(&ctx);
218 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
219 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
222 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
223 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
224 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
225 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 TEST_ASSERT(mbedtls_rsa_pkcs1_verify(&ctx, digest, message_str->len, message_str->x,
228 result_str->x) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100229
Paul Bakkerbd51b262014-07-10 15:26:12 +0200230exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
232 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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238void rsa_pkcs1_sign_raw(data_t *hash_result,
239 int padding_mode, int mod,
240 char *input_P, char *input_Q,
241 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 mbedtls_rsa_init(&ctx);
250 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
254 MBEDTLS_MD_NONE) == 0);
Paul Elliotte57dd2d2021-06-25 11:13:24 +0100255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 memset(output, 0x00, sizeof(output));
257 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000258
Gilles Peskine449bd832023-01-11 14:50:10 +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
Gilles Peskine449bd832023-01-11 14:50:10 +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));
266 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
267 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000268
Paul Bakker821fb082009-07-12 13:26:42 +0000269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 TEST_ASSERT(mbedtls_rsa_pkcs1_sign(&ctx, &mbedtls_test_rnd_pseudo_rand,
271 &rnd_info, MBEDTLS_MD_NONE,
272 hash_result->len,
273 hash_result->x, output) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000274
Paul Bakker821fb082009-07-12 13:26:42 +0000275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 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:
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
281 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288void rsa_pkcs1_verify_raw(data_t *hash_result,
289 int padding_mode, int mod,
290 char *input_N, char *input_E,
291 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;
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 mbedtls_rsa_init(&ctx);
300 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
301 MBEDTLS_MD_NONE) == 0);
302 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000303
Gilles Peskine449bd832023-01-11 14:50:10 +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
Gilles Peskine449bd832023-01-11 14:50:10 +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));
309 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000310
Paul Bakker821fb082009-07-12 13:26:42 +0000311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 TEST_ASSERT(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE, hash_result->len, hash_result->x,
313 result_str->x) == correct);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100314
Paul Bakkerbd51b262014-07-10 15:26:12 +0200315exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
317 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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322void mbedtls_rsa_pkcs1_encrypt(data_t *message_str, int padding_mode,
323 int mod, char *input_N, char *input_E,
324 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;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker821fb082009-07-12 13:26:42 +0000334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 mbedtls_rsa_init(&ctx);
336 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
337 MBEDTLS_MD_NONE) == 0);
338 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000339
Gilles Peskine449bd832023-01-11 14:50:10 +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
Gilles Peskine449bd832023-01-11 14:50:10 +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));
345 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000346
Paul Bakker42a29bf2009-07-07 20:18:41 +0000347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 TEST_ASSERT(mbedtls_rsa_pkcs1_encrypt(&ctx,
349 &mbedtls_test_rnd_pseudo_rand,
350 &rnd_info, message_str->len,
351 message_str->x,
352 output) == result);
353 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 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:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
361 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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366void rsa_pkcs1_encrypt_bad_rng(data_t *message_str, int padding_mode,
367 int mod, char *input_N, char *input_E,
368 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
376 mbedtls_rsa_init(&ctx);
377 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
378 MBEDTLS_MD_NONE) == 0);
379 memset(output, 0x00, sizeof(output));
Paul Bakkera6656852010-07-18 19:47:14 +0000380
Gilles Peskine449bd832023-01-11 14:50:10 +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
Gilles Peskine449bd832023-01-11 14:50:10 +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));
386 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000387
Paul Bakkera6656852010-07-18 19:47:14 +0000388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 TEST_ASSERT(mbedtls_rsa_pkcs1_encrypt(&ctx, &mbedtls_test_rnd_zero_rand,
390 NULL, message_str->len,
391 message_str->x,
392 output) == result);
393 if (result == 0) {
Paul Bakkera6656852010-07-18 19:47:14 +0000394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
396 ctx.len, result_str->len) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000397 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100398
Paul Bakkerbd51b262014-07-10 15:26:12 +0200399exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
401 mbedtls_rsa_free(&ctx);
Paul Bakkera6656852010-07-18 19:47:14 +0000402}
Paul Bakker33b43f12013-08-20 11:48:36 +0200403/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000404
Paul Bakker33b43f12013-08-20 11:48:36 +0200405/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100406void mbedtls_rsa_pkcs1_decrypt(data_t *message_str, int padding_mode,
407 int mod, char *input_P,
408 char *input_Q, char *input_N,
409 char *input_E, int max_output,
410 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000411{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200412 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000414 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200415 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100416 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
419 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 mbedtls_rsa_init(&ctx);
422 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
423 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 memset(output, 0x00, sizeof(output));
426 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000427
Paul Bakker42a29bf2009-07-07 20:18:41 +0000428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
430 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
431 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
432 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
435 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
436 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
437 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000438
Paul Bakker69998dd2009-07-11 19:15:20 +0000439 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 TEST_ASSERT(mbedtls_rsa_pkcs1_decrypt(&ctx, mbedtls_test_rnd_pseudo_rand,
442 &rnd_info,
443 &output_len, message_str->x, output,
444 max_output) == result);
445 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
448 output_len,
449 result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000450 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000451
Paul Bakkerbd51b262014-07-10 15:26:12 +0200452exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
454 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
455 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000456}
Paul Bakker33b43f12013-08-20 11:48:36 +0200457/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000458
Paul Bakker33b43f12013-08-20 11:48:36 +0200459/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460void mbedtls_rsa_public(data_t *message_str, int mod,
461 char *input_N, char *input_E,
462 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000463{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200464 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000466
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100467 mbedtls_mpi N, E;
468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
470 mbedtls_rsa_init(&ctx);
471 mbedtls_rsa_init(&ctx2);
472 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
475 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
Gilles Peskine058d0092021-06-09 16:24:35 +0200478
479 /* Check test data consistency */
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 TEST_ASSERT(message_str->len == (size_t) (mod / 8));
481 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
482 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 TEST_ASSERT(mbedtls_rsa_public(&ctx, message_str->x, output) == result);
485 if (result == 0) {
Paul Bakker821fb082009-07-12 13:26:42 +0000486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
488 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000489 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100490
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100491 /* And now with the copy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 TEST_ASSERT(mbedtls_rsa_copy(&ctx2, &ctx) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200493 /* clear the original to be sure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 mbedtls_rsa_free(&ctx);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx2) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 memset(output, 0x00, sizeof(output));
499 TEST_ASSERT(mbedtls_rsa_public(&ctx2, message_str->x, output) == result);
500 if (result == 0) {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
503 ctx.len, result_str->len) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100504 }
505
Paul Bakkerbd51b262014-07-10 15:26:12 +0200506exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
508 mbedtls_rsa_free(&ctx);
509 mbedtls_rsa_free(&ctx2);
Paul Bakker821fb082009-07-12 13:26:42 +0000510}
Paul Bakker33b43f12013-08-20 11:48:36 +0200511/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000512
Paul Bakker33b43f12013-08-20 11:48:36 +0200513/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514void mbedtls_rsa_private(data_t *message_str, int mod,
515 char *input_P, char *input_Q,
516 char *input_N, char *input_E,
517 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000518{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200519 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100521 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200522 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200523 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
526 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
527 mbedtls_rsa_init(&ctx);
528 mbedtls_rsa_init(&ctx2);
Paul Bakker821fb082009-07-12 13:26:42 +0000529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker821fb082009-07-12 13:26:42 +0000531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
533 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
534 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
535 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
Gilles Peskine058d0092021-06-09 16:24:35 +0200538
539 /* Check test data consistency */
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 TEST_ASSERT(message_str->len == (size_t) (mod / 8));
541 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
542 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
543 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000544
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200545 /* repeat three times to test updating of blinding values */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 for (i = 0; i < 3; i++) {
547 memset(output, 0x00, sizeof(output));
548 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_pseudo_rand,
549 &rnd_info, message_str->x,
550 output) == result);
551 if (result == 0) {
Paul Bakker821fb082009-07-12 13:26:42 +0000552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
554 ctx.len,
555 result_str->len) == 0);
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200556 }
Paul Bakker821fb082009-07-12 13:26:42 +0000557 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000558
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100559 /* And now one more time with the copy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 TEST_ASSERT(mbedtls_rsa_copy(&ctx2, &ctx) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200561 /* clear the original to be sure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_rsa_free(&ctx);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx2) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 memset(output, 0x00, sizeof(output));
567 TEST_ASSERT(mbedtls_rsa_private(&ctx2, mbedtls_test_rnd_pseudo_rand,
568 &rnd_info, message_str->x,
569 output) == result);
570 if (result == 0) {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
573 ctx2.len,
574 result_str->len) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100575 }
576
Paul Bakkerbd51b262014-07-10 15:26:12 +0200577exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
579 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 mbedtls_rsa_free(&ctx); mbedtls_rsa_free(&ctx2);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000582}
Paul Bakker33b43f12013-08-20 11:48:36 +0200583/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000584
Paul Bakker33b43f12013-08-20 11:48:36 +0200585/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100586void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000587{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_rsa_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 memset(&ctx, 0x00, sizeof(mbedtls_rsa_context));
Paul Bakker37940d9f2009-07-10 22:38:58 +0000590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000592}
Paul Bakker33b43f12013-08-20 11:48:36 +0200593/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000594
Paul Bakker33b43f12013-08-20 11:48:36 +0200595/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100596void mbedtls_rsa_check_pubkey(char *input_N, char *input_E, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000597{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100599 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
602 mbedtls_rsa_init(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (strlen(input_N)) {
605 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000606 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if (strlen(input_E)) {
608 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000609 }
610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
612 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100613
Paul Bakkerbd51b262014-07-10 15:26:12 +0200614exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
616 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000617}
Paul Bakker33b43f12013-08-20 11:48:36 +0200618/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000619
Paul Bakker33b43f12013-08-20 11:48:36 +0200620/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621void mbedtls_rsa_check_privkey(int mod, char *input_P, char *input_Q,
622 char *input_N, char *input_E, char *input_D,
623 char *input_DP, char *input_DQ, char *input_QP,
624 int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 mbedtls_rsa_init(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000629
Paul Bakker33b43f12013-08-20 11:48:36 +0200630 ctx.len = mod / 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (strlen(input_P)) {
632 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.P, input_P) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000633 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (strlen(input_Q)) {
635 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.Q, input_Q) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000636 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 if (strlen(input_N)) {
638 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.N, input_N) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000639 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (strlen(input_E)) {
641 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000642 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (strlen(input_D)) {
644 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.D, input_D) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000645 }
Hanno Becker131134f2017-08-23 08:31:07 +0100646#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (strlen(input_DP)) {
648 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.DP, input_DP) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000649 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (strlen(input_DQ)) {
651 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.DQ, input_DQ) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000652 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (strlen(input_QP)) {
654 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.QP, input_QP) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000655 }
Hanno Becker131134f2017-08-23 08:31:07 +0100656#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100657 ((void) input_DP);
658 ((void) input_DQ);
659 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100660#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100663
Paul Bakkerbd51b262014-07-10 15:26:12 +0200664exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000666}
Paul Bakker33b43f12013-08-20 11:48:36 +0200667/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000668
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100669/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670void rsa_check_pubpriv(int mod, char *input_Npub, char *input_Epub,
671 char *input_P, char *input_Q, char *input_N,
672 char *input_E, char *input_D, char *input_DP,
673 char *input_DQ, char *input_QP, int result)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100674{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 mbedtls_rsa_init(&pub);
678 mbedtls_rsa_init(&prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100679
680 pub.len = mod / 8;
681 prv.len = mod / 8;
682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (strlen(input_Npub)) {
684 TEST_ASSERT(mbedtls_test_read_mpi(&pub.N, input_Npub) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100685 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (strlen(input_Epub)) {
687 TEST_ASSERT(mbedtls_test_read_mpi(&pub.E, input_Epub) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100688 }
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (strlen(input_P)) {
691 TEST_ASSERT(mbedtls_test_read_mpi(&prv.P, input_P) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100692 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (strlen(input_Q)) {
694 TEST_ASSERT(mbedtls_test_read_mpi(&prv.Q, input_Q) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100695 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (strlen(input_N)) {
697 TEST_ASSERT(mbedtls_test_read_mpi(&prv.N, input_N) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100698 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (strlen(input_E)) {
700 TEST_ASSERT(mbedtls_test_read_mpi(&prv.E, input_E) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100701 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (strlen(input_D)) {
703 TEST_ASSERT(mbedtls_test_read_mpi(&prv.D, input_D) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100704 }
Hanno Becker131134f2017-08-23 08:31:07 +0100705#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (strlen(input_DP)) {
707 TEST_ASSERT(mbedtls_test_read_mpi(&prv.DP, input_DP) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (strlen(input_DQ)) {
710 TEST_ASSERT(mbedtls_test_read_mpi(&prv.DQ, input_DQ) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100711 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (strlen(input_QP)) {
713 TEST_ASSERT(mbedtls_test_read_mpi(&prv.QP, input_QP) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100714 }
Hanno Becker131134f2017-08-23 08:31:07 +0100715#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100716 ((void) input_DP);
717 ((void) input_DQ);
718 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100719#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 TEST_ASSERT(mbedtls_rsa_check_pub_priv(&pub, &prv) == result);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100722
723exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 mbedtls_rsa_free(&pub);
725 mbedtls_rsa_free(&prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100726}
727/* END_CASE */
728
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200729/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730void mbedtls_rsa_gen_key(int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000731{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_rsa_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 mbedtls_rsa_init(&ctx);
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000734
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200735 /* This test uses an insecure RNG, suitable only for testing.
736 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 TEST_ASSERT(mbedtls_rsa_gen_key(&ctx, mbedtls_test_rnd_std_rand, NULL, nrbits,
738 exponent) == result);
739 if (result == 0) {
740 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
741 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&ctx.P, &ctx.Q) > 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000742 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100743
Paul Bakkerbd51b262014-07-10 15:26:12 +0200744exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000746}
Paul Bakker33b43f12013-08-20 11:48:36 +0200747/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000748
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +0200749/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750void mbedtls_rsa_deduce_primes(char *input_N,
751 char *input_D,
752 char *input_E,
753 char *output_P,
754 char *output_Q,
755 int corrupt, int result)
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100756{
757 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 mbedtls_mpi_init(&N);
760 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
761 mbedtls_mpi_init(&Pp); mbedtls_mpi_init(&Qp);
762 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
765 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
766 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
767 TEST_ASSERT(mbedtls_test_read_mpi(&Qp, output_P) == 0);
768 TEST_ASSERT(mbedtls_test_read_mpi(&Pp, output_Q) == 0);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (corrupt) {
771 TEST_ASSERT(mbedtls_mpi_add_int(&D, &D, 2) == 0);
772 }
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100773
774 /* Try to deduce P, Q from N, D, E only. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 TEST_ASSERT(mbedtls_rsa_deduce_primes(&N, &D, &E, &P, &Q) == result);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (!corrupt) {
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100778 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 TEST_ASSERT((mbedtls_mpi_cmp_mpi(&P, &Pp) == 0 && mbedtls_mpi_cmp_mpi(&Q, &Qp) == 0) ||
780 (mbedtls_mpi_cmp_mpi(&P, &Qp) == 0 && mbedtls_mpi_cmp_mpi(&Q, &Pp) == 0));
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100781 }
782
783exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 mbedtls_mpi_free(&N);
785 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
786 mbedtls_mpi_free(&Pp); mbedtls_mpi_free(&Qp);
787 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100788}
789/* END_CASE */
790
Hanno Becker6b4ce492017-08-23 11:00:21 +0100791/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792void mbedtls_rsa_deduce_private_exponent(char *input_P,
793 char *input_Q,
794 char *input_E,
795 char *output_D,
796 int corrupt, int result)
Hanno Becker6b4ce492017-08-23 11:00:21 +0100797{
798 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
801 mbedtls_mpi_init(&D); mbedtls_mpi_init(&Dp);
802 mbedtls_mpi_init(&E);
803 mbedtls_mpi_init(&R); mbedtls_mpi_init(&Rp);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
806 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
807 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
808 TEST_ASSERT(mbedtls_test_read_mpi(&Dp, output_D) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (corrupt) {
Hanno Becker6b4ce492017-08-23 11:00:21 +0100811 /* Make E even */
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 TEST_ASSERT(mbedtls_mpi_set_bit(&E, 0, 0) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100813 }
814
815 /* Try to deduce D from N, P, Q, E. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 TEST_ASSERT(mbedtls_rsa_deduce_private_exponent(&P, &Q,
817 &E, &D) == result);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (!corrupt) {
Hanno Becker6b4ce492017-08-23 11:00:21 +0100820 /*
821 * Check that D and Dp agree modulo LCM(P-1, Q-1).
822 */
823
824 /* Replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 TEST_ASSERT(mbedtls_mpi_sub_int(&P, &P, 1) == 0);
826 TEST_ASSERT(mbedtls_mpi_sub_int(&Q, &Q, 1) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100827
828 /* Check D == Dp modulo P-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &D, &P) == 0);
830 TEST_ASSERT(mbedtls_mpi_mod_mpi(&Rp, &Dp, &P) == 0);
831 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &Rp) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100832
833 /* Check D == Dp modulo Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &D, &Q) == 0);
835 TEST_ASSERT(mbedtls_mpi_mod_mpi(&Rp, &Dp, &Q) == 0);
836 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &Rp) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100837 }
838
839exit:
840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
842 mbedtls_mpi_free(&D); mbedtls_mpi_free(&Dp);
843 mbedtls_mpi_free(&E);
844 mbedtls_mpi_free(&R); mbedtls_mpi_free(&Rp);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100845}
846/* END_CASE */
847
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200848/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100849void mbedtls_rsa_import(char *input_N,
850 char *input_P,
851 char *input_Q,
852 char *input_D,
853 char *input_E,
854 int successive,
855 int is_priv,
856 int res_check,
857 int res_complete)
Hanno Beckerc77ab892017-08-23 11:01:06 +0100858{
859 mbedtls_mpi N, P, Q, D, E;
860 mbedtls_rsa_context ctx;
861
Hanno Beckere1582a82017-09-29 11:51:05 +0100862 /* Buffers used for encryption-decryption test */
863 unsigned char *buf_orig = NULL;
864 unsigned char *buf_enc = NULL;
865 unsigned char *buf_dec = NULL;
866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 const int have_N = (strlen(input_N) > 0);
868 const int have_P = (strlen(input_P) > 0);
869 const int have_Q = (strlen(input_Q) > 0);
870 const int have_D = (strlen(input_D) > 0);
871 const int have_E = (strlen(input_E) > 0);
Hanno Becker4d6e8342017-09-29 11:50:18 +0100872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_rsa_init(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 mbedtls_mpi_init(&N);
876 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
877 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if (have_N) {
880 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100881 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100882
883 if (have_P) {
884 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
885 }
886
887 if (have_Q) {
888 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
889 }
890
891 if (have_D) {
892 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
893 }
894
895 if (have_E) {
896 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
897 }
898
899 if (!successive) {
900 TEST_ASSERT(mbedtls_rsa_import(&ctx,
901 have_N ? &N : NULL,
902 have_P ? &P : NULL,
903 have_Q ? &Q : NULL,
904 have_D ? &D : NULL,
905 have_E ? &E : NULL) == 0);
906 } else {
Hanno Beckerc77ab892017-08-23 11:01:06 +0100907 /* Import N, P, Q, D, E separately.
908 * This should make no functional difference. */
909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 TEST_ASSERT(mbedtls_rsa_import(&ctx,
911 have_N ? &N : NULL,
912 NULL, NULL, NULL, NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 TEST_ASSERT(mbedtls_rsa_import(&ctx,
915 NULL,
916 have_P ? &P : NULL,
917 NULL, NULL, NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 TEST_ASSERT(mbedtls_rsa_import(&ctx,
920 NULL, NULL,
921 have_Q ? &Q : NULL,
922 NULL, NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 TEST_ASSERT(mbedtls_rsa_import(&ctx,
925 NULL, NULL, NULL,
926 have_D ? &D : NULL,
927 NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 TEST_ASSERT(mbedtls_rsa_import(&ctx,
930 NULL, NULL, NULL, NULL,
931 have_E ? &E : NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100932 }
933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == res_complete);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100935
Hanno Beckere1582a82017-09-29 11:51:05 +0100936 /* On expected success, perform some public and private
937 * key operations to check if the key is working properly. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (res_complete == 0) {
939 if (is_priv) {
940 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == res_check);
941 } else {
942 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == res_check);
943 }
Hanno Becker04877a42017-10-11 10:01:33 +0100944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (res_check != 0) {
Hanno Becker04877a42017-10-11 10:01:33 +0100946 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 }
Hanno Beckere1582a82017-09-29 11:51:05 +0100948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 buf_orig = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
950 buf_enc = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
951 buf_dec = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
952 if (buf_orig == NULL || buf_enc == NULL || buf_dec == NULL) {
Hanno Beckere1582a82017-09-29 11:51:05 +0100953 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 }
Hanno Beckere1582a82017-09-29 11:51:05 +0100955
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200956 /* This test uses an insecure RNG, suitable only for testing.
957 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL,
959 buf_orig, mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100960
961 /* Make sure the number we're generating is smaller than the modulus */
962 buf_orig[0] = 0x00;
963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 TEST_ASSERT(mbedtls_rsa_public(&ctx, buf_orig, buf_enc) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if (is_priv) {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200967 /* This test uses an insecure RNG, suitable only for testing.
968 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_std_rand,
970 NULL, buf_enc,
971 buf_dec) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 TEST_ASSERT(memcmp(buf_orig, buf_dec,
974 mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100975 }
976 }
977
Hanno Beckerc77ab892017-08-23 11:01:06 +0100978exit:
979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 mbedtls_free(buf_orig);
981 mbedtls_free(buf_enc);
982 mbedtls_free(buf_dec);
Hanno Beckere1582a82017-09-29 11:51:05 +0100983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 mbedtls_rsa_free(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 mbedtls_mpi_free(&N);
987 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
988 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100989}
990/* END_CASE */
991
Hanno Becker417f2d62017-08-23 11:44:51 +0100992/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993void mbedtls_rsa_export(char *input_N,
994 char *input_P,
995 char *input_Q,
996 char *input_D,
997 char *input_E,
998 int is_priv,
999 int successive)
Hanno Becker417f2d62017-08-23 11:44:51 +01001000{
1001 /* Original MPI's with which we set up the RSA context */
1002 mbedtls_mpi N, P, Q, D, E;
1003
1004 /* Exported MPI's */
1005 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 const int have_N = (strlen(input_N) > 0);
1008 const int have_P = (strlen(input_P) > 0);
1009 const int have_Q = (strlen(input_Q) > 0);
1010 const int have_D = (strlen(input_D) > 0);
1011 const int have_E = (strlen(input_E) > 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001012
Hanno Becker417f2d62017-08-23 11:44:51 +01001013 mbedtls_rsa_context ctx;
1014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 mbedtls_rsa_init(&ctx);
Hanno Becker417f2d62017-08-23 11:44:51 +01001016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 mbedtls_mpi_init(&N);
1018 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
1019 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Becker417f2d62017-08-23 11:44:51 +01001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 mbedtls_mpi_init(&Ne);
1022 mbedtls_mpi_init(&Pe); mbedtls_mpi_init(&Qe);
1023 mbedtls_mpi_init(&De); mbedtls_mpi_init(&Ee);
Hanno Becker417f2d62017-08-23 11:44:51 +01001024
1025 /* Setup RSA context */
1026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (have_N) {
1028 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
1029 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (have_P) {
1032 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
1033 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if (have_Q) {
1036 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
1037 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (have_D) {
1040 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
1041 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if (have_E) {
1044 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
1045 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 TEST_ASSERT(mbedtls_rsa_import(&ctx,
1048 strlen(input_N) ? &N : NULL,
1049 strlen(input_P) ? &P : NULL,
1050 strlen(input_Q) ? &Q : NULL,
1051 strlen(input_D) ? &D : NULL,
1052 strlen(input_E) ? &E : NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001055
1056 /*
1057 * Export parameters and compare to original ones.
1058 */
1059
1060 /* N and E must always be present. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (!successive) {
1062 TEST_ASSERT(mbedtls_rsa_export(&ctx, &Ne, NULL, NULL, NULL, &Ee) == 0);
1063 } else {
1064 TEST_ASSERT(mbedtls_rsa_export(&ctx, &Ne, NULL, NULL, NULL, NULL) == 0);
1065 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, NULL, NULL, &Ee) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001066 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&N, &Ne) == 0);
1068 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&E, &Ee) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001069
1070 /* If we were providing enough information to setup a complete private context,
1071 * we expect to be able to export all core parameters. */
1072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if (is_priv) {
1074 if (!successive) {
1075 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, &Pe, &Qe,
1076 &De, NULL) == 0);
1077 } else {
1078 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, &Pe, NULL,
1079 NULL, NULL) == 0);
1080 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, &Qe,
1081 NULL, NULL) == 0);
1082 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, NULL,
1083 &De, NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001084 }
1085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (have_P) {
1087 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P, &Pe) == 0);
1088 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if (have_Q) {
1091 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Q, &Qe) == 0);
1092 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if (have_D) {
1095 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&D, &De) == 0);
1096 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001097
1098 /* While at it, perform a sanity check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 TEST_ASSERT(mbedtls_rsa_validate_params(&Ne, &Pe, &Qe, &De, &Ee,
1100 NULL, NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001101 }
1102
1103exit:
1104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 mbedtls_rsa_free(&ctx);
Hanno Becker417f2d62017-08-23 11:44:51 +01001106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 mbedtls_mpi_free(&N);
1108 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
1109 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Becker417f2d62017-08-23 11:44:51 +01001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 mbedtls_mpi_free(&Ne);
1112 mbedtls_mpi_free(&Pe); mbedtls_mpi_free(&Qe);
1113 mbedtls_mpi_free(&De); mbedtls_mpi_free(&Ee);
Hanno Becker417f2d62017-08-23 11:44:51 +01001114}
1115/* END_CASE */
1116
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001117/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118void mbedtls_rsa_validate_params(char *input_N,
1119 char *input_P,
1120 char *input_Q,
1121 char *input_D,
1122 char *input_E,
1123 int prng, int result)
Hanno Beckerce002632017-08-23 13:22:36 +01001124{
1125 /* Original MPI's with which we set up the RSA context */
1126 mbedtls_mpi N, P, Q, D, E;
1127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 const int have_N = (strlen(input_N) > 0);
1129 const int have_P = (strlen(input_P) > 0);
1130 const int have_Q = (strlen(input_Q) > 0);
1131 const int have_D = (strlen(input_D) > 0);
1132 const int have_E = (strlen(input_E) > 0);
Hanno Beckerce002632017-08-23 13:22:36 +01001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 mbedtls_mpi_init(&N);
1135 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
1136 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerce002632017-08-23 13:22:36 +01001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 if (have_N) {
1139 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
1140 }
Hanno Beckerce002632017-08-23 13:22:36 +01001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (have_P) {
1143 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
1144 }
Hanno Beckerce002632017-08-23 13:22:36 +01001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (have_Q) {
1147 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
1148 }
Hanno Beckerce002632017-08-23 13:22:36 +01001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if (have_D) {
1151 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
1152 }
Hanno Beckerce002632017-08-23 13:22:36 +01001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (have_E) {
1155 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
1156 }
Hanno Beckerce002632017-08-23 13:22:36 +01001157
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001158 /* This test uses an insecure RNG, suitable only for testing.
1159 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 TEST_ASSERT(mbedtls_rsa_validate_params(have_N ? &N : NULL,
1161 have_P ? &P : NULL,
1162 have_Q ? &Q : NULL,
1163 have_D ? &D : NULL,
1164 have_E ? &E : NULL,
1165 prng ? mbedtls_test_rnd_std_rand : NULL,
1166 prng ? NULL : NULL) == result);
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001167
Hanno Beckerce002632017-08-23 13:22:36 +01001168exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 mbedtls_mpi_free(&N);
1170 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
1171 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerce002632017-08-23 13:22:36 +01001172}
1173/* END_CASE */
1174
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +02001175/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001176void mbedtls_rsa_export_raw(data_t *input_N, data_t *input_P,
1177 data_t *input_Q, data_t *input_D,
1178 data_t *input_E, int is_priv,
1179 int successive)
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001180{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001181 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001182 unsigned char bufNe[256];
1183 unsigned char bufPe[128];
1184 unsigned char bufQe[128];
1185 unsigned char bufDe[256];
1186 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001187
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001188 mbedtls_rsa_context ctx;
1189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 mbedtls_rsa_init(&ctx);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001191
1192 /* Setup RSA context */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1194 input_N->len ? input_N->x : NULL, input_N->len,
1195 input_P->len ? input_P->x : NULL, input_P->len,
1196 input_Q->len ? input_Q->x : NULL, input_Q->len,
1197 input_D->len ? input_D->x : NULL, input_D->len,
1198 input_E->len ? input_E->x : NULL, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001201
1202 /*
1203 * Export parameters and compare to original ones.
1204 */
1205
1206 /* N and E must always be present. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (!successive) {
1208 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, bufNe, input_N->len,
1209 NULL, 0, NULL, 0, NULL, 0,
1210 bufEe, input_E->len) == 0);
1211 } else {
1212 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, bufNe, input_N->len,
1213 NULL, 0, NULL, 0, NULL, 0,
1214 NULL, 0) == 0);
1215 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1216 NULL, 0, NULL, 0, NULL, 0,
1217 bufEe, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001218 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 TEST_ASSERT(memcmp(input_N->x, bufNe, input_N->len) == 0);
1220 TEST_ASSERT(memcmp(input_E->x, bufEe, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001221
1222 /* If we were providing enough information to setup a complete private context,
1223 * we expect to be able to export all core parameters. */
1224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (is_priv) {
1226 if (!successive) {
1227 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1228 bufPe, input_P->len ? input_P->len : sizeof(bufPe),
1229 bufQe, input_Q->len ? input_Q->len : sizeof(bufQe),
1230 bufDe, input_D->len ? input_D->len : sizeof(bufDe),
1231 NULL, 0) == 0);
1232 } else {
1233 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1234 bufPe, input_P->len ? input_P->len : sizeof(bufPe),
1235 NULL, 0, NULL, 0,
1236 NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0, NULL, 0,
1239 bufQe, input_Q->len ? input_Q->len : sizeof(bufQe),
1240 NULL, 0, NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0, NULL, 0, NULL, 0,
1243 bufDe, input_D->len ? input_D->len : sizeof(bufDe),
1244 NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001245 }
1246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 if (input_P->len) {
1248 TEST_ASSERT(memcmp(input_P->x, bufPe, input_P->len) == 0);
1249 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (input_Q->len) {
1252 TEST_ASSERT(memcmp(input_Q->x, bufQe, input_Q->len) == 0);
1253 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 if (input_D->len) {
1256 TEST_ASSERT(memcmp(input_D->x, bufDe, input_D->len) == 0);
1257 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001258
1259 }
1260
1261exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 mbedtls_rsa_free(&ctx);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001263}
1264/* END_CASE */
1265
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001266/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001267void mbedtls_rsa_import_raw(data_t *input_N,
1268 data_t *input_P, data_t *input_Q,
1269 data_t *input_D, data_t *input_E,
1270 int successive,
1271 int is_priv,
1272 int res_check,
1273 int res_complete)
Hanno Beckerc77ab892017-08-23 11:01:06 +01001274{
Hanno Beckere1582a82017-09-29 11:51:05 +01001275 /* Buffers used for encryption-decryption test */
1276 unsigned char *buf_orig = NULL;
1277 unsigned char *buf_enc = NULL;
1278 unsigned char *buf_dec = NULL;
1279
Hanno Beckerc77ab892017-08-23 11:01:06 +01001280 mbedtls_rsa_context ctx;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 mbedtls_rsa_init(&ctx);
Hanno Becker3f3ae852017-10-02 10:08:39 +01001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (!successive) {
1285 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1286 (input_N->len > 0) ? input_N->x : NULL, input_N->len,
1287 (input_P->len > 0) ? input_P->x : NULL, input_P->len,
1288 (input_Q->len > 0) ? input_Q->x : NULL, input_Q->len,
1289 (input_D->len > 0) ? input_D->x : NULL, input_D->len,
1290 (input_E->len > 0) ? input_E->x : NULL,
1291 input_E->len) == 0);
1292 } else {
Hanno Beckerc77ab892017-08-23 11:01:06 +01001293 /* Import N, P, Q, D, E separately.
1294 * This should make no functional difference. */
1295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1297 (input_N->len > 0) ? input_N->x : NULL, input_N->len,
1298 NULL, 0, NULL, 0, NULL, 0, NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1301 NULL, 0,
1302 (input_P->len > 0) ? input_P->x : NULL, input_P->len,
1303 NULL, 0, NULL, 0, NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1306 NULL, 0, NULL, 0,
1307 (input_Q->len > 0) ? input_Q->x : NULL, input_Q->len,
1308 NULL, 0, NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1311 NULL, 0, NULL, 0, NULL, 0,
1312 (input_D->len > 0) ? input_D->x : NULL, input_D->len,
1313 NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1316 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1317 (input_E->len > 0) ? input_E->x : NULL,
1318 input_E->len) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001319 }
1320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == res_complete);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001322
Hanno Beckere1582a82017-09-29 11:51:05 +01001323 /* On expected success, perform some public and private
1324 * key operations to check if the key is working properly. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 if (res_complete == 0) {
1326 if (is_priv) {
1327 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == res_check);
1328 } else {
1329 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == res_check);
1330 }
Hanno Becker04877a42017-10-11 10:01:33 +01001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if (res_check != 0) {
Hanno Becker04877a42017-10-11 10:01:33 +01001333 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 }
Hanno Beckere1582a82017-09-29 11:51:05 +01001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 buf_orig = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1337 buf_enc = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1338 buf_dec = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1339 if (buf_orig == NULL || buf_enc == NULL || buf_dec == NULL) {
Hanno Beckere1582a82017-09-29 11:51:05 +01001340 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 }
Hanno Beckere1582a82017-09-29 11:51:05 +01001342
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001343 /* This test uses an insecure RNG, suitable only for testing.
1344 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL,
1346 buf_orig, mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001347
1348 /* Make sure the number we're generating is smaller than the modulus */
1349 buf_orig[0] = 0x00;
1350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 TEST_ASSERT(mbedtls_rsa_public(&ctx, buf_orig, buf_enc) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (is_priv) {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001354 /* This test uses an insecure RNG, suitable only for testing.
1355 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_std_rand,
1357 NULL, buf_enc,
1358 buf_dec) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 TEST_ASSERT(memcmp(buf_orig, buf_dec,
1361 mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001362 }
1363 }
1364
Hanno Beckerc77ab892017-08-23 11:01:06 +01001365exit:
1366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 mbedtls_free(buf_orig);
1368 mbedtls_free(buf_enc);
1369 mbedtls_free(buf_dec);
Hanno Becker3f3ae852017-10-02 10:08:39 +01001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 mbedtls_rsa_free(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001372}
1373/* END_CASE */
1374
Valerio Setti8e6093d2024-01-23 15:19:07 +01001375/* BEGIN_CASE */
Valerio Setti6d597f12024-01-24 13:44:41 +01001376void rsa_parse_pkcs1_key(int is_public, data_t *input, int exp_ret_val)
Valerio Setti6def24c2024-01-24 12:33:04 +01001377{
1378 mbedtls_rsa_context rsa_ctx;
Valerio Setti6d597f12024-01-24 13:44:41 +01001379
Valerio Setti6def24c2024-01-24 12:33:04 +01001380 mbedtls_rsa_init(&rsa_ctx);
1381
Valerio Setti6d597f12024-01-24 13:44:41 +01001382 if (is_public) {
Valerio Setti201e6432024-02-01 17:19:37 +01001383 TEST_EQUAL(mbedtls_rsa_parse_pubkey(&rsa_ctx, input->x, input->len), exp_ret_val);
Valerio Setti6d597f12024-01-24 13:44:41 +01001384 } else {
Valerio Setti135ebde2024-02-01 17:00:29 +01001385 TEST_EQUAL(mbedtls_rsa_parse_key(&rsa_ctx, input->x, input->len), exp_ret_val);
Valerio Setti6d597f12024-01-24 13:44:41 +01001386 }
Valerio Setti6def24c2024-01-24 12:33:04 +01001387
1388exit:
1389 mbedtls_rsa_free(&rsa_ctx);
1390}
1391/* END_CASE */
1392
1393/* BEGIN_CASE */
Valerio Setti1533c3f2024-01-24 11:24:20 +01001394void rsa_parse_write_pkcs1_key(int is_public, data_t *input)
Valerio Setti8e6093d2024-01-23 15:19:07 +01001395{
1396 mbedtls_rsa_context rsa_ctx;
Valerio Setti8e6093d2024-01-23 15:19:07 +01001397 unsigned char *output_buf = NULL;
Valerio Setti56cfe2f2024-02-01 17:53:26 +01001398 unsigned char *output_end, *output_p;
1399 size_t output_len;
Valerio Setti8e6093d2024-01-23 15:19:07 +01001400
1401 mbedtls_rsa_init(&rsa_ctx);
1402
Valerio Setti56cfe2f2024-02-01 17:53:26 +01001403 TEST_CALLOC(output_buf, input->len);
1404 output_end = output_buf + input->len;
1405 output_p = output_end;
1406
Valerio Setti1533c3f2024-01-24 11:24:20 +01001407 /* Parse the key and write it back to output_buf. */
Valerio Setti8e6093d2024-01-23 15:19:07 +01001408 if (is_public) {
Valerio Setti201e6432024-02-01 17:19:37 +01001409 TEST_EQUAL(mbedtls_rsa_parse_pubkey(&rsa_ctx, input->x, input->len), 0);
Valerio Setti56cfe2f2024-02-01 17:53:26 +01001410 TEST_EQUAL(mbedtls_rsa_write_pubkey(&rsa_ctx, output_buf, &output_p), input->len);
Valerio Setti8e6093d2024-01-23 15:19:07 +01001411 } else {
Valerio Setti135ebde2024-02-01 17:00:29 +01001412 TEST_EQUAL(mbedtls_rsa_parse_key(&rsa_ctx, input->x, input->len), 0);
Valerio Setti56cfe2f2024-02-01 17:53:26 +01001413 TEST_EQUAL(mbedtls_rsa_write_key(&rsa_ctx, output_buf, &output_p), input->len);
Valerio Setti8e6093d2024-01-23 15:19:07 +01001414 }
Valerio Setti56cfe2f2024-02-01 17:53:26 +01001415 output_len = output_end - output_p;
Valerio Setti1533c3f2024-01-24 11:24:20 +01001416
1417 /* Check that the written key matches with the one provided in input. */
Valerio Setti56cfe2f2024-02-01 17:53:26 +01001418 TEST_MEMORY_COMPARE(output_p, output_len, input->x, input->len);
Valerio Setti8e6093d2024-01-23 15:19:07 +01001419
1420exit:
1421 mbedtls_free(output_buf);
1422 mbedtls_rsa_free(&rsa_ctx);
1423}
1424/* END_CASE */
1425
Valerio Settia8886452024-01-30 17:35:49 +01001426/* BEGIN_CASE */
1427void rsa_key_write_incremental(int is_public, data_t *input)
1428{
1429 mbedtls_rsa_context rsa_ctx;
Valerio Setti201e6432024-02-01 17:19:37 +01001430 unsigned char *buf = NULL, *end;
Valerio Settia8886452024-01-30 17:35:49 +01001431 size_t i;
1432
1433 mbedtls_rsa_init(&rsa_ctx);
1434
1435 /* This is supposed to succeed as the real target of this test are the
1436 * write attempt below. */
1437 if (is_public) {
Valerio Setti201e6432024-02-01 17:19:37 +01001438 TEST_EQUAL(mbedtls_rsa_parse_pubkey(&rsa_ctx, input->x, input->len), 0);
Valerio Settia8886452024-01-30 17:35:49 +01001439 } else {
Valerio Setti135ebde2024-02-01 17:00:29 +01001440 TEST_EQUAL(mbedtls_rsa_parse_key(&rsa_ctx, input->x, input->len), 0);
Valerio Settia8886452024-01-30 17:35:49 +01001441 }
1442
1443 for (i = 1; i < input->len; i++) {
1444 TEST_CALLOC(buf, i);
1445 end = buf + i;
1446 /* We don't care much about the return value as long as it fails. */
1447 if (is_public) {
Valerio Setti135ebde2024-02-01 17:00:29 +01001448 TEST_ASSERT(mbedtls_rsa_write_pubkey(&rsa_ctx, buf, &end) != 0);
Valerio Settia8886452024-01-30 17:35:49 +01001449 } else {
Valerio Setti135ebde2024-02-01 17:00:29 +01001450 TEST_ASSERT(mbedtls_rsa_write_key(&rsa_ctx, buf, &end) != 0);
Valerio Settia8886452024-01-30 17:35:49 +01001451 }
1452 mbedtls_free(buf);
1453 buf = NULL;
1454 }
1455
1456 /* Ensure with the correct output buffer size everything works as expected. */
1457 TEST_CALLOC(buf, i);
1458 end = buf + i;
1459
1460 if (is_public) {
Valerio Setti135ebde2024-02-01 17:00:29 +01001461 TEST_ASSERT(mbedtls_rsa_write_pubkey(&rsa_ctx, buf, &end) != 0);
Valerio Settia8886452024-01-30 17:35:49 +01001462 } else {
Valerio Setti135ebde2024-02-01 17:00:29 +01001463 TEST_ASSERT(mbedtls_rsa_write_key(&rsa_ctx, buf, &end) > 0);
Valerio Settia8886452024-01-30 17:35:49 +01001464 }
1465
1466exit:
Valerio Setti56cfe2f2024-02-01 17:53:26 +01001467 mbedtls_free(buf);
Valerio Settia8886452024-01-30 17:35:49 +01001468 mbedtls_rsa_free(&rsa_ctx);
1469}
1470/* END_CASE */
1471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +01001473void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +00001474{
Manuel Pégourié-Gonnardfb8d90a2023-03-16 10:47:59 +01001475 MD_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 TEST_ASSERT(mbedtls_rsa_self_test(1) == 0);
Manuel Pégourié-Gonnardfb8d90a2023-03-16 10:47:59 +01001477
1478exit:
1479 MD_PSA_DONE();
Paul Bakker42a29bf2009-07-07 20:18:41 +00001480}
Paul Bakker33b43f12013-08-20 11:48:36 +02001481/* END_CASE */