blob: 37bed6dcd8327fd795a5943d0d6cba0287029bc5 [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"
Paul Bakker33b43f12013-08-20 11:48:36 +02004/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +00005
Paul Bakker33b43f12013-08-20 11:48:36 +02006/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +02008 * END_DEPENDENCIES
9 */
Paul Bakker5690efc2011-05-26 13:16:06 +000010
Paul Bakker33b43f12013-08-20 11:48:36 +020011/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010012void rsa_invalid_param()
Ronald Cronea7631b2021-06-03 18:51:59 +020013{
14 mbedtls_rsa_context ctx;
15 const int invalid_padding = 42;
16 const int invalid_hash_id = 0xff;
Gilles Peskine449bd832023-01-11 14:50:10 +010017 unsigned char buf[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
18 size_t buf_len = sizeof(buf);
Ronald Cronea7631b2021-06-03 18:51:59 +020019
Gilles Peskine449bd832023-01-11 14:50:10 +010020 mbedtls_rsa_init(&ctx);
Ronald Cronea7631b2021-06-03 18:51:59 +020021
Gilles Peskine449bd832023-01-11 14:50:10 +010022 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
23 invalid_padding,
24 MBEDTLS_MD_NONE),
25 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cronea7631b2021-06-03 18:51:59 +020026
Gilles Peskine449bd832023-01-11 14:50:10 +010027 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
28 MBEDTLS_RSA_PKCS_V21,
29 invalid_hash_id),
30 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cronea7631b2021-06-03 18:51:59 +020031
Gilles Peskine449bd832023-01-11 14:50:10 +010032 TEST_EQUAL(mbedtls_rsa_pkcs1_sign(&ctx, NULL,
33 NULL, MBEDTLS_MD_NONE,
34 buf_len,
35 NULL, buf),
36 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010037
Gilles Peskine449bd832023-01-11 14:50:10 +010038 TEST_EQUAL(mbedtls_rsa_pkcs1_sign(&ctx, NULL,
39 NULL, MBEDTLS_MD_SHA256,
40 0,
41 NULL, buf),
42 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010043
Gilles Peskine449bd832023-01-11 14:50:10 +010044 TEST_EQUAL(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE,
45 buf_len,
46 NULL, buf),
47 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 TEST_EQUAL(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_SHA256,
50 0,
51 NULL, buf),
52 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010053
Ronald Cron3a0375f2021-06-08 10:22:28 +020054#if !defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +010055 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
56 MBEDTLS_RSA_PKCS_V15,
57 MBEDTLS_MD_NONE),
58 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cron3a0375f2021-06-08 10:22:28 +020059#endif
60
Tuvshinzaya Erdenekhuufe7524d2022-09-01 16:07:18 +010061#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +010062 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
63 NULL, MBEDTLS_MD_NONE,
64 buf_len,
65 NULL, buf),
66 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010067
Gilles Peskine449bd832023-01-11 14:50:10 +010068 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
69 NULL, MBEDTLS_MD_SHA256,
70 0,
71 NULL, buf),
72 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_NONE,
75 buf_len,
76 NULL, buf),
77 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_SHA256,
80 0,
81 NULL, buf),
82 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010083
84
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010085#endif
86
Ronald Cron3a0375f2021-06-08 10:22:28 +020087#if !defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +010088 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
89 MBEDTLS_RSA_PKCS_V21,
90 MBEDTLS_MD_NONE),
91 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cron3a0375f2021-06-08 10:22:28 +020092#endif
93
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010094#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +010095 TEST_EQUAL(mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
96 MBEDTLS_MD_NONE, buf_len,
97 NULL, buf_len,
98 buf),
99 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 TEST_EQUAL(mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
102 MBEDTLS_MD_SHA256, 0,
103 NULL, buf_len,
104 buf),
105 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_NONE,
108 buf_len, NULL,
109 MBEDTLS_MD_NONE,
110 buf_len, buf),
111 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_SHA256,
114 0, NULL,
115 MBEDTLS_MD_NONE,
116 buf_len, buf),
117 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_NONE,
120 buf_len,
121 NULL, buf),
122 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_SHA256,
125 0,
126 NULL, buf),
127 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100128#endif
129
Ronald Cronea7631b2021-06-03 18:51:59 +0200130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_rsa_free(&ctx);
Ronald Cronea7631b2021-06-03 18:51:59 +0200132}
133/* END_CASE */
134
135/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136void rsa_init_free(int reinit)
Gilles Peskine914afe12021-02-01 17:55:24 +0100137{
138 mbedtls_rsa_context ctx;
139
140 /* Double free is not explicitly documented to work, but we rely on it
141 * even inside the library so that you can call mbedtls_rsa_free()
142 * unconditionally on an error path without checking whether it has
143 * already been called in the success path. */
144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 mbedtls_rsa_init(&ctx);
146 mbedtls_rsa_free(&ctx);
Gilles Peskine914afe12021-02-01 17:55:24 +0100147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (reinit) {
149 mbedtls_rsa_init(&ctx);
150 }
151 mbedtls_rsa_free(&ctx);
Gilles Peskine914afe12021-02-01 17:55:24 +0100152
153 /* This test case always succeeds, functionally speaking. A plausible
154 * bug might trigger an invalid pointer dereference or a memory leak. */
155 goto exit;
156}
157/* END_CASE */
158
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200159/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160void mbedtls_rsa_pkcs1_sign(data_t *message_str, int padding_mode,
161 int digest, int mod, char *input_P,
162 char *input_Q, char *input_N, char *input_E,
163 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000164{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200165 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100167 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200168 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
171 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
172 mbedtls_rsa_init(&ctx);
173 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
174 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 memset(output, 0x00, sizeof(output));
177 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
180 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
181 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
182 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
185 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
186 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
187 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 TEST_ASSERT(mbedtls_rsa_pkcs1_sign(
190 &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
191 digest, message_str->len, message_str->x,
192 output) == result);
193 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
196 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000197 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000198
Paul Bakkerbd51b262014-07-10 15:26:12 +0200199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
201 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
202 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000203}
Paul Bakker33b43f12013-08-20 11:48:36 +0200204/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000205
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200206/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207void mbedtls_rsa_pkcs1_verify(data_t *message_str, int padding_mode,
208 int digest, int mod,
209 char *input_N, char *input_E,
210 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100213 mbedtls_mpi N, E;
214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
216 mbedtls_rsa_init(&ctx);
217 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
218 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
221 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
222 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
223 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
224 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 TEST_ASSERT(mbedtls_rsa_pkcs1_verify(&ctx, digest, message_str->len, message_str->x,
227 result_str->x) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100228
Paul Bakkerbd51b262014-07-10 15:26:12 +0200229exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
231 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000232}
Paul Bakker33b43f12013-08-20 11:48:36 +0200233/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000234
Paul Bakker821fb082009-07-12 13:26:42 +0000235
Paul Bakker33b43f12013-08-20 11:48:36 +0200236/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237void rsa_pkcs1_sign_raw(data_t *hash_result,
238 int padding_mode, int mod,
239 char *input_P, char *input_Q,
240 char *input_N, char *input_E,
241 data_t *result_str)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000242{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200243 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100245 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200246 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 mbedtls_rsa_init(&ctx);
249 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
250 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
Paul Bakker821fb082009-07-12 13:26:42 +0000251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
253 MBEDTLS_MD_NONE) == 0);
Paul Elliotte57dd2d2021-06-25 11:13:24 +0100254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 memset(output, 0x00, sizeof(output));
256 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
259 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
260 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
261 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
264 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
265 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
266 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000267
Paul Bakker821fb082009-07-12 13:26:42 +0000268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 TEST_ASSERT(mbedtls_rsa_pkcs1_sign(&ctx, &mbedtls_test_rnd_pseudo_rand,
270 &rnd_info, MBEDTLS_MD_NONE,
271 hash_result->len,
272 hash_result->x, output) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000273
Paul Bakker821fb082009-07-12 13:26:42 +0000274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
276 ctx.len, result_str->len) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000277
Paul Bakkerbd51b262014-07-10 15:26:12 +0200278exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
280 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000283}
Paul Bakker33b43f12013-08-20 11:48:36 +0200284/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000285
Paul Bakker33b43f12013-08-20 11:48:36 +0200286/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287void rsa_pkcs1_verify_raw(data_t *hash_result,
288 int padding_mode, int mod,
289 char *input_N, char *input_E,
290 data_t *result_str, int correct)
Paul Bakker821fb082009-07-12 13:26:42 +0000291{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200292 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000294
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100295 mbedtls_mpi N, E;
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 mbedtls_rsa_init(&ctx);
299 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
300 MBEDTLS_MD_NONE) == 0);
301 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
304 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
307 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
308 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000309
Paul Bakker821fb082009-07-12 13:26:42 +0000310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 TEST_ASSERT(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE, hash_result->len, hash_result->x,
312 result_str->x) == correct);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100313
Paul Bakkerbd51b262014-07-10 15:26:12 +0200314exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
316 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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321void mbedtls_rsa_pkcs1_encrypt(data_t *message_str, int padding_mode,
322 int mod, char *input_N, char *input_E,
323 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;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker821fb082009-07-12 13:26:42 +0000333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 mbedtls_rsa_init(&ctx);
335 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
336 MBEDTLS_MD_NONE) == 0);
337 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000338
Gilles Peskine449bd832023-01-11 14:50:10 +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
Gilles Peskine449bd832023-01-11 14:50:10 +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));
344 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000345
Paul Bakker42a29bf2009-07-07 20:18:41 +0000346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 TEST_ASSERT(mbedtls_rsa_pkcs1_encrypt(&ctx,
348 &mbedtls_test_rnd_pseudo_rand,
349 &rnd_info, message_str->len,
350 message_str->x,
351 output) == result);
352 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
355 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000356 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100357
Paul Bakkerbd51b262014-07-10 15:26:12 +0200358exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
360 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000361}
Paul Bakker33b43f12013-08-20 11:48:36 +0200362/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000363
Paul Bakker33b43f12013-08-20 11:48:36 +0200364/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365void rsa_pkcs1_encrypt_bad_rng(data_t *message_str, int padding_mode,
366 int mod, char *input_N, char *input_E,
367 data_t *result_str, int result)
Paul Bakkera6656852010-07-18 19:47:14 +0000368{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200369 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000371
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100372 mbedtls_mpi N, E;
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
375 mbedtls_rsa_init(&ctx);
376 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
377 MBEDTLS_MD_NONE) == 0);
378 memset(output, 0x00, sizeof(output));
Paul Bakkera6656852010-07-18 19:47:14 +0000379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
381 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
384 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
385 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000386
Paul Bakkera6656852010-07-18 19:47:14 +0000387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 TEST_ASSERT(mbedtls_rsa_pkcs1_encrypt(&ctx, &mbedtls_test_rnd_zero_rand,
389 NULL, message_str->len,
390 message_str->x,
391 output) == result);
392 if (result == 0) {
Paul Bakkera6656852010-07-18 19:47:14 +0000393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
395 ctx.len, result_str->len) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000396 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100397
Paul Bakkerbd51b262014-07-10 15:26:12 +0200398exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
400 mbedtls_rsa_free(&ctx);
Paul Bakkera6656852010-07-18 19:47:14 +0000401}
Paul Bakker33b43f12013-08-20 11:48:36 +0200402/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000403
Paul Bakker33b43f12013-08-20 11:48:36 +0200404/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405void mbedtls_rsa_pkcs1_decrypt(data_t *message_str, int padding_mode,
406 int mod, char *input_P,
407 char *input_Q, char *input_N,
408 char *input_E, int max_output,
409 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000410{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200411 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000413 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200414 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100415 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
418 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 mbedtls_rsa_init(&ctx);
421 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
422 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 memset(output, 0x00, sizeof(output));
425 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000426
Paul Bakker42a29bf2009-07-07 20:18:41 +0000427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
429 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
430 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
431 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
434 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
435 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
436 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000437
Paul Bakker69998dd2009-07-11 19:15:20 +0000438 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 TEST_ASSERT(mbedtls_rsa_pkcs1_decrypt(&ctx, mbedtls_test_rnd_pseudo_rand,
441 &rnd_info,
442 &output_len, message_str->x, output,
443 max_output) == result);
444 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
447 output_len,
448 result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000449 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000450
Paul Bakkerbd51b262014-07-10 15:26:12 +0200451exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
453 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
454 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000455}
Paul Bakker33b43f12013-08-20 11:48:36 +0200456/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000457
Paul Bakker33b43f12013-08-20 11:48:36 +0200458/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100459void mbedtls_rsa_public(data_t *message_str, int mod,
460 char *input_N, char *input_E,
461 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000462{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200463 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000465
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100466 mbedtls_mpi N, E;
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
469 mbedtls_rsa_init(&ctx);
470 mbedtls_rsa_init(&ctx2);
471 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
474 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
Gilles Peskine058d0092021-06-09 16:24:35 +0200477
478 /* Check test data consistency */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 TEST_ASSERT(message_str->len == (size_t) (mod / 8));
480 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
481 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 TEST_ASSERT(mbedtls_rsa_public(&ctx, message_str->x, output) == result);
484 if (result == 0) {
Paul Bakker821fb082009-07-12 13:26:42 +0000485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
487 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000488 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100489
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100490 /* And now with the copy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 TEST_ASSERT(mbedtls_rsa_copy(&ctx2, &ctx) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200492 /* clear the original to be sure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 mbedtls_rsa_free(&ctx);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx2) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 memset(output, 0x00, sizeof(output));
498 TEST_ASSERT(mbedtls_rsa_public(&ctx2, message_str->x, output) == result);
499 if (result == 0) {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
502 ctx.len, result_str->len) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100503 }
504
Paul Bakkerbd51b262014-07-10 15:26:12 +0200505exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
507 mbedtls_rsa_free(&ctx);
508 mbedtls_rsa_free(&ctx2);
Paul Bakker821fb082009-07-12 13:26:42 +0000509}
Paul Bakker33b43f12013-08-20 11:48:36 +0200510/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000511
Paul Bakker33b43f12013-08-20 11:48:36 +0200512/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513void mbedtls_rsa_private(data_t *message_str, int mod,
514 char *input_P, char *input_Q,
515 char *input_N, char *input_E,
516 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000517{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200518 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100520 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200521 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200522 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
525 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
526 mbedtls_rsa_init(&ctx);
527 mbedtls_rsa_init(&ctx2);
Paul Bakker821fb082009-07-12 13:26:42 +0000528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker821fb082009-07-12 13:26:42 +0000530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
532 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
533 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
534 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
Gilles Peskine058d0092021-06-09 16:24:35 +0200537
538 /* Check test data consistency */
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 TEST_ASSERT(message_str->len == (size_t) (mod / 8));
540 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
541 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
542 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000543
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200544 /* repeat three times to test updating of blinding values */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 for (i = 0; i < 3; i++) {
546 memset(output, 0x00, sizeof(output));
547 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_pseudo_rand,
548 &rnd_info, message_str->x,
549 output) == result);
550 if (result == 0) {
Paul Bakker821fb082009-07-12 13:26:42 +0000551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
553 ctx.len,
554 result_str->len) == 0);
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200555 }
Paul Bakker821fb082009-07-12 13:26:42 +0000556 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000557
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100558 /* And now one more time with the copy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 TEST_ASSERT(mbedtls_rsa_copy(&ctx2, &ctx) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200560 /* clear the original to be sure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 mbedtls_rsa_free(&ctx);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx2) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 memset(output, 0x00, sizeof(output));
566 TEST_ASSERT(mbedtls_rsa_private(&ctx2, mbedtls_test_rnd_pseudo_rand,
567 &rnd_info, message_str->x,
568 output) == result);
569 if (result == 0) {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
572 ctx2.len,
573 result_str->len) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100574 }
575
Paul Bakkerbd51b262014-07-10 15:26:12 +0200576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
578 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 mbedtls_rsa_free(&ctx); mbedtls_rsa_free(&ctx2);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000581}
Paul Bakker33b43f12013-08-20 11:48:36 +0200582/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000583
Paul Bakker33b43f12013-08-20 11:48:36 +0200584/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100585void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000586{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_rsa_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 memset(&ctx, 0x00, sizeof(mbedtls_rsa_context));
Paul Bakker37940d9f2009-07-10 22:38:58 +0000589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000591}
Paul Bakker33b43f12013-08-20 11:48:36 +0200592/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000593
Paul Bakker33b43f12013-08-20 11:48:36 +0200594/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100595void mbedtls_rsa_check_pubkey(char *input_N, char *input_E, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000596{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100598 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
601 mbedtls_rsa_init(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (strlen(input_N)) {
604 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000605 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if (strlen(input_E)) {
607 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000608 }
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
611 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100612
Paul Bakkerbd51b262014-07-10 15:26:12 +0200613exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
615 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000616}
Paul Bakker33b43f12013-08-20 11:48:36 +0200617/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000618
Paul Bakker33b43f12013-08-20 11:48:36 +0200619/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100620void mbedtls_rsa_check_privkey(int mod, char *input_P, char *input_Q,
621 char *input_N, char *input_E, char *input_D,
622 char *input_DP, char *input_DQ, char *input_QP,
623 int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000624{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 mbedtls_rsa_init(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000628
Paul Bakker33b43f12013-08-20 11:48:36 +0200629 ctx.len = mod / 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (strlen(input_P)) {
631 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.P, input_P) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000632 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (strlen(input_Q)) {
634 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.Q, input_Q) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000635 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 if (strlen(input_N)) {
637 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.N, input_N) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000638 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (strlen(input_E)) {
640 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000641 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (strlen(input_D)) {
643 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.D, input_D) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000644 }
Hanno Becker131134f2017-08-23 08:31:07 +0100645#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (strlen(input_DP)) {
647 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.DP, input_DP) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000648 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (strlen(input_DQ)) {
650 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.DQ, input_DQ) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000651 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (strlen(input_QP)) {
653 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.QP, input_QP) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000654 }
Hanno Becker131134f2017-08-23 08:31:07 +0100655#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100656 ((void) input_DP);
657 ((void) input_DQ);
658 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100659#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100662
Paul Bakkerbd51b262014-07-10 15:26:12 +0200663exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000665}
Paul Bakker33b43f12013-08-20 11:48:36 +0200666/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000667
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100668/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669void rsa_check_pubpriv(int mod, char *input_Npub, char *input_Epub,
670 char *input_P, char *input_Q, char *input_N,
671 char *input_E, char *input_D, char *input_DP,
672 char *input_DQ, char *input_QP, int result)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100673{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 mbedtls_rsa_init(&pub);
677 mbedtls_rsa_init(&prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100678
679 pub.len = mod / 8;
680 prv.len = mod / 8;
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (strlen(input_Npub)) {
683 TEST_ASSERT(mbedtls_test_read_mpi(&pub.N, input_Npub) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100684 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (strlen(input_Epub)) {
686 TEST_ASSERT(mbedtls_test_read_mpi(&pub.E, input_Epub) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100687 }
688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (strlen(input_P)) {
690 TEST_ASSERT(mbedtls_test_read_mpi(&prv.P, input_P) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100691 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (strlen(input_Q)) {
693 TEST_ASSERT(mbedtls_test_read_mpi(&prv.Q, input_Q) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100694 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (strlen(input_N)) {
696 TEST_ASSERT(mbedtls_test_read_mpi(&prv.N, input_N) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100697 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (strlen(input_E)) {
699 TEST_ASSERT(mbedtls_test_read_mpi(&prv.E, input_E) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100700 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (strlen(input_D)) {
702 TEST_ASSERT(mbedtls_test_read_mpi(&prv.D, input_D) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100703 }
Hanno Becker131134f2017-08-23 08:31:07 +0100704#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (strlen(input_DP)) {
706 TEST_ASSERT(mbedtls_test_read_mpi(&prv.DP, input_DP) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100707 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (strlen(input_DQ)) {
709 TEST_ASSERT(mbedtls_test_read_mpi(&prv.DQ, input_DQ) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100710 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (strlen(input_QP)) {
712 TEST_ASSERT(mbedtls_test_read_mpi(&prv.QP, input_QP) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100713 }
Hanno Becker131134f2017-08-23 08:31:07 +0100714#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100715 ((void) input_DP);
716 ((void) input_DQ);
717 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100718#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 TEST_ASSERT(mbedtls_rsa_check_pub_priv(&pub, &prv) == result);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100721
722exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 mbedtls_rsa_free(&pub);
724 mbedtls_rsa_free(&prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100725}
726/* END_CASE */
727
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200728/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729void mbedtls_rsa_gen_key(int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000730{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_rsa_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 mbedtls_rsa_init(&ctx);
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000733
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200734 /* This test uses an insecure RNG, suitable only for testing.
735 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 TEST_ASSERT(mbedtls_rsa_gen_key(&ctx, mbedtls_test_rnd_std_rand, NULL, nrbits,
737 exponent) == result);
738 if (result == 0) {
739 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
740 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&ctx.P, &ctx.Q) > 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000741 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100742
Paul Bakkerbd51b262014-07-10 15:26:12 +0200743exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000745}
Paul Bakker33b43f12013-08-20 11:48:36 +0200746/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000747
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +0200748/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749void mbedtls_rsa_deduce_primes(char *input_N,
750 char *input_D,
751 char *input_E,
752 char *output_P,
753 char *output_Q,
754 int corrupt, int result)
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100755{
756 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 mbedtls_mpi_init(&N);
759 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
760 mbedtls_mpi_init(&Pp); mbedtls_mpi_init(&Qp);
761 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
764 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
765 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
766 TEST_ASSERT(mbedtls_test_read_mpi(&Qp, output_P) == 0);
767 TEST_ASSERT(mbedtls_test_read_mpi(&Pp, output_Q) == 0);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (corrupt) {
770 TEST_ASSERT(mbedtls_mpi_add_int(&D, &D, 2) == 0);
771 }
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100772
773 /* Try to deduce P, Q from N, D, E only. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 TEST_ASSERT(mbedtls_rsa_deduce_primes(&N, &D, &E, &P, &Q) == result);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (!corrupt) {
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100777 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 TEST_ASSERT((mbedtls_mpi_cmp_mpi(&P, &Pp) == 0 && mbedtls_mpi_cmp_mpi(&Q, &Qp) == 0) ||
779 (mbedtls_mpi_cmp_mpi(&P, &Qp) == 0 && mbedtls_mpi_cmp_mpi(&Q, &Pp) == 0));
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100780 }
781
782exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 mbedtls_mpi_free(&N);
784 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
785 mbedtls_mpi_free(&Pp); mbedtls_mpi_free(&Qp);
786 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100787}
788/* END_CASE */
789
Hanno Becker6b4ce492017-08-23 11:00:21 +0100790/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100791void mbedtls_rsa_deduce_private_exponent(char *input_P,
792 char *input_Q,
793 char *input_E,
794 char *output_D,
795 int corrupt, int result)
Hanno Becker6b4ce492017-08-23 11:00:21 +0100796{
797 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
800 mbedtls_mpi_init(&D); mbedtls_mpi_init(&Dp);
801 mbedtls_mpi_init(&E);
802 mbedtls_mpi_init(&R); mbedtls_mpi_init(&Rp);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
805 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
806 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
807 TEST_ASSERT(mbedtls_test_read_mpi(&Dp, output_D) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (corrupt) {
Hanno Becker6b4ce492017-08-23 11:00:21 +0100810 /* Make E even */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 TEST_ASSERT(mbedtls_mpi_set_bit(&E, 0, 0) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100812 }
813
814 /* Try to deduce D from N, P, Q, E. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 TEST_ASSERT(mbedtls_rsa_deduce_private_exponent(&P, &Q,
816 &E, &D) == result);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (!corrupt) {
Hanno Becker6b4ce492017-08-23 11:00:21 +0100819 /*
820 * Check that D and Dp agree modulo LCM(P-1, Q-1).
821 */
822
823 /* Replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 TEST_ASSERT(mbedtls_mpi_sub_int(&P, &P, 1) == 0);
825 TEST_ASSERT(mbedtls_mpi_sub_int(&Q, &Q, 1) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100826
827 /* Check D == Dp modulo P-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &D, &P) == 0);
829 TEST_ASSERT(mbedtls_mpi_mod_mpi(&Rp, &Dp, &P) == 0);
830 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &Rp) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100831
832 /* Check D == Dp modulo Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &D, &Q) == 0);
834 TEST_ASSERT(mbedtls_mpi_mod_mpi(&Rp, &Dp, &Q) == 0);
835 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &Rp) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100836 }
837
838exit:
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
841 mbedtls_mpi_free(&D); mbedtls_mpi_free(&Dp);
842 mbedtls_mpi_free(&E);
843 mbedtls_mpi_free(&R); mbedtls_mpi_free(&Rp);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100844}
845/* END_CASE */
846
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200847/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848void mbedtls_rsa_import(char *input_N,
849 char *input_P,
850 char *input_Q,
851 char *input_D,
852 char *input_E,
853 int successive,
854 int is_priv,
855 int res_check,
856 int res_complete)
Hanno Beckerc77ab892017-08-23 11:01:06 +0100857{
858 mbedtls_mpi N, P, Q, D, E;
859 mbedtls_rsa_context ctx;
860
Hanno Beckere1582a82017-09-29 11:51:05 +0100861 /* Buffers used for encryption-decryption test */
862 unsigned char *buf_orig = NULL;
863 unsigned char *buf_enc = NULL;
864 unsigned char *buf_dec = NULL;
865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 const int have_N = (strlen(input_N) > 0);
867 const int have_P = (strlen(input_P) > 0);
868 const int have_Q = (strlen(input_Q) > 0);
869 const int have_D = (strlen(input_D) > 0);
870 const int have_E = (strlen(input_E) > 0);
Hanno Becker4d6e8342017-09-29 11:50:18 +0100871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 mbedtls_rsa_init(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 mbedtls_mpi_init(&N);
875 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
876 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (have_N) {
879 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100880 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100881
882 if (have_P) {
883 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
884 }
885
886 if (have_Q) {
887 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
888 }
889
890 if (have_D) {
891 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
892 }
893
894 if (have_E) {
895 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
896 }
897
898 if (!successive) {
899 TEST_ASSERT(mbedtls_rsa_import(&ctx,
900 have_N ? &N : NULL,
901 have_P ? &P : NULL,
902 have_Q ? &Q : NULL,
903 have_D ? &D : NULL,
904 have_E ? &E : NULL) == 0);
905 } else {
Hanno Beckerc77ab892017-08-23 11:01:06 +0100906 /* Import N, P, Q, D, E separately.
907 * This should make no functional difference. */
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 TEST_ASSERT(mbedtls_rsa_import(&ctx,
910 have_N ? &N : NULL,
911 NULL, NULL, NULL, NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 TEST_ASSERT(mbedtls_rsa_import(&ctx,
914 NULL,
915 have_P ? &P : NULL,
916 NULL, NULL, NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 TEST_ASSERT(mbedtls_rsa_import(&ctx,
919 NULL, NULL,
920 have_Q ? &Q : NULL,
921 NULL, NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 TEST_ASSERT(mbedtls_rsa_import(&ctx,
924 NULL, NULL, NULL,
925 have_D ? &D : NULL,
926 NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 TEST_ASSERT(mbedtls_rsa_import(&ctx,
929 NULL, NULL, NULL, NULL,
930 have_E ? &E : NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100931 }
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == res_complete);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100934
Hanno Beckere1582a82017-09-29 11:51:05 +0100935 /* On expected success, perform some public and private
936 * key operations to check if the key is working properly. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (res_complete == 0) {
938 if (is_priv) {
939 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == res_check);
940 } else {
941 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == res_check);
942 }
Hanno Becker04877a42017-10-11 10:01:33 +0100943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (res_check != 0) {
Hanno Becker04877a42017-10-11 10:01:33 +0100945 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 }
Hanno Beckere1582a82017-09-29 11:51:05 +0100947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 buf_orig = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
949 buf_enc = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
950 buf_dec = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
951 if (buf_orig == NULL || buf_enc == NULL || buf_dec == NULL) {
Hanno Beckere1582a82017-09-29 11:51:05 +0100952 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 }
Hanno Beckere1582a82017-09-29 11:51:05 +0100954
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200955 /* This test uses an insecure RNG, suitable only for testing.
956 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL,
958 buf_orig, mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100959
960 /* Make sure the number we're generating is smaller than the modulus */
961 buf_orig[0] = 0x00;
962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 TEST_ASSERT(mbedtls_rsa_public(&ctx, buf_orig, buf_enc) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if (is_priv) {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200966 /* This test uses an insecure RNG, suitable only for testing.
967 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_std_rand,
969 NULL, buf_enc,
970 buf_dec) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 TEST_ASSERT(memcmp(buf_orig, buf_dec,
973 mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100974 }
975 }
976
Hanno Beckerc77ab892017-08-23 11:01:06 +0100977exit:
978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 mbedtls_free(buf_orig);
980 mbedtls_free(buf_enc);
981 mbedtls_free(buf_dec);
Hanno Beckere1582a82017-09-29 11:51:05 +0100982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 mbedtls_rsa_free(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 mbedtls_mpi_free(&N);
986 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
987 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100988}
989/* END_CASE */
990
Hanno Becker417f2d62017-08-23 11:44:51 +0100991/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100992void mbedtls_rsa_export(char *input_N,
993 char *input_P,
994 char *input_Q,
995 char *input_D,
996 char *input_E,
997 int is_priv,
998 int successive)
Hanno Becker417f2d62017-08-23 11:44:51 +0100999{
1000 /* Original MPI's with which we set up the RSA context */
1001 mbedtls_mpi N, P, Q, D, E;
1002
1003 /* Exported MPI's */
1004 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 const int have_N = (strlen(input_N) > 0);
1007 const int have_P = (strlen(input_P) > 0);
1008 const int have_Q = (strlen(input_Q) > 0);
1009 const int have_D = (strlen(input_D) > 0);
1010 const int have_E = (strlen(input_E) > 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001011
Hanno Becker417f2d62017-08-23 11:44:51 +01001012 mbedtls_rsa_context ctx;
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 mbedtls_rsa_init(&ctx);
Hanno Becker417f2d62017-08-23 11:44:51 +01001015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 mbedtls_mpi_init(&N);
1017 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
1018 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Becker417f2d62017-08-23 11:44:51 +01001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 mbedtls_mpi_init(&Ne);
1021 mbedtls_mpi_init(&Pe); mbedtls_mpi_init(&Qe);
1022 mbedtls_mpi_init(&De); mbedtls_mpi_init(&Ee);
Hanno Becker417f2d62017-08-23 11:44:51 +01001023
1024 /* Setup RSA context */
1025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if (have_N) {
1027 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
1028 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if (have_P) {
1031 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
1032 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if (have_Q) {
1035 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
1036 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001037
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if (have_D) {
1039 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
1040 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (have_E) {
1043 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
1044 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 TEST_ASSERT(mbedtls_rsa_import(&ctx,
1047 strlen(input_N) ? &N : NULL,
1048 strlen(input_P) ? &P : NULL,
1049 strlen(input_Q) ? &Q : NULL,
1050 strlen(input_D) ? &D : NULL,
1051 strlen(input_E) ? &E : NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001054
1055 /*
1056 * Export parameters and compare to original ones.
1057 */
1058
1059 /* N and E must always be present. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (!successive) {
1061 TEST_ASSERT(mbedtls_rsa_export(&ctx, &Ne, NULL, NULL, NULL, &Ee) == 0);
1062 } else {
1063 TEST_ASSERT(mbedtls_rsa_export(&ctx, &Ne, NULL, NULL, NULL, NULL) == 0);
1064 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, NULL, NULL, &Ee) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001065 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&N, &Ne) == 0);
1067 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&E, &Ee) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001068
1069 /* If we were providing enough information to setup a complete private context,
1070 * we expect to be able to export all core parameters. */
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (is_priv) {
1073 if (!successive) {
1074 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, &Pe, &Qe,
1075 &De, NULL) == 0);
1076 } else {
1077 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, &Pe, NULL,
1078 NULL, NULL) == 0);
1079 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, &Qe,
1080 NULL, NULL) == 0);
1081 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, NULL,
1082 &De, NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001083 }
1084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if (have_P) {
1086 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P, &Pe) == 0);
1087 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 if (have_Q) {
1090 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Q, &Qe) == 0);
1091 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if (have_D) {
1094 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&D, &De) == 0);
1095 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001096
1097 /* While at it, perform a sanity check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 TEST_ASSERT(mbedtls_rsa_validate_params(&Ne, &Pe, &Qe, &De, &Ee,
1099 NULL, NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001100 }
1101
1102exit:
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 mbedtls_rsa_free(&ctx);
Hanno Becker417f2d62017-08-23 11:44:51 +01001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 mbedtls_mpi_free(&N);
1107 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
1108 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Becker417f2d62017-08-23 11:44:51 +01001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 mbedtls_mpi_free(&Ne);
1111 mbedtls_mpi_free(&Pe); mbedtls_mpi_free(&Qe);
1112 mbedtls_mpi_free(&De); mbedtls_mpi_free(&Ee);
Hanno Becker417f2d62017-08-23 11:44:51 +01001113}
1114/* END_CASE */
1115
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001116/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117void mbedtls_rsa_validate_params(char *input_N,
1118 char *input_P,
1119 char *input_Q,
1120 char *input_D,
1121 char *input_E,
1122 int prng, int result)
Hanno Beckerce002632017-08-23 13:22:36 +01001123{
1124 /* Original MPI's with which we set up the RSA context */
1125 mbedtls_mpi N, P, Q, D, E;
1126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 const int have_N = (strlen(input_N) > 0);
1128 const int have_P = (strlen(input_P) > 0);
1129 const int have_Q = (strlen(input_Q) > 0);
1130 const int have_D = (strlen(input_D) > 0);
1131 const int have_E = (strlen(input_E) > 0);
Hanno Beckerce002632017-08-23 13:22:36 +01001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 mbedtls_mpi_init(&N);
1134 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
1135 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerce002632017-08-23 13:22:36 +01001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if (have_N) {
1138 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
1139 }
Hanno Beckerce002632017-08-23 13:22:36 +01001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (have_P) {
1142 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
1143 }
Hanno Beckerce002632017-08-23 13:22:36 +01001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (have_Q) {
1146 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
1147 }
Hanno Beckerce002632017-08-23 13:22:36 +01001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (have_D) {
1150 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
1151 }
Hanno Beckerce002632017-08-23 13:22:36 +01001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if (have_E) {
1154 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
1155 }
Hanno Beckerce002632017-08-23 13:22:36 +01001156
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001157 /* This test uses an insecure RNG, suitable only for testing.
1158 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 TEST_ASSERT(mbedtls_rsa_validate_params(have_N ? &N : NULL,
1160 have_P ? &P : NULL,
1161 have_Q ? &Q : NULL,
1162 have_D ? &D : NULL,
1163 have_E ? &E : NULL,
1164 prng ? mbedtls_test_rnd_std_rand : NULL,
1165 prng ? NULL : NULL) == result);
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001166
Hanno Beckerce002632017-08-23 13:22:36 +01001167exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 mbedtls_mpi_free(&N);
1169 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
1170 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerce002632017-08-23 13:22:36 +01001171}
1172/* END_CASE */
1173
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +02001174/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001175void mbedtls_rsa_export_raw(data_t *input_N, data_t *input_P,
1176 data_t *input_Q, data_t *input_D,
1177 data_t *input_E, int is_priv,
1178 int successive)
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001179{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001180 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001181 unsigned char bufNe[256];
1182 unsigned char bufPe[128];
1183 unsigned char bufQe[128];
1184 unsigned char bufDe[256];
1185 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001186
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001187 mbedtls_rsa_context ctx;
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 mbedtls_rsa_init(&ctx);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001190
1191 /* Setup RSA context */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1193 input_N->len ? input_N->x : NULL, input_N->len,
1194 input_P->len ? input_P->x : NULL, input_P->len,
1195 input_Q->len ? input_Q->x : NULL, input_Q->len,
1196 input_D->len ? input_D->x : NULL, input_D->len,
1197 input_E->len ? input_E->x : NULL, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001200
1201 /*
1202 * Export parameters and compare to original ones.
1203 */
1204
1205 /* N and E must always be present. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (!successive) {
1207 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, bufNe, input_N->len,
1208 NULL, 0, NULL, 0, NULL, 0,
1209 bufEe, input_E->len) == 0);
1210 } else {
1211 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, bufNe, input_N->len,
1212 NULL, 0, NULL, 0, NULL, 0,
1213 NULL, 0) == 0);
1214 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1215 NULL, 0, NULL, 0, NULL, 0,
1216 bufEe, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001217 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 TEST_ASSERT(memcmp(input_N->x, bufNe, input_N->len) == 0);
1219 TEST_ASSERT(memcmp(input_E->x, bufEe, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001220
1221 /* If we were providing enough information to setup a complete private context,
1222 * we expect to be able to export all core parameters. */
1223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (is_priv) {
1225 if (!successive) {
1226 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1227 bufPe, input_P->len ? input_P->len : sizeof(bufPe),
1228 bufQe, input_Q->len ? input_Q->len : sizeof(bufQe),
1229 bufDe, input_D->len ? input_D->len : sizeof(bufDe),
1230 NULL, 0) == 0);
1231 } else {
1232 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1233 bufPe, input_P->len ? input_P->len : sizeof(bufPe),
1234 NULL, 0, NULL, 0,
1235 NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0, NULL, 0,
1238 bufQe, input_Q->len ? input_Q->len : sizeof(bufQe),
1239 NULL, 0, NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0, NULL, 0, NULL, 0,
1242 bufDe, input_D->len ? input_D->len : sizeof(bufDe),
1243 NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001244 }
1245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (input_P->len) {
1247 TEST_ASSERT(memcmp(input_P->x, bufPe, input_P->len) == 0);
1248 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (input_Q->len) {
1251 TEST_ASSERT(memcmp(input_Q->x, bufQe, input_Q->len) == 0);
1252 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if (input_D->len) {
1255 TEST_ASSERT(memcmp(input_D->x, bufDe, input_D->len) == 0);
1256 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001257
1258 }
1259
1260exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 mbedtls_rsa_free(&ctx);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001262}
1263/* END_CASE */
1264
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001265/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001266void mbedtls_rsa_import_raw(data_t *input_N,
1267 data_t *input_P, data_t *input_Q,
1268 data_t *input_D, data_t *input_E,
1269 int successive,
1270 int is_priv,
1271 int res_check,
1272 int res_complete)
Hanno Beckerc77ab892017-08-23 11:01:06 +01001273{
Hanno Beckere1582a82017-09-29 11:51:05 +01001274 /* Buffers used for encryption-decryption test */
1275 unsigned char *buf_orig = NULL;
1276 unsigned char *buf_enc = NULL;
1277 unsigned char *buf_dec = NULL;
1278
Hanno Beckerc77ab892017-08-23 11:01:06 +01001279 mbedtls_rsa_context ctx;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 mbedtls_rsa_init(&ctx);
Hanno Becker3f3ae852017-10-02 10:08:39 +01001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (!successive) {
1284 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1285 (input_N->len > 0) ? input_N->x : NULL, input_N->len,
1286 (input_P->len > 0) ? input_P->x : NULL, input_P->len,
1287 (input_Q->len > 0) ? input_Q->x : NULL, input_Q->len,
1288 (input_D->len > 0) ? input_D->x : NULL, input_D->len,
1289 (input_E->len > 0) ? input_E->x : NULL,
1290 input_E->len) == 0);
1291 } else {
Hanno Beckerc77ab892017-08-23 11:01:06 +01001292 /* Import N, P, Q, D, E separately.
1293 * This should make no functional difference. */
1294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1296 (input_N->len > 0) ? input_N->x : NULL, input_N->len,
1297 NULL, 0, NULL, 0, NULL, 0, NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1300 NULL, 0,
1301 (input_P->len > 0) ? input_P->x : NULL, input_P->len,
1302 NULL, 0, NULL, 0, NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1305 NULL, 0, NULL, 0,
1306 (input_Q->len > 0) ? input_Q->x : NULL, input_Q->len,
1307 NULL, 0, NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1310 NULL, 0, NULL, 0, NULL, 0,
1311 (input_D->len > 0) ? input_D->x : NULL, input_D->len,
1312 NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1315 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1316 (input_E->len > 0) ? input_E->x : NULL,
1317 input_E->len) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001318 }
1319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == res_complete);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001321
Hanno Beckere1582a82017-09-29 11:51:05 +01001322 /* On expected success, perform some public and private
1323 * key operations to check if the key is working properly. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (res_complete == 0) {
1325 if (is_priv) {
1326 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == res_check);
1327 } else {
1328 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == res_check);
1329 }
Hanno Becker04877a42017-10-11 10:01:33 +01001330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if (res_check != 0) {
Hanno Becker04877a42017-10-11 10:01:33 +01001332 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 }
Hanno Beckere1582a82017-09-29 11:51:05 +01001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 buf_orig = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1336 buf_enc = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1337 buf_dec = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1338 if (buf_orig == NULL || buf_enc == NULL || buf_dec == NULL) {
Hanno Beckere1582a82017-09-29 11:51:05 +01001339 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 }
Hanno Beckere1582a82017-09-29 11:51:05 +01001341
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001342 /* This test uses an insecure RNG, suitable only for testing.
1343 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL,
1345 buf_orig, mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001346
1347 /* Make sure the number we're generating is smaller than the modulus */
1348 buf_orig[0] = 0x00;
1349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 TEST_ASSERT(mbedtls_rsa_public(&ctx, buf_orig, buf_enc) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 if (is_priv) {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001353 /* This test uses an insecure RNG, suitable only for testing.
1354 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_std_rand,
1356 NULL, buf_enc,
1357 buf_dec) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 TEST_ASSERT(memcmp(buf_orig, buf_dec,
1360 mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001361 }
1362 }
1363
Hanno Beckerc77ab892017-08-23 11:01:06 +01001364exit:
1365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 mbedtls_free(buf_orig);
1367 mbedtls_free(buf_enc);
1368 mbedtls_free(buf_dec);
Hanno Becker3f3ae852017-10-02 10:08:39 +01001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 mbedtls_rsa_free(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001371}
1372/* END_CASE */
1373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +01001375void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +00001376{
Manuel Pégourié-Gonnardfb8d90a2023-03-16 10:47:59 +01001377 MD_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 TEST_ASSERT(mbedtls_rsa_self_test(1) == 0);
Manuel Pégourié-Gonnardfb8d90a2023-03-16 10:47:59 +01001379
1380exit:
1381 MD_PSA_DONE();
Paul Bakker42a29bf2009-07-07 20:18:41 +00001382}
Paul Bakker33b43f12013-08-20 11:48:36 +02001383/* END_CASE */