blob: deb156e7195200fb1386f0cb5b415c83d58e0506 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/rsa.h"
Chris Jones66a4cd42021-03-09 16:04:12 +00003#include "rsa_alt_helpers.h"
Hanno Becker47deec42017-07-24 12:27:09 +01004
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +02005#include "mbedtls/legacy_or_psa.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02006/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +00007
Paul Bakker33b43f12013-08-20 11:48:36 +02008/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +020010 * END_DEPENDENCIES
11 */
Paul Bakker5690efc2011-05-26 13:16:06 +000012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010014void rsa_invalid_param()
Ronald Cronea7631b2021-06-03 18:51:59 +020015{
16 mbedtls_rsa_context ctx;
17 const int invalid_padding = 42;
18 const int invalid_hash_id = 0xff;
Gilles Peskine449bd832023-01-11 14:50:10 +010019 unsigned char buf[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
20 size_t buf_len = sizeof(buf);
Ronald Cronea7631b2021-06-03 18:51:59 +020021
Gilles Peskine449bd832023-01-11 14:50:10 +010022 mbedtls_rsa_init(&ctx);
Ronald Cronea7631b2021-06-03 18:51:59 +020023
Gilles Peskine449bd832023-01-11 14:50:10 +010024 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
25 invalid_padding,
26 MBEDTLS_MD_NONE),
27 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cronea7631b2021-06-03 18:51:59 +020028
Yanray Wang09714752023-03-01 16:31:46 +080029 TEST_EQUAL(mbedtls_rsa_get_padding_mode(&ctx), MBEDTLS_RSA_PKCS_V15);
30 TEST_EQUAL(mbedtls_rsa_get_hash_id(&ctx), MBEDTLS_MD_NONE);
31
Gilles Peskine449bd832023-01-11 14:50:10 +010032 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
33 MBEDTLS_RSA_PKCS_V21,
34 invalid_hash_id),
35 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cronea7631b2021-06-03 18:51:59 +020036
Yanray Wang09714752023-03-01 16:31:46 +080037 TEST_EQUAL(mbedtls_rsa_get_padding_mode(&ctx), MBEDTLS_RSA_PKCS_V15);
38 TEST_EQUAL(mbedtls_rsa_get_hash_id(&ctx), MBEDTLS_MD_NONE);
39
Gilles Peskine449bd832023-01-11 14:50:10 +010040 TEST_EQUAL(mbedtls_rsa_pkcs1_sign(&ctx, NULL,
41 NULL, MBEDTLS_MD_NONE,
42 buf_len,
43 NULL, buf),
44 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 TEST_EQUAL(mbedtls_rsa_pkcs1_sign(&ctx, NULL,
47 NULL, MBEDTLS_MD_SHA256,
48 0,
49 NULL, buf),
50 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010051
Gilles Peskine449bd832023-01-11 14:50:10 +010052 TEST_EQUAL(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE,
53 buf_len,
54 NULL, buf),
55 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010056
Gilles Peskine449bd832023-01-11 14:50:10 +010057 TEST_EQUAL(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_SHA256,
58 0,
59 NULL, buf),
60 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010061
Ronald Cron3a0375f2021-06-08 10:22:28 +020062#if !defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +010063 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
64 MBEDTLS_RSA_PKCS_V15,
65 MBEDTLS_MD_NONE),
66 MBEDTLS_ERR_RSA_INVALID_PADDING);
Yanray Wang09714752023-03-01 16:31:46 +080067
68 TEST_EQUAL(mbedtls_rsa_get_padding_mode(&ctx), MBEDTLS_RSA_PKCS_V15);
69 TEST_EQUAL(mbedtls_rsa_get_hash_id(&ctx), MBEDTLS_MD_NONE);
Ronald Cron3a0375f2021-06-08 10:22:28 +020070#endif
71
Tuvshinzaya Erdenekhuufe7524d2022-09-01 16:07:18 +010072#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +010073 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
74 NULL, 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_sign(&ctx, NULL,
80 NULL, MBEDTLS_MD_SHA256,
81 0,
82 NULL, buf),
83 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_NONE,
86 buf_len,
87 NULL, buf),
88 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010089
Gilles Peskine449bd832023-01-11 14:50:10 +010090 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_SHA256,
91 0,
92 NULL, buf),
93 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010094
95
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010096#endif
97
Ronald Cron3a0375f2021-06-08 10:22:28 +020098#if !defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +010099 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
100 MBEDTLS_RSA_PKCS_V21,
101 MBEDTLS_MD_NONE),
102 MBEDTLS_ERR_RSA_INVALID_PADDING);
Yanray Wang09714752023-03-01 16:31:46 +0800103
104 TEST_EQUAL(mbedtls_rsa_get_padding_mode(&ctx), MBEDTLS_RSA_PKCS_V15);
105 TEST_EQUAL(mbedtls_rsa_get_hash_id(&ctx), MBEDTLS_MD_NONE);
Ronald Cron3a0375f2021-06-08 10:22:28 +0200106#endif
107
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100108#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 TEST_EQUAL(mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
110 MBEDTLS_MD_NONE, buf_len,
111 NULL, buf_len,
112 buf),
113 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 TEST_EQUAL(mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
116 MBEDTLS_MD_SHA256, 0,
117 NULL, buf_len,
118 buf),
119 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_NONE,
122 buf_len, NULL,
123 MBEDTLS_MD_NONE,
124 buf_len, buf),
125 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_SHA256,
128 0, NULL,
129 MBEDTLS_MD_NONE,
130 buf_len, buf),
131 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_NONE,
134 buf_len,
135 NULL, buf),
136 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_SHA256,
139 0,
140 NULL, buf),
141 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100142#endif
143
Ronald Cronea7631b2021-06-03 18:51:59 +0200144exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 mbedtls_rsa_free(&ctx);
Ronald Cronea7631b2021-06-03 18:51:59 +0200146}
147/* END_CASE */
148
149/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150void rsa_init_free(int reinit)
Gilles Peskine914afe12021-02-01 17:55:24 +0100151{
152 mbedtls_rsa_context ctx;
153
154 /* Double free is not explicitly documented to work, but we rely on it
155 * even inside the library so that you can call mbedtls_rsa_free()
156 * unconditionally on an error path without checking whether it has
157 * already been called in the success path. */
158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 mbedtls_rsa_init(&ctx);
160 mbedtls_rsa_free(&ctx);
Gilles Peskine914afe12021-02-01 17:55:24 +0100161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if (reinit) {
163 mbedtls_rsa_init(&ctx);
164 }
165 mbedtls_rsa_free(&ctx);
Gilles Peskine914afe12021-02-01 17:55:24 +0100166
167 /* This test case always succeeds, functionally speaking. A plausible
168 * bug might trigger an invalid pointer dereference or a memory leak. */
169 goto exit;
170}
171/* END_CASE */
172
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200173/* BEGIN_CASE */
Yanray Wang09714752023-03-01 16:31:46 +0800174void rsa_ctx_param_validation(int padding_mode, int hash_id)
175{
176 mbedtls_rsa_context ctx;
177
178 mbedtls_rsa_init(&ctx);
179
180 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx,
181 padding_mode,
182 hash_id) == 0);
183
184 TEST_ASSERT(mbedtls_rsa_get_padding_mode(&ctx) == padding_mode);
185 TEST_ASSERT(mbedtls_rsa_get_hash_id(&ctx) == hash_id);
186
187exit:
188 mbedtls_rsa_free(&ctx);
189}
190/* END_CASE */
191
192/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193void mbedtls_rsa_pkcs1_sign(data_t *message_str, int padding_mode,
194 int digest, int mod, char *input_P,
195 char *input_Q, char *input_N, char *input_E,
196 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000197{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200198 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100200 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200201 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
204 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
205 mbedtls_rsa_init(&ctx);
206 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
207 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 memset(output, 0x00, sizeof(output));
210 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
213 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
214 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
215 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
218 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
219 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
220 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 TEST_ASSERT(mbedtls_rsa_pkcs1_sign(
223 &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
224 digest, message_str->len, message_str->x,
225 output) == result);
226 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
229 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000230 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000231
Paul Bakkerbd51b262014-07-10 15:26:12 +0200232exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
234 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
235 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000236}
Paul Bakker33b43f12013-08-20 11:48:36 +0200237/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000238
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200239/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240void mbedtls_rsa_pkcs1_verify(data_t *message_str, int padding_mode,
241 int digest, int mod,
242 char *input_N, char *input_E,
243 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000244{
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, E;
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
249 mbedtls_rsa_init(&ctx);
250 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
251 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
254 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
255 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
256 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
257 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 TEST_ASSERT(mbedtls_rsa_pkcs1_verify(&ctx, digest, message_str->len, message_str->x,
260 result_str->x) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100261
Paul Bakkerbd51b262014-07-10 15:26:12 +0200262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
264 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000265}
Paul Bakker33b43f12013-08-20 11:48:36 +0200266/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000267
Paul Bakker821fb082009-07-12 13:26:42 +0000268
Paul Bakker33b43f12013-08-20 11:48:36 +0200269/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270void rsa_pkcs1_sign_raw(data_t *hash_result,
271 int padding_mode, int mod,
272 char *input_P, char *input_Q,
273 char *input_N, char *input_E,
274 data_t *result_str)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000275{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200276 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100278 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200279 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 mbedtls_rsa_init(&ctx);
282 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
283 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
Paul Bakker821fb082009-07-12 13:26:42 +0000284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
286 MBEDTLS_MD_NONE) == 0);
Paul Elliotte57dd2d2021-06-25 11:13:24 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 memset(output, 0x00, sizeof(output));
289 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
292 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
293 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
294 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
297 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
298 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
299 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000300
Paul Bakker821fb082009-07-12 13:26:42 +0000301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 TEST_ASSERT(mbedtls_rsa_pkcs1_sign(&ctx, &mbedtls_test_rnd_pseudo_rand,
303 &rnd_info, MBEDTLS_MD_NONE,
304 hash_result->len,
305 hash_result->x, output) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000306
Paul Bakker821fb082009-07-12 13:26:42 +0000307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
309 ctx.len, result_str->len) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000310
Paul Bakkerbd51b262014-07-10 15:26:12 +0200311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
313 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000316}
Paul Bakker33b43f12013-08-20 11:48:36 +0200317/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000318
Paul Bakker33b43f12013-08-20 11:48:36 +0200319/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320void rsa_pkcs1_verify_raw(data_t *hash_result,
321 int padding_mode, int mod,
322 char *input_N, char *input_E,
323 data_t *result_str, int correct)
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;
Paul Bakker821fb082009-07-12 13:26:42 +0000327
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100328 mbedtls_mpi N, E;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 mbedtls_rsa_init(&ctx);
332 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
333 MBEDTLS_MD_NONE) == 0);
334 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
337 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
340 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
341 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000342
Paul Bakker821fb082009-07-12 13:26:42 +0000343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 TEST_ASSERT(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE, hash_result->len, hash_result->x,
345 result_str->x) == correct);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100346
Paul Bakkerbd51b262014-07-10 15:26:12 +0200347exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
349 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000350}
Paul Bakker33b43f12013-08-20 11:48:36 +0200351/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000352
Paul Bakker33b43f12013-08-20 11:48:36 +0200353/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354void mbedtls_rsa_pkcs1_encrypt(data_t *message_str, int padding_mode,
355 int mod, char *input_N, char *input_E,
356 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000357{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200358 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200360 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000361
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100362 mbedtls_mpi N, E;
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker821fb082009-07-12 13:26:42 +0000366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 mbedtls_rsa_init(&ctx);
368 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
369 MBEDTLS_MD_NONE) == 0);
370 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
373 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
376 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
377 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000378
Paul Bakker42a29bf2009-07-07 20:18:41 +0000379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 TEST_ASSERT(mbedtls_rsa_pkcs1_encrypt(&ctx,
381 &mbedtls_test_rnd_pseudo_rand,
382 &rnd_info, message_str->len,
383 message_str->x,
384 output) == result);
385 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
388 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000389 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100390
Paul Bakkerbd51b262014-07-10 15:26:12 +0200391exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
393 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000394}
Paul Bakker33b43f12013-08-20 11:48:36 +0200395/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000396
Paul Bakker33b43f12013-08-20 11:48:36 +0200397/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100398void rsa_pkcs1_encrypt_bad_rng(data_t *message_str, int padding_mode,
399 int mod, char *input_N, char *input_E,
400 data_t *result_str, int result)
Paul Bakkera6656852010-07-18 19:47:14 +0000401{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200402 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000404
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100405 mbedtls_mpi N, E;
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
408 mbedtls_rsa_init(&ctx);
409 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
410 MBEDTLS_MD_NONE) == 0);
411 memset(output, 0x00, sizeof(output));
Paul Bakkera6656852010-07-18 19:47:14 +0000412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
414 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
417 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
418 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000419
Paul Bakkera6656852010-07-18 19:47:14 +0000420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 TEST_ASSERT(mbedtls_rsa_pkcs1_encrypt(&ctx, &mbedtls_test_rnd_zero_rand,
422 NULL, message_str->len,
423 message_str->x,
424 output) == result);
425 if (result == 0) {
Paul Bakkera6656852010-07-18 19:47:14 +0000426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
428 ctx.len, result_str->len) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000429 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100430
Paul Bakkerbd51b262014-07-10 15:26:12 +0200431exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
433 mbedtls_rsa_free(&ctx);
Paul Bakkera6656852010-07-18 19:47:14 +0000434}
Paul Bakker33b43f12013-08-20 11:48:36 +0200435/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000436
Paul Bakker33b43f12013-08-20 11:48:36 +0200437/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438void mbedtls_rsa_pkcs1_decrypt(data_t *message_str, int padding_mode,
439 int mod, char *input_P,
440 char *input_Q, char *input_N,
441 char *input_E, int max_output,
442 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000443{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200444 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000446 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200447 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100448 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
451 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 mbedtls_rsa_init(&ctx);
454 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
455 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 memset(output, 0x00, sizeof(output));
458 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000459
Paul Bakker42a29bf2009-07-07 20:18:41 +0000460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
462 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
463 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
464 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
467 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
468 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
469 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000470
Paul Bakker69998dd2009-07-11 19:15:20 +0000471 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 TEST_ASSERT(mbedtls_rsa_pkcs1_decrypt(&ctx, mbedtls_test_rnd_pseudo_rand,
474 &rnd_info,
475 &output_len, message_str->x, output,
476 max_output) == result);
477 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
480 output_len,
481 result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000482 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000483
Paul Bakkerbd51b262014-07-10 15:26:12 +0200484exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
486 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
487 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000488}
Paul Bakker33b43f12013-08-20 11:48:36 +0200489/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000490
Paul Bakker33b43f12013-08-20 11:48:36 +0200491/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100492void mbedtls_rsa_public(data_t *message_str, int mod,
493 char *input_N, char *input_E,
494 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000495{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200496 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000498
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100499 mbedtls_mpi N, E;
500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
502 mbedtls_rsa_init(&ctx);
503 mbedtls_rsa_init(&ctx2);
504 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
507 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
Gilles Peskine058d0092021-06-09 16:24:35 +0200510
511 /* Check test data consistency */
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 TEST_ASSERT(message_str->len == (size_t) (mod / 8));
513 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
514 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 TEST_ASSERT(mbedtls_rsa_public(&ctx, message_str->x, output) == result);
517 if (result == 0) {
Paul Bakker821fb082009-07-12 13:26:42 +0000518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
520 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000521 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100522
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100523 /* And now with the copy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 TEST_ASSERT(mbedtls_rsa_copy(&ctx2, &ctx) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200525 /* clear the original to be sure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 mbedtls_rsa_free(&ctx);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx2) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 memset(output, 0x00, sizeof(output));
531 TEST_ASSERT(mbedtls_rsa_public(&ctx2, message_str->x, output) == result);
532 if (result == 0) {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
535 ctx.len, result_str->len) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100536 }
537
Paul Bakkerbd51b262014-07-10 15:26:12 +0200538exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
540 mbedtls_rsa_free(&ctx);
541 mbedtls_rsa_free(&ctx2);
Paul Bakker821fb082009-07-12 13:26:42 +0000542}
Paul Bakker33b43f12013-08-20 11:48:36 +0200543/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000544
Paul Bakker33b43f12013-08-20 11:48:36 +0200545/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546void mbedtls_rsa_private(data_t *message_str, int mod,
547 char *input_P, char *input_Q,
548 char *input_N, char *input_E,
549 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000550{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200551 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100553 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200554 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200555 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
558 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
559 mbedtls_rsa_init(&ctx);
560 mbedtls_rsa_init(&ctx2);
Paul Bakker821fb082009-07-12 13:26:42 +0000561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker821fb082009-07-12 13:26:42 +0000563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
565 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
566 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
567 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
Gilles Peskine058d0092021-06-09 16:24:35 +0200570
571 /* Check test data consistency */
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 TEST_ASSERT(message_str->len == (size_t) (mod / 8));
573 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
574 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
575 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000576
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200577 /* repeat three times to test updating of blinding values */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 for (i = 0; i < 3; i++) {
579 memset(output, 0x00, sizeof(output));
580 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_pseudo_rand,
581 &rnd_info, message_str->x,
582 output) == result);
583 if (result == 0) {
Paul Bakker821fb082009-07-12 13:26:42 +0000584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
586 ctx.len,
587 result_str->len) == 0);
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200588 }
Paul Bakker821fb082009-07-12 13:26:42 +0000589 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000590
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100591 /* And now one more time with the copy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 TEST_ASSERT(mbedtls_rsa_copy(&ctx2, &ctx) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200593 /* clear the original to be sure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 mbedtls_rsa_free(&ctx);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx2) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 memset(output, 0x00, sizeof(output));
599 TEST_ASSERT(mbedtls_rsa_private(&ctx2, mbedtls_test_rnd_pseudo_rand,
600 &rnd_info, message_str->x,
601 output) == result);
602 if (result == 0) {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
605 ctx2.len,
606 result_str->len) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100607 }
608
Paul Bakkerbd51b262014-07-10 15:26:12 +0200609exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
611 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 mbedtls_rsa_free(&ctx); mbedtls_rsa_free(&ctx2);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000614}
Paul Bakker33b43f12013-08-20 11:48:36 +0200615/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000616
Paul Bakker33b43f12013-08-20 11:48:36 +0200617/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_rsa_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 memset(&ctx, 0x00, sizeof(mbedtls_rsa_context));
Paul Bakker37940d9f2009-07-10 22:38:58 +0000622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000624}
Paul Bakker33b43f12013-08-20 11:48:36 +0200625/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000626
Paul Bakker33b43f12013-08-20 11:48:36 +0200627/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628void mbedtls_rsa_check_pubkey(char *input_N, char *input_E, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000629{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100631 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
634 mbedtls_rsa_init(&ctx);
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(&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(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000641 }
642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
644 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100645
Paul Bakkerbd51b262014-07-10 15:26:12 +0200646exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
648 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000649}
Paul Bakker33b43f12013-08-20 11:48:36 +0200650/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000651
Paul Bakker33b43f12013-08-20 11:48:36 +0200652/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100653void mbedtls_rsa_check_privkey(int mod, char *input_P, char *input_Q,
654 char *input_N, char *input_E, char *input_D,
655 char *input_DP, char *input_DQ, char *input_QP,
656 int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000657{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 mbedtls_rsa_init(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000661
Paul Bakker33b43f12013-08-20 11:48:36 +0200662 ctx.len = mod / 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (strlen(input_P)) {
664 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.P, input_P) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000665 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (strlen(input_Q)) {
667 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.Q, input_Q) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000668 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (strlen(input_N)) {
670 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.N, input_N) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000671 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (strlen(input_E)) {
673 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000674 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (strlen(input_D)) {
676 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.D, input_D) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000677 }
Hanno Becker131134f2017-08-23 08:31:07 +0100678#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (strlen(input_DP)) {
680 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.DP, input_DP) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000681 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (strlen(input_DQ)) {
683 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.DQ, input_DQ) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000684 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (strlen(input_QP)) {
686 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.QP, input_QP) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000687 }
Hanno Becker131134f2017-08-23 08:31:07 +0100688#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100689 ((void) input_DP);
690 ((void) input_DQ);
691 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100692#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100695
Paul Bakkerbd51b262014-07-10 15:26:12 +0200696exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000698}
Paul Bakker33b43f12013-08-20 11:48:36 +0200699/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000700
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100701/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100702void rsa_check_pubpriv(int mod, char *input_Npub, char *input_Epub,
703 char *input_P, char *input_Q, char *input_N,
704 char *input_E, char *input_D, char *input_DP,
705 char *input_DQ, char *input_QP, int result)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 mbedtls_rsa_init(&pub);
710 mbedtls_rsa_init(&prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100711
712 pub.len = mod / 8;
713 prv.len = mod / 8;
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (strlen(input_Npub)) {
716 TEST_ASSERT(mbedtls_test_read_mpi(&pub.N, input_Npub) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100717 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (strlen(input_Epub)) {
719 TEST_ASSERT(mbedtls_test_read_mpi(&pub.E, input_Epub) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100720 }
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 if (strlen(input_P)) {
723 TEST_ASSERT(mbedtls_test_read_mpi(&prv.P, input_P) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100724 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (strlen(input_Q)) {
726 TEST_ASSERT(mbedtls_test_read_mpi(&prv.Q, input_Q) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100727 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (strlen(input_N)) {
729 TEST_ASSERT(mbedtls_test_read_mpi(&prv.N, input_N) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100730 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (strlen(input_E)) {
732 TEST_ASSERT(mbedtls_test_read_mpi(&prv.E, input_E) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100733 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (strlen(input_D)) {
735 TEST_ASSERT(mbedtls_test_read_mpi(&prv.D, input_D) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100736 }
Hanno Becker131134f2017-08-23 08:31:07 +0100737#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if (strlen(input_DP)) {
739 TEST_ASSERT(mbedtls_test_read_mpi(&prv.DP, input_DP) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100740 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (strlen(input_DQ)) {
742 TEST_ASSERT(mbedtls_test_read_mpi(&prv.DQ, input_DQ) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100743 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if (strlen(input_QP)) {
745 TEST_ASSERT(mbedtls_test_read_mpi(&prv.QP, input_QP) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100746 }
Hanno Becker131134f2017-08-23 08:31:07 +0100747#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100748 ((void) input_DP);
749 ((void) input_DQ);
750 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100751#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 TEST_ASSERT(mbedtls_rsa_check_pub_priv(&pub, &prv) == result);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100754
755exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 mbedtls_rsa_free(&pub);
757 mbedtls_rsa_free(&prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100758}
759/* END_CASE */
760
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200761/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762void mbedtls_rsa_gen_key(int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000763{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_rsa_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 mbedtls_rsa_init(&ctx);
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000766
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200767 /* This test uses an insecure RNG, suitable only for testing.
768 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 TEST_ASSERT(mbedtls_rsa_gen_key(&ctx, mbedtls_test_rnd_std_rand, NULL, nrbits,
770 exponent) == result);
771 if (result == 0) {
772 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
773 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&ctx.P, &ctx.Q) > 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000774 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100775
Paul Bakkerbd51b262014-07-10 15:26:12 +0200776exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000778}
Paul Bakker33b43f12013-08-20 11:48:36 +0200779/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000780
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +0200781/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782void mbedtls_rsa_deduce_primes(char *input_N,
783 char *input_D,
784 char *input_E,
785 char *output_P,
786 char *output_Q,
787 int corrupt, int result)
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100788{
789 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 mbedtls_mpi_init(&N);
792 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
793 mbedtls_mpi_init(&Pp); mbedtls_mpi_init(&Qp);
794 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
797 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
798 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
799 TEST_ASSERT(mbedtls_test_read_mpi(&Qp, output_P) == 0);
800 TEST_ASSERT(mbedtls_test_read_mpi(&Pp, output_Q) == 0);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (corrupt) {
803 TEST_ASSERT(mbedtls_mpi_add_int(&D, &D, 2) == 0);
804 }
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100805
806 /* Try to deduce P, Q from N, D, E only. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 TEST_ASSERT(mbedtls_rsa_deduce_primes(&N, &D, &E, &P, &Q) == result);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (!corrupt) {
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100810 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 TEST_ASSERT((mbedtls_mpi_cmp_mpi(&P, &Pp) == 0 && mbedtls_mpi_cmp_mpi(&Q, &Qp) == 0) ||
812 (mbedtls_mpi_cmp_mpi(&P, &Qp) == 0 && mbedtls_mpi_cmp_mpi(&Q, &Pp) == 0));
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100813 }
814
815exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 mbedtls_mpi_free(&N);
817 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
818 mbedtls_mpi_free(&Pp); mbedtls_mpi_free(&Qp);
819 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100820}
821/* END_CASE */
822
Hanno Becker6b4ce492017-08-23 11:00:21 +0100823/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824void mbedtls_rsa_deduce_private_exponent(char *input_P,
825 char *input_Q,
826 char *input_E,
827 char *output_D,
828 int corrupt, int result)
Hanno Becker6b4ce492017-08-23 11:00:21 +0100829{
830 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
833 mbedtls_mpi_init(&D); mbedtls_mpi_init(&Dp);
834 mbedtls_mpi_init(&E);
835 mbedtls_mpi_init(&R); mbedtls_mpi_init(&Rp);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
838 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
839 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
840 TEST_ASSERT(mbedtls_test_read_mpi(&Dp, output_D) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if (corrupt) {
Hanno Becker6b4ce492017-08-23 11:00:21 +0100843 /* Make E even */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 TEST_ASSERT(mbedtls_mpi_set_bit(&E, 0, 0) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100845 }
846
847 /* Try to deduce D from N, P, Q, E. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 TEST_ASSERT(mbedtls_rsa_deduce_private_exponent(&P, &Q,
849 &E, &D) == result);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (!corrupt) {
Hanno Becker6b4ce492017-08-23 11:00:21 +0100852 /*
853 * Check that D and Dp agree modulo LCM(P-1, Q-1).
854 */
855
856 /* Replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 TEST_ASSERT(mbedtls_mpi_sub_int(&P, &P, 1) == 0);
858 TEST_ASSERT(mbedtls_mpi_sub_int(&Q, &Q, 1) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100859
860 /* Check D == Dp modulo P-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &D, &P) == 0);
862 TEST_ASSERT(mbedtls_mpi_mod_mpi(&Rp, &Dp, &P) == 0);
863 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &Rp) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100864
865 /* Check D == Dp modulo Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &D, &Q) == 0);
867 TEST_ASSERT(mbedtls_mpi_mod_mpi(&Rp, &Dp, &Q) == 0);
868 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &Rp) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100869 }
870
871exit:
872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
874 mbedtls_mpi_free(&D); mbedtls_mpi_free(&Dp);
875 mbedtls_mpi_free(&E);
876 mbedtls_mpi_free(&R); mbedtls_mpi_free(&Rp);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100877}
878/* END_CASE */
879
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200880/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881void mbedtls_rsa_import(char *input_N,
882 char *input_P,
883 char *input_Q,
884 char *input_D,
885 char *input_E,
886 int successive,
887 int is_priv,
888 int res_check,
889 int res_complete)
Hanno Beckerc77ab892017-08-23 11:01:06 +0100890{
891 mbedtls_mpi N, P, Q, D, E;
892 mbedtls_rsa_context ctx;
893
Hanno Beckere1582a82017-09-29 11:51:05 +0100894 /* Buffers used for encryption-decryption test */
895 unsigned char *buf_orig = NULL;
896 unsigned char *buf_enc = NULL;
897 unsigned char *buf_dec = NULL;
898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 const int have_N = (strlen(input_N) > 0);
900 const int have_P = (strlen(input_P) > 0);
901 const int have_Q = (strlen(input_Q) > 0);
902 const int have_D = (strlen(input_D) > 0);
903 const int have_E = (strlen(input_E) > 0);
Hanno Becker4d6e8342017-09-29 11:50:18 +0100904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 mbedtls_rsa_init(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 mbedtls_mpi_init(&N);
908 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
909 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (have_N) {
912 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100913 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100914
915 if (have_P) {
916 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
917 }
918
919 if (have_Q) {
920 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
921 }
922
923 if (have_D) {
924 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
925 }
926
927 if (have_E) {
928 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
929 }
930
931 if (!successive) {
932 TEST_ASSERT(mbedtls_rsa_import(&ctx,
933 have_N ? &N : NULL,
934 have_P ? &P : NULL,
935 have_Q ? &Q : NULL,
936 have_D ? &D : NULL,
937 have_E ? &E : NULL) == 0);
938 } else {
Hanno Beckerc77ab892017-08-23 11:01:06 +0100939 /* Import N, P, Q, D, E separately.
940 * This should make no functional difference. */
941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 TEST_ASSERT(mbedtls_rsa_import(&ctx,
943 have_N ? &N : NULL,
944 NULL, NULL, NULL, NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 TEST_ASSERT(mbedtls_rsa_import(&ctx,
947 NULL,
948 have_P ? &P : NULL,
949 NULL, NULL, NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 TEST_ASSERT(mbedtls_rsa_import(&ctx,
952 NULL, NULL,
953 have_Q ? &Q : NULL,
954 NULL, NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 TEST_ASSERT(mbedtls_rsa_import(&ctx,
957 NULL, NULL, NULL,
958 have_D ? &D : NULL,
959 NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 TEST_ASSERT(mbedtls_rsa_import(&ctx,
962 NULL, NULL, NULL, NULL,
963 have_E ? &E : NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100964 }
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == res_complete);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100967
Hanno Beckere1582a82017-09-29 11:51:05 +0100968 /* On expected success, perform some public and private
969 * key operations to check if the key is working properly. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (res_complete == 0) {
971 if (is_priv) {
972 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == res_check);
973 } else {
974 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == res_check);
975 }
Hanno Becker04877a42017-10-11 10:01:33 +0100976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (res_check != 0) {
Hanno Becker04877a42017-10-11 10:01:33 +0100978 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 }
Hanno Beckere1582a82017-09-29 11:51:05 +0100980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 buf_orig = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
982 buf_enc = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
983 buf_dec = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
984 if (buf_orig == NULL || buf_enc == NULL || buf_dec == NULL) {
Hanno Beckere1582a82017-09-29 11:51:05 +0100985 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 }
Hanno Beckere1582a82017-09-29 11:51:05 +0100987
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200988 /* This test uses an insecure RNG, suitable only for testing.
989 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL,
991 buf_orig, mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100992
993 /* Make sure the number we're generating is smaller than the modulus */
994 buf_orig[0] = 0x00;
995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 TEST_ASSERT(mbedtls_rsa_public(&ctx, buf_orig, buf_enc) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (is_priv) {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200999 /* This test uses an insecure RNG, suitable only for testing.
1000 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_std_rand,
1002 NULL, buf_enc,
1003 buf_dec) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 TEST_ASSERT(memcmp(buf_orig, buf_dec,
1006 mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001007 }
1008 }
1009
Hanno Beckerc77ab892017-08-23 11:01:06 +01001010exit:
1011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 mbedtls_free(buf_orig);
1013 mbedtls_free(buf_enc);
1014 mbedtls_free(buf_dec);
Hanno Beckere1582a82017-09-29 11:51:05 +01001015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 mbedtls_rsa_free(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 mbedtls_mpi_free(&N);
1019 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
1020 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001021}
1022/* END_CASE */
1023
Hanno Becker417f2d62017-08-23 11:44:51 +01001024/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001025void mbedtls_rsa_export(char *input_N,
1026 char *input_P,
1027 char *input_Q,
1028 char *input_D,
1029 char *input_E,
1030 int is_priv,
1031 int successive)
Hanno Becker417f2d62017-08-23 11:44:51 +01001032{
1033 /* Original MPI's with which we set up the RSA context */
1034 mbedtls_mpi N, P, Q, D, E;
1035
1036 /* Exported MPI's */
1037 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 const int have_N = (strlen(input_N) > 0);
1040 const int have_P = (strlen(input_P) > 0);
1041 const int have_Q = (strlen(input_Q) > 0);
1042 const int have_D = (strlen(input_D) > 0);
1043 const int have_E = (strlen(input_E) > 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001044
Hanno Becker417f2d62017-08-23 11:44:51 +01001045 mbedtls_rsa_context ctx;
1046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 mbedtls_rsa_init(&ctx);
Hanno Becker417f2d62017-08-23 11:44:51 +01001048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 mbedtls_mpi_init(&N);
1050 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
1051 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Becker417f2d62017-08-23 11:44:51 +01001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 mbedtls_mpi_init(&Ne);
1054 mbedtls_mpi_init(&Pe); mbedtls_mpi_init(&Qe);
1055 mbedtls_mpi_init(&De); mbedtls_mpi_init(&Ee);
Hanno Becker417f2d62017-08-23 11:44:51 +01001056
1057 /* Setup RSA context */
1058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (have_N) {
1060 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
1061 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (have_P) {
1064 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
1065 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (have_Q) {
1068 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
1069 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (have_D) {
1072 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
1073 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (have_E) {
1076 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
1077 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 TEST_ASSERT(mbedtls_rsa_import(&ctx,
1080 strlen(input_N) ? &N : NULL,
1081 strlen(input_P) ? &P : NULL,
1082 strlen(input_Q) ? &Q : NULL,
1083 strlen(input_D) ? &D : NULL,
1084 strlen(input_E) ? &E : NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001087
1088 /*
1089 * Export parameters and compare to original ones.
1090 */
1091
1092 /* N and E must always be present. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if (!successive) {
1094 TEST_ASSERT(mbedtls_rsa_export(&ctx, &Ne, NULL, NULL, NULL, &Ee) == 0);
1095 } else {
1096 TEST_ASSERT(mbedtls_rsa_export(&ctx, &Ne, NULL, NULL, NULL, NULL) == 0);
1097 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, NULL, NULL, &Ee) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001098 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&N, &Ne) == 0);
1100 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&E, &Ee) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001101
1102 /* If we were providing enough information to setup a complete private context,
1103 * we expect to be able to export all core parameters. */
1104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (is_priv) {
1106 if (!successive) {
1107 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, &Pe, &Qe,
1108 &De, NULL) == 0);
1109 } else {
1110 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, &Pe, NULL,
1111 NULL, NULL) == 0);
1112 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, &Qe,
1113 NULL, NULL) == 0);
1114 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, NULL,
1115 &De, NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001116 }
1117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (have_P) {
1119 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P, &Pe) == 0);
1120 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (have_Q) {
1123 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Q, &Qe) == 0);
1124 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if (have_D) {
1127 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&D, &De) == 0);
1128 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001129
1130 /* While at it, perform a sanity check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 TEST_ASSERT(mbedtls_rsa_validate_params(&Ne, &Pe, &Qe, &De, &Ee,
1132 NULL, NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001133 }
1134
1135exit:
1136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 mbedtls_rsa_free(&ctx);
Hanno Becker417f2d62017-08-23 11:44:51 +01001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 mbedtls_mpi_free(&N);
1140 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
1141 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Becker417f2d62017-08-23 11:44:51 +01001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 mbedtls_mpi_free(&Ne);
1144 mbedtls_mpi_free(&Pe); mbedtls_mpi_free(&Qe);
1145 mbedtls_mpi_free(&De); mbedtls_mpi_free(&Ee);
Hanno Becker417f2d62017-08-23 11:44:51 +01001146}
1147/* END_CASE */
1148
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001149/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001150void mbedtls_rsa_validate_params(char *input_N,
1151 char *input_P,
1152 char *input_Q,
1153 char *input_D,
1154 char *input_E,
1155 int prng, int result)
Hanno Beckerce002632017-08-23 13:22:36 +01001156{
1157 /* Original MPI's with which we set up the RSA context */
1158 mbedtls_mpi N, P, Q, D, E;
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 const int have_N = (strlen(input_N) > 0);
1161 const int have_P = (strlen(input_P) > 0);
1162 const int have_Q = (strlen(input_Q) > 0);
1163 const int have_D = (strlen(input_D) > 0);
1164 const int have_E = (strlen(input_E) > 0);
Hanno Beckerce002632017-08-23 13:22:36 +01001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 mbedtls_mpi_init(&N);
1167 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
1168 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerce002632017-08-23 13:22:36 +01001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (have_N) {
1171 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
1172 }
Hanno Beckerce002632017-08-23 13:22:36 +01001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (have_P) {
1175 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
1176 }
Hanno Beckerce002632017-08-23 13:22:36 +01001177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (have_Q) {
1179 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
1180 }
Hanno Beckerce002632017-08-23 13:22:36 +01001181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (have_D) {
1183 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
1184 }
Hanno Beckerce002632017-08-23 13:22:36 +01001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if (have_E) {
1187 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
1188 }
Hanno Beckerce002632017-08-23 13:22:36 +01001189
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001190 /* This test uses an insecure RNG, suitable only for testing.
1191 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 TEST_ASSERT(mbedtls_rsa_validate_params(have_N ? &N : NULL,
1193 have_P ? &P : NULL,
1194 have_Q ? &Q : NULL,
1195 have_D ? &D : NULL,
1196 have_E ? &E : NULL,
1197 prng ? mbedtls_test_rnd_std_rand : NULL,
1198 prng ? NULL : NULL) == result);
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001199
Hanno Beckerce002632017-08-23 13:22:36 +01001200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 mbedtls_mpi_free(&N);
1202 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
1203 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerce002632017-08-23 13:22:36 +01001204}
1205/* END_CASE */
1206
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +02001207/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001208void mbedtls_rsa_export_raw(data_t *input_N, data_t *input_P,
1209 data_t *input_Q, data_t *input_D,
1210 data_t *input_E, int is_priv,
1211 int successive)
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001212{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001213 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001214 unsigned char bufNe[256];
1215 unsigned char bufPe[128];
1216 unsigned char bufQe[128];
1217 unsigned char bufDe[256];
1218 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001219
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001220 mbedtls_rsa_context ctx;
1221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 mbedtls_rsa_init(&ctx);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001223
1224 /* Setup RSA context */
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1226 input_N->len ? input_N->x : NULL, input_N->len,
1227 input_P->len ? input_P->x : NULL, input_P->len,
1228 input_Q->len ? input_Q->x : NULL, input_Q->len,
1229 input_D->len ? input_D->x : NULL, input_D->len,
1230 input_E->len ? input_E->x : NULL, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001233
1234 /*
1235 * Export parameters and compare to original ones.
1236 */
1237
1238 /* N and E must always be present. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if (!successive) {
1240 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, bufNe, input_N->len,
1241 NULL, 0, NULL, 0, NULL, 0,
1242 bufEe, input_E->len) == 0);
1243 } else {
1244 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, bufNe, input_N->len,
1245 NULL, 0, NULL, 0, NULL, 0,
1246 NULL, 0) == 0);
1247 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1248 NULL, 0, NULL, 0, NULL, 0,
1249 bufEe, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001250 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 TEST_ASSERT(memcmp(input_N->x, bufNe, input_N->len) == 0);
1252 TEST_ASSERT(memcmp(input_E->x, bufEe, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001253
1254 /* If we were providing enough information to setup a complete private context,
1255 * we expect to be able to export all core parameters. */
1256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (is_priv) {
1258 if (!successive) {
1259 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1260 bufPe, input_P->len ? input_P->len : sizeof(bufPe),
1261 bufQe, input_Q->len ? input_Q->len : sizeof(bufQe),
1262 bufDe, input_D->len ? input_D->len : sizeof(bufDe),
1263 NULL, 0) == 0);
1264 } else {
1265 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1266 bufPe, input_P->len ? input_P->len : sizeof(bufPe),
1267 NULL, 0, NULL, 0,
1268 NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0, NULL, 0,
1271 bufQe, input_Q->len ? input_Q->len : sizeof(bufQe),
1272 NULL, 0, NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0, NULL, 0, NULL, 0,
1275 bufDe, input_D->len ? input_D->len : sizeof(bufDe),
1276 NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001277 }
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if (input_P->len) {
1280 TEST_ASSERT(memcmp(input_P->x, bufPe, input_P->len) == 0);
1281 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (input_Q->len) {
1284 TEST_ASSERT(memcmp(input_Q->x, bufQe, input_Q->len) == 0);
1285 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 if (input_D->len) {
1288 TEST_ASSERT(memcmp(input_D->x, bufDe, input_D->len) == 0);
1289 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001290
1291 }
1292
1293exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 mbedtls_rsa_free(&ctx);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001295}
1296/* END_CASE */
1297
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001298/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001299void mbedtls_rsa_import_raw(data_t *input_N,
1300 data_t *input_P, data_t *input_Q,
1301 data_t *input_D, data_t *input_E,
1302 int successive,
1303 int is_priv,
1304 int res_check,
1305 int res_complete)
Hanno Beckerc77ab892017-08-23 11:01:06 +01001306{
Hanno Beckere1582a82017-09-29 11:51:05 +01001307 /* Buffers used for encryption-decryption test */
1308 unsigned char *buf_orig = NULL;
1309 unsigned char *buf_enc = NULL;
1310 unsigned char *buf_dec = NULL;
1311
Hanno Beckerc77ab892017-08-23 11:01:06 +01001312 mbedtls_rsa_context ctx;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 mbedtls_rsa_init(&ctx);
Hanno Becker3f3ae852017-10-02 10:08:39 +01001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 if (!successive) {
1317 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1318 (input_N->len > 0) ? input_N->x : NULL, input_N->len,
1319 (input_P->len > 0) ? input_P->x : NULL, input_P->len,
1320 (input_Q->len > 0) ? input_Q->x : NULL, input_Q->len,
1321 (input_D->len > 0) ? input_D->x : NULL, input_D->len,
1322 (input_E->len > 0) ? input_E->x : NULL,
1323 input_E->len) == 0);
1324 } else {
Hanno Beckerc77ab892017-08-23 11:01:06 +01001325 /* Import N, P, Q, D, E separately.
1326 * This should make no functional difference. */
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1329 (input_N->len > 0) ? input_N->x : NULL, input_N->len,
1330 NULL, 0, NULL, 0, NULL, 0, NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1333 NULL, 0,
1334 (input_P->len > 0) ? input_P->x : NULL, input_P->len,
1335 NULL, 0, NULL, 0, NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1338 NULL, 0, NULL, 0,
1339 (input_Q->len > 0) ? input_Q->x : NULL, input_Q->len,
1340 NULL, 0, NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1343 NULL, 0, NULL, 0, NULL, 0,
1344 (input_D->len > 0) ? input_D->x : NULL, input_D->len,
1345 NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1348 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1349 (input_E->len > 0) ? input_E->x : NULL,
1350 input_E->len) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001351 }
1352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == res_complete);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001354
Hanno Beckere1582a82017-09-29 11:51:05 +01001355 /* On expected success, perform some public and private
1356 * key operations to check if the key is working properly. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 if (res_complete == 0) {
1358 if (is_priv) {
1359 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == res_check);
1360 } else {
1361 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == res_check);
1362 }
Hanno Becker04877a42017-10-11 10:01:33 +01001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 if (res_check != 0) {
Hanno Becker04877a42017-10-11 10:01:33 +01001365 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 }
Hanno Beckere1582a82017-09-29 11:51:05 +01001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 buf_orig = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1369 buf_enc = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1370 buf_dec = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1371 if (buf_orig == NULL || buf_enc == NULL || buf_dec == NULL) {
Hanno Beckere1582a82017-09-29 11:51:05 +01001372 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 }
Hanno Beckere1582a82017-09-29 11:51:05 +01001374
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001375 /* This test uses an insecure RNG, suitable only for testing.
1376 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL,
1378 buf_orig, mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001379
1380 /* Make sure the number we're generating is smaller than the modulus */
1381 buf_orig[0] = 0x00;
1382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 TEST_ASSERT(mbedtls_rsa_public(&ctx, buf_orig, buf_enc) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if (is_priv) {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001386 /* This test uses an insecure RNG, suitable only for testing.
1387 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_std_rand,
1389 NULL, buf_enc,
1390 buf_dec) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 TEST_ASSERT(memcmp(buf_orig, buf_dec,
1393 mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001394 }
1395 }
1396
Hanno Beckerc77ab892017-08-23 11:01:06 +01001397exit:
1398
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 mbedtls_free(buf_orig);
1400 mbedtls_free(buf_enc);
1401 mbedtls_free(buf_dec);
Hanno Becker3f3ae852017-10-02 10:08:39 +01001402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 mbedtls_rsa_free(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001404}
1405/* END_CASE */
1406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +01001408void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +00001409{
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 TEST_ASSERT(mbedtls_rsa_self_test(1) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +00001411}
Paul Bakker33b43f12013-08-20 11:48:36 +02001412/* END_CASE */