blob: 73eb5b257da932d8b5a92ef08eb805210e514f31 [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
Yanray Wangac361152023-03-15 16:55:32 +080024 TEST_EQUAL(mbedtls_rsa_get_padding_mode(&ctx), MBEDTLS_RSA_PKCS_V15);
Yanray Wangd41684e2023-03-17 18:54:22 +080025 TEST_EQUAL(mbedtls_rsa_get_md_alg(&ctx), MBEDTLS_MD_NONE);
Yanray Wangac361152023-03-15 16:55:32 +080026
Gilles Peskine449bd832023-01-11 14:50:10 +010027 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
28 invalid_padding,
29 MBEDTLS_MD_NONE),
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_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
Gilles Peskine449bd832023-01-11 14:50:10 +010037 TEST_EQUAL(mbedtls_rsa_pkcs1_sign(&ctx, NULL,
38 NULL, MBEDTLS_MD_NONE,
39 buf_len,
40 NULL, buf),
41 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010042
Gilles Peskine449bd832023-01-11 14:50:10 +010043 TEST_EQUAL(mbedtls_rsa_pkcs1_sign(&ctx, NULL,
44 NULL, MBEDTLS_MD_SHA256,
45 0,
46 NULL, buf),
47 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 TEST_EQUAL(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE,
50 buf_len,
51 NULL, buf),
52 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010053
Gilles Peskine449bd832023-01-11 14:50:10 +010054 TEST_EQUAL(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_SHA256,
55 0,
56 NULL, buf),
57 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010058
Ronald Cron3a0375f2021-06-08 10:22:28 +020059#if !defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +010060 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
61 MBEDTLS_RSA_PKCS_V15,
62 MBEDTLS_MD_NONE),
63 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cron3a0375f2021-06-08 10:22:28 +020064#endif
65
Tuvshinzaya Erdenekhuufe7524d2022-09-01 16:07:18 +010066#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +010067 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
68 NULL, MBEDTLS_MD_NONE,
69 buf_len,
70 NULL, buf),
71 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
74 NULL, MBEDTLS_MD_SHA256,
75 0,
76 NULL, buf),
77 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_NONE,
80 buf_len,
81 NULL, buf),
82 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_SHA256,
85 0,
86 NULL, buf),
87 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010088
89
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010090#endif
91
Ronald Cron3a0375f2021-06-08 10:22:28 +020092#if !defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +010093 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
94 MBEDTLS_RSA_PKCS_V21,
95 MBEDTLS_MD_NONE),
96 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cron3a0375f2021-06-08 10:22:28 +020097#endif
98
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010099#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 TEST_EQUAL(mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
101 MBEDTLS_MD_NONE, buf_len,
102 NULL, buf_len,
103 buf),
104 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 TEST_EQUAL(mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
107 MBEDTLS_MD_SHA256, 0,
108 NULL, buf_len,
109 buf),
110 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_NONE,
113 buf_len, NULL,
114 MBEDTLS_MD_NONE,
115 buf_len, buf),
116 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_SHA256,
119 0, NULL,
120 MBEDTLS_MD_NONE,
121 buf_len, buf),
122 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_NONE,
125 buf_len,
126 NULL, buf),
127 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_SHA256,
130 0,
131 NULL, buf),
132 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100133#endif
134
Ronald Cronea7631b2021-06-03 18:51:59 +0200135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_rsa_free(&ctx);
Ronald Cronea7631b2021-06-03 18:51:59 +0200137}
138/* END_CASE */
139
140/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100141void rsa_init_free(int reinit)
Gilles Peskine914afe12021-02-01 17:55:24 +0100142{
143 mbedtls_rsa_context ctx;
144
145 /* Double free is not explicitly documented to work, but we rely on it
146 * even inside the library so that you can call mbedtls_rsa_free()
147 * unconditionally on an error path without checking whether it has
148 * already been called in the success path. */
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 mbedtls_rsa_init(&ctx);
151 mbedtls_rsa_free(&ctx);
Gilles Peskine914afe12021-02-01 17:55:24 +0100152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (reinit) {
154 mbedtls_rsa_init(&ctx);
155 }
156 mbedtls_rsa_free(&ctx);
Gilles Peskine914afe12021-02-01 17:55:24 +0100157
158 /* This test case always succeeds, functionally speaking. A plausible
159 * bug might trigger an invalid pointer dereference or a memory leak. */
160 goto exit;
161}
162/* END_CASE */
163
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200164/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165void mbedtls_rsa_pkcs1_sign(data_t *message_str, int padding_mode,
166 int digest, int mod, char *input_P,
167 char *input_Q, char *input_N, char *input_E,
168 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000169{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200170 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100172 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200173 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
176 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
177 mbedtls_rsa_init(&ctx);
178 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
179 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 memset(output, 0x00, sizeof(output));
182 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
185 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
186 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
187 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
190 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
191 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
192 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 TEST_ASSERT(mbedtls_rsa_pkcs1_sign(
195 &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
196 digest, message_str->len, message_str->x,
197 output) == result);
198 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
201 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000202 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000203
Paul Bakkerbd51b262014-07-10 15:26:12 +0200204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
206 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
207 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000208}
Paul Bakker33b43f12013-08-20 11:48:36 +0200209/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000210
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200211/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212void mbedtls_rsa_pkcs1_verify(data_t *message_str, int padding_mode,
213 int digest, int mod,
214 char *input_N, char *input_E,
215 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000216{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100218 mbedtls_mpi N, E;
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
221 mbedtls_rsa_init(&ctx);
222 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
223 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
226 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
227 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
228 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
229 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 TEST_ASSERT(mbedtls_rsa_pkcs1_verify(&ctx, digest, message_str->len, message_str->x,
232 result_str->x) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100233
Paul Bakkerbd51b262014-07-10 15:26:12 +0200234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
236 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000237}
Paul Bakker33b43f12013-08-20 11:48:36 +0200238/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000239
Paul Bakker821fb082009-07-12 13:26:42 +0000240
Paul Bakker33b43f12013-08-20 11:48:36 +0200241/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242void rsa_pkcs1_sign_raw(data_t *hash_result,
243 int padding_mode, int mod,
244 char *input_P, char *input_Q,
245 char *input_N, char *input_E,
246 data_t *result_str)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000247{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200248 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100250 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200251 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_rsa_init(&ctx);
254 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
255 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
Paul Bakker821fb082009-07-12 13:26:42 +0000256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
258 MBEDTLS_MD_NONE) == 0);
Paul Elliotte57dd2d2021-06-25 11:13:24 +0100259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 memset(output, 0x00, sizeof(output));
261 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
264 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
265 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
266 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
269 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
270 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
271 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000272
Paul Bakker821fb082009-07-12 13:26:42 +0000273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 TEST_ASSERT(mbedtls_rsa_pkcs1_sign(&ctx, &mbedtls_test_rnd_pseudo_rand,
275 &rnd_info, MBEDTLS_MD_NONE,
276 hash_result->len,
277 hash_result->x, output) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000278
Paul Bakker821fb082009-07-12 13:26:42 +0000279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
281 ctx.len, result_str->len) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000282
Paul Bakkerbd51b262014-07-10 15:26:12 +0200283exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
285 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000288}
Paul Bakker33b43f12013-08-20 11:48:36 +0200289/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000290
Paul Bakker33b43f12013-08-20 11:48:36 +0200291/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292void rsa_pkcs1_verify_raw(data_t *hash_result,
293 int padding_mode, int mod,
294 char *input_N, char *input_E,
295 data_t *result_str, int correct)
Paul Bakker821fb082009-07-12 13:26:42 +0000296{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200297 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000299
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100300 mbedtls_mpi N, E;
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 mbedtls_rsa_init(&ctx);
304 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
305 MBEDTLS_MD_NONE) == 0);
306 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
309 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
312 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
313 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000314
Paul Bakker821fb082009-07-12 13:26:42 +0000315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 TEST_ASSERT(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE, hash_result->len, hash_result->x,
317 result_str->x) == correct);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100318
Paul Bakkerbd51b262014-07-10 15:26:12 +0200319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
321 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000322}
Paul Bakker33b43f12013-08-20 11:48:36 +0200323/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000324
Paul Bakker33b43f12013-08-20 11:48:36 +0200325/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326void mbedtls_rsa_pkcs1_encrypt(data_t *message_str, int padding_mode,
327 int mod, char *input_N, char *input_E,
328 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000329{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200330 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200332 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000333
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100334 mbedtls_mpi N, E;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker821fb082009-07-12 13:26:42 +0000338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 mbedtls_rsa_init(&ctx);
340 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
341 MBEDTLS_MD_NONE) == 0);
342 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
345 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
348 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
349 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000350
Paul Bakker42a29bf2009-07-07 20:18:41 +0000351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 TEST_ASSERT(mbedtls_rsa_pkcs1_encrypt(&ctx,
353 &mbedtls_test_rnd_pseudo_rand,
354 &rnd_info, message_str->len,
355 message_str->x,
356 output) == result);
357 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
360 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000361 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100362
Paul Bakkerbd51b262014-07-10 15:26:12 +0200363exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
365 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000366}
Paul Bakker33b43f12013-08-20 11:48:36 +0200367/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000368
Paul Bakker33b43f12013-08-20 11:48:36 +0200369/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370void rsa_pkcs1_encrypt_bad_rng(data_t *message_str, int padding_mode,
371 int mod, char *input_N, char *input_E,
372 data_t *result_str, int result)
Paul Bakkera6656852010-07-18 19:47:14 +0000373{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200374 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000376
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100377 mbedtls_mpi N, E;
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
380 mbedtls_rsa_init(&ctx);
381 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
382 MBEDTLS_MD_NONE) == 0);
383 memset(output, 0x00, sizeof(output));
Paul Bakkera6656852010-07-18 19:47:14 +0000384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
386 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
389 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
390 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000391
Paul Bakkera6656852010-07-18 19:47:14 +0000392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 TEST_ASSERT(mbedtls_rsa_pkcs1_encrypt(&ctx, &mbedtls_test_rnd_zero_rand,
394 NULL, message_str->len,
395 message_str->x,
396 output) == result);
397 if (result == 0) {
Paul Bakkera6656852010-07-18 19:47:14 +0000398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
400 ctx.len, result_str->len) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000401 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100402
Paul Bakkerbd51b262014-07-10 15:26:12 +0200403exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
405 mbedtls_rsa_free(&ctx);
Paul Bakkera6656852010-07-18 19:47:14 +0000406}
Paul Bakker33b43f12013-08-20 11:48:36 +0200407/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000408
Paul Bakker33b43f12013-08-20 11:48:36 +0200409/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410void mbedtls_rsa_pkcs1_decrypt(data_t *message_str, int padding_mode,
411 int mod, char *input_P,
412 char *input_Q, char *input_N,
413 char *input_E, int max_output,
414 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000415{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200416 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000418 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200419 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100420 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
423 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 mbedtls_rsa_init(&ctx);
426 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
427 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 memset(output, 0x00, sizeof(output));
430 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000431
Paul Bakker42a29bf2009-07-07 20:18:41 +0000432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
434 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
435 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
436 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
439 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
440 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
441 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000442
Paul Bakker69998dd2009-07-11 19:15:20 +0000443 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 TEST_ASSERT(mbedtls_rsa_pkcs1_decrypt(&ctx, mbedtls_test_rnd_pseudo_rand,
446 &rnd_info,
447 &output_len, message_str->x, output,
448 max_output) == result);
449 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
452 output_len,
453 result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000454 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000455
Paul Bakkerbd51b262014-07-10 15:26:12 +0200456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
458 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
459 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000460}
Paul Bakker33b43f12013-08-20 11:48:36 +0200461/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000462
Paul Bakker33b43f12013-08-20 11:48:36 +0200463/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100464void mbedtls_rsa_public(data_t *message_str, int mod,
465 char *input_N, char *input_E,
466 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000467{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200468 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000470
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100471 mbedtls_mpi N, E;
472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
474 mbedtls_rsa_init(&ctx);
475 mbedtls_rsa_init(&ctx2);
476 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
479 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
Gilles Peskine058d0092021-06-09 16:24:35 +0200482
483 /* Check test data consistency */
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 TEST_ASSERT(message_str->len == (size_t) (mod / 8));
485 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
486 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 TEST_ASSERT(mbedtls_rsa_public(&ctx, message_str->x, output) == result);
489 if (result == 0) {
Paul Bakker821fb082009-07-12 13:26:42 +0000490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
492 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000493 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100494
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100495 /* And now with the copy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 TEST_ASSERT(mbedtls_rsa_copy(&ctx2, &ctx) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200497 /* clear the original to be sure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 mbedtls_rsa_free(&ctx);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx2) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 memset(output, 0x00, sizeof(output));
503 TEST_ASSERT(mbedtls_rsa_public(&ctx2, message_str->x, output) == result);
504 if (result == 0) {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
507 ctx.len, result_str->len) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100508 }
509
Paul Bakkerbd51b262014-07-10 15:26:12 +0200510exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
512 mbedtls_rsa_free(&ctx);
513 mbedtls_rsa_free(&ctx2);
Paul Bakker821fb082009-07-12 13:26:42 +0000514}
Paul Bakker33b43f12013-08-20 11:48:36 +0200515/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000516
Paul Bakker33b43f12013-08-20 11:48:36 +0200517/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100518void mbedtls_rsa_private(data_t *message_str, int mod,
519 char *input_P, char *input_Q,
520 char *input_N, char *input_E,
521 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000522{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200523 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100525 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200526 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200527 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
530 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
531 mbedtls_rsa_init(&ctx);
532 mbedtls_rsa_init(&ctx2);
Paul Bakker821fb082009-07-12 13:26:42 +0000533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker821fb082009-07-12 13:26:42 +0000535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
537 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
538 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
539 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
Gilles Peskine058d0092021-06-09 16:24:35 +0200542
543 /* Check test data consistency */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 TEST_ASSERT(message_str->len == (size_t) (mod / 8));
545 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
546 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
547 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000548
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200549 /* repeat three times to test updating of blinding values */
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 for (i = 0; i < 3; i++) {
551 memset(output, 0x00, sizeof(output));
552 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_pseudo_rand,
553 &rnd_info, message_str->x,
554 output) == result);
555 if (result == 0) {
Paul Bakker821fb082009-07-12 13:26:42 +0000556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
558 ctx.len,
559 result_str->len) == 0);
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200560 }
Paul Bakker821fb082009-07-12 13:26:42 +0000561 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000562
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100563 /* And now one more time with the copy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 TEST_ASSERT(mbedtls_rsa_copy(&ctx2, &ctx) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200565 /* clear the original to be sure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 mbedtls_rsa_free(&ctx);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx2) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 memset(output, 0x00, sizeof(output));
571 TEST_ASSERT(mbedtls_rsa_private(&ctx2, mbedtls_test_rnd_pseudo_rand,
572 &rnd_info, message_str->x,
573 output) == result);
574 if (result == 0) {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
577 ctx2.len,
578 result_str->len) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100579 }
580
Paul Bakkerbd51b262014-07-10 15:26:12 +0200581exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
583 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 mbedtls_rsa_free(&ctx); mbedtls_rsa_free(&ctx2);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000586}
Paul Bakker33b43f12013-08-20 11:48:36 +0200587/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000588
Paul Bakker33b43f12013-08-20 11:48:36 +0200589/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000591{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_rsa_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 memset(&ctx, 0x00, sizeof(mbedtls_rsa_context));
Paul Bakker37940d9f2009-07-10 22:38:58 +0000594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000596}
Paul Bakker33b43f12013-08-20 11:48:36 +0200597/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000598
Paul Bakker33b43f12013-08-20 11:48:36 +0200599/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100600void mbedtls_rsa_check_pubkey(char *input_N, char *input_E, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000601{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100603 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
606 mbedtls_rsa_init(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (strlen(input_N)) {
609 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000610 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (strlen(input_E)) {
612 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000613 }
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
616 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100617
Paul Bakkerbd51b262014-07-10 15:26:12 +0200618exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
620 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000621}
Paul Bakker33b43f12013-08-20 11:48:36 +0200622/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000623
Paul Bakker33b43f12013-08-20 11:48:36 +0200624/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100625void mbedtls_rsa_check_privkey(int mod, char *input_P, char *input_Q,
626 char *input_N, char *input_E, char *input_D,
627 char *input_DP, char *input_DQ, char *input_QP,
628 int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000629{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 mbedtls_rsa_init(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000633
Paul Bakker33b43f12013-08-20 11:48:36 +0200634 ctx.len = mod / 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (strlen(input_P)) {
636 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.P, input_P) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000637 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (strlen(input_Q)) {
639 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.Q, input_Q) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000640 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (strlen(input_N)) {
642 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.N, input_N) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000643 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (strlen(input_E)) {
645 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000646 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (strlen(input_D)) {
648 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.D, input_D) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000649 }
Hanno Becker131134f2017-08-23 08:31:07 +0100650#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (strlen(input_DP)) {
652 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.DP, input_DP) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000653 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (strlen(input_DQ)) {
655 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.DQ, input_DQ) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000656 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (strlen(input_QP)) {
658 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.QP, input_QP) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000659 }
Hanno Becker131134f2017-08-23 08:31:07 +0100660#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100661 ((void) input_DP);
662 ((void) input_DQ);
663 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100664#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100667
Paul Bakkerbd51b262014-07-10 15:26:12 +0200668exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000670}
Paul Bakker33b43f12013-08-20 11:48:36 +0200671/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000672
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100673/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674void rsa_check_pubpriv(int mod, char *input_Npub, char *input_Epub,
675 char *input_P, char *input_Q, char *input_N,
676 char *input_E, char *input_D, char *input_DP,
677 char *input_DQ, char *input_QP, int result)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100678{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 mbedtls_rsa_init(&pub);
682 mbedtls_rsa_init(&prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100683
684 pub.len = mod / 8;
685 prv.len = mod / 8;
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (strlen(input_Npub)) {
688 TEST_ASSERT(mbedtls_test_read_mpi(&pub.N, input_Npub) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100689 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (strlen(input_Epub)) {
691 TEST_ASSERT(mbedtls_test_read_mpi(&pub.E, input_Epub) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100692 }
693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (strlen(input_P)) {
695 TEST_ASSERT(mbedtls_test_read_mpi(&prv.P, input_P) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (strlen(input_Q)) {
698 TEST_ASSERT(mbedtls_test_read_mpi(&prv.Q, input_Q) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100699 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (strlen(input_N)) {
701 TEST_ASSERT(mbedtls_test_read_mpi(&prv.N, input_N) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100702 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (strlen(input_E)) {
704 TEST_ASSERT(mbedtls_test_read_mpi(&prv.E, input_E) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100705 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (strlen(input_D)) {
707 TEST_ASSERT(mbedtls_test_read_mpi(&prv.D, input_D) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100708 }
Hanno Becker131134f2017-08-23 08:31:07 +0100709#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (strlen(input_DP)) {
711 TEST_ASSERT(mbedtls_test_read_mpi(&prv.DP, input_DP) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100712 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (strlen(input_DQ)) {
714 TEST_ASSERT(mbedtls_test_read_mpi(&prv.DQ, input_DQ) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100715 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (strlen(input_QP)) {
717 TEST_ASSERT(mbedtls_test_read_mpi(&prv.QP, input_QP) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100718 }
Hanno Becker131134f2017-08-23 08:31:07 +0100719#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100720 ((void) input_DP);
721 ((void) input_DQ);
722 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100723#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 TEST_ASSERT(mbedtls_rsa_check_pub_priv(&pub, &prv) == result);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100726
727exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 mbedtls_rsa_free(&pub);
729 mbedtls_rsa_free(&prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100730}
731/* END_CASE */
732
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200733/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100734void mbedtls_rsa_gen_key(int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000735{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_rsa_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 mbedtls_rsa_init(&ctx);
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000738
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200739 /* This test uses an insecure RNG, suitable only for testing.
740 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 TEST_ASSERT(mbedtls_rsa_gen_key(&ctx, mbedtls_test_rnd_std_rand, NULL, nrbits,
742 exponent) == result);
743 if (result == 0) {
744 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
745 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&ctx.P, &ctx.Q) > 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000746 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100747
Paul Bakkerbd51b262014-07-10 15:26:12 +0200748exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000750}
Paul Bakker33b43f12013-08-20 11:48:36 +0200751/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000752
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +0200753/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754void mbedtls_rsa_deduce_primes(char *input_N,
755 char *input_D,
756 char *input_E,
757 char *output_P,
758 char *output_Q,
759 int corrupt, int result)
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100760{
761 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 mbedtls_mpi_init(&N);
764 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
765 mbedtls_mpi_init(&Pp); mbedtls_mpi_init(&Qp);
766 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
769 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
770 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
771 TEST_ASSERT(mbedtls_test_read_mpi(&Qp, output_P) == 0);
772 TEST_ASSERT(mbedtls_test_read_mpi(&Pp, output_Q) == 0);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (corrupt) {
775 TEST_ASSERT(mbedtls_mpi_add_int(&D, &D, 2) == 0);
776 }
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100777
778 /* Try to deduce P, Q from N, D, E only. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 TEST_ASSERT(mbedtls_rsa_deduce_primes(&N, &D, &E, &P, &Q) == result);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (!corrupt) {
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100782 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 TEST_ASSERT((mbedtls_mpi_cmp_mpi(&P, &Pp) == 0 && mbedtls_mpi_cmp_mpi(&Q, &Qp) == 0) ||
784 (mbedtls_mpi_cmp_mpi(&P, &Qp) == 0 && mbedtls_mpi_cmp_mpi(&Q, &Pp) == 0));
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100785 }
786
787exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 mbedtls_mpi_free(&N);
789 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
790 mbedtls_mpi_free(&Pp); mbedtls_mpi_free(&Qp);
791 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100792}
793/* END_CASE */
794
Hanno Becker6b4ce492017-08-23 11:00:21 +0100795/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796void mbedtls_rsa_deduce_private_exponent(char *input_P,
797 char *input_Q,
798 char *input_E,
799 char *output_D,
800 int corrupt, int result)
Hanno Becker6b4ce492017-08-23 11:00:21 +0100801{
802 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
805 mbedtls_mpi_init(&D); mbedtls_mpi_init(&Dp);
806 mbedtls_mpi_init(&E);
807 mbedtls_mpi_init(&R); mbedtls_mpi_init(&Rp);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
810 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
811 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
812 TEST_ASSERT(mbedtls_test_read_mpi(&Dp, output_D) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (corrupt) {
Hanno Becker6b4ce492017-08-23 11:00:21 +0100815 /* Make E even */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 TEST_ASSERT(mbedtls_mpi_set_bit(&E, 0, 0) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100817 }
818
819 /* Try to deduce D from N, P, Q, E. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 TEST_ASSERT(mbedtls_rsa_deduce_private_exponent(&P, &Q,
821 &E, &D) == result);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if (!corrupt) {
Hanno Becker6b4ce492017-08-23 11:00:21 +0100824 /*
825 * Check that D and Dp agree modulo LCM(P-1, Q-1).
826 */
827
828 /* Replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 TEST_ASSERT(mbedtls_mpi_sub_int(&P, &P, 1) == 0);
830 TEST_ASSERT(mbedtls_mpi_sub_int(&Q, &Q, 1) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100831
832 /* Check D == Dp modulo P-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &D, &P) == 0);
834 TEST_ASSERT(mbedtls_mpi_mod_mpi(&Rp, &Dp, &P) == 0);
835 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &Rp) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100836
837 /* Check D == Dp modulo Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &D, &Q) == 0);
839 TEST_ASSERT(mbedtls_mpi_mod_mpi(&Rp, &Dp, &Q) == 0);
840 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &Rp) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100841 }
842
843exit:
844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
846 mbedtls_mpi_free(&D); mbedtls_mpi_free(&Dp);
847 mbedtls_mpi_free(&E);
848 mbedtls_mpi_free(&R); mbedtls_mpi_free(&Rp);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100849}
850/* END_CASE */
851
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200852/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853void mbedtls_rsa_import(char *input_N,
854 char *input_P,
855 char *input_Q,
856 char *input_D,
857 char *input_E,
858 int successive,
859 int is_priv,
860 int res_check,
861 int res_complete)
Hanno Beckerc77ab892017-08-23 11:01:06 +0100862{
863 mbedtls_mpi N, P, Q, D, E;
864 mbedtls_rsa_context ctx;
865
Hanno Beckere1582a82017-09-29 11:51:05 +0100866 /* Buffers used for encryption-decryption test */
867 unsigned char *buf_orig = NULL;
868 unsigned char *buf_enc = NULL;
869 unsigned char *buf_dec = NULL;
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 const int have_N = (strlen(input_N) > 0);
872 const int have_P = (strlen(input_P) > 0);
873 const int have_Q = (strlen(input_Q) > 0);
874 const int have_D = (strlen(input_D) > 0);
875 const int have_E = (strlen(input_E) > 0);
Hanno Becker4d6e8342017-09-29 11:50:18 +0100876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 mbedtls_rsa_init(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 mbedtls_mpi_init(&N);
880 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
881 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if (have_N) {
884 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100885 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100886
887 if (have_P) {
888 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
889 }
890
891 if (have_Q) {
892 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
893 }
894
895 if (have_D) {
896 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
897 }
898
899 if (have_E) {
900 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
901 }
902
903 if (!successive) {
904 TEST_ASSERT(mbedtls_rsa_import(&ctx,
905 have_N ? &N : NULL,
906 have_P ? &P : NULL,
907 have_Q ? &Q : NULL,
908 have_D ? &D : NULL,
909 have_E ? &E : NULL) == 0);
910 } else {
Hanno Beckerc77ab892017-08-23 11:01:06 +0100911 /* Import N, P, Q, D, E separately.
912 * This should make no functional difference. */
913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 TEST_ASSERT(mbedtls_rsa_import(&ctx,
915 have_N ? &N : NULL,
916 NULL, 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,
920 have_P ? &P : NULL,
921 NULL, 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,
925 have_Q ? &Q : NULL,
926 NULL, 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,
930 have_D ? &D : NULL,
931 NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 TEST_ASSERT(mbedtls_rsa_import(&ctx,
934 NULL, NULL, NULL, NULL,
935 have_E ? &E : NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100936 }
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == res_complete);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100939
Hanno Beckere1582a82017-09-29 11:51:05 +0100940 /* On expected success, perform some public and private
941 * key operations to check if the key is working properly. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (res_complete == 0) {
943 if (is_priv) {
944 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == res_check);
945 } else {
946 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == res_check);
947 }
Hanno Becker04877a42017-10-11 10:01:33 +0100948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (res_check != 0) {
Hanno Becker04877a42017-10-11 10:01:33 +0100950 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 }
Hanno Beckere1582a82017-09-29 11:51:05 +0100952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 buf_orig = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
954 buf_enc = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
955 buf_dec = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
956 if (buf_orig == NULL || buf_enc == NULL || buf_dec == NULL) {
Hanno Beckere1582a82017-09-29 11:51:05 +0100957 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 }
Hanno Beckere1582a82017-09-29 11:51:05 +0100959
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200960 /* This test uses an insecure RNG, suitable only for testing.
961 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL,
963 buf_orig, mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100964
965 /* Make sure the number we're generating is smaller than the modulus */
966 buf_orig[0] = 0x00;
967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 TEST_ASSERT(mbedtls_rsa_public(&ctx, buf_orig, buf_enc) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (is_priv) {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200971 /* This test uses an insecure RNG, suitable only for testing.
972 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_std_rand,
974 NULL, buf_enc,
975 buf_dec) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 TEST_ASSERT(memcmp(buf_orig, buf_dec,
978 mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100979 }
980 }
981
Hanno Beckerc77ab892017-08-23 11:01:06 +0100982exit:
983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 mbedtls_free(buf_orig);
985 mbedtls_free(buf_enc);
986 mbedtls_free(buf_dec);
Hanno Beckere1582a82017-09-29 11:51:05 +0100987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 mbedtls_rsa_free(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 mbedtls_mpi_free(&N);
991 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
992 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100993}
994/* END_CASE */
995
Hanno Becker417f2d62017-08-23 11:44:51 +0100996/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997void mbedtls_rsa_export(char *input_N,
998 char *input_P,
999 char *input_Q,
1000 char *input_D,
1001 char *input_E,
1002 int is_priv,
1003 int successive)
Hanno Becker417f2d62017-08-23 11:44:51 +01001004{
1005 /* Original MPI's with which we set up the RSA context */
1006 mbedtls_mpi N, P, Q, D, E;
1007
1008 /* Exported MPI's */
1009 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 const int have_N = (strlen(input_N) > 0);
1012 const int have_P = (strlen(input_P) > 0);
1013 const int have_Q = (strlen(input_Q) > 0);
1014 const int have_D = (strlen(input_D) > 0);
1015 const int have_E = (strlen(input_E) > 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001016
Hanno Becker417f2d62017-08-23 11:44:51 +01001017 mbedtls_rsa_context ctx;
1018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 mbedtls_rsa_init(&ctx);
Hanno Becker417f2d62017-08-23 11:44:51 +01001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 mbedtls_mpi_init(&N);
1022 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
1023 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Becker417f2d62017-08-23 11:44:51 +01001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 mbedtls_mpi_init(&Ne);
1026 mbedtls_mpi_init(&Pe); mbedtls_mpi_init(&Qe);
1027 mbedtls_mpi_init(&De); mbedtls_mpi_init(&Ee);
Hanno Becker417f2d62017-08-23 11:44:51 +01001028
1029 /* Setup RSA context */
1030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (have_N) {
1032 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
1033 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if (have_P) {
1036 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
1037 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (have_Q) {
1040 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
1041 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if (have_D) {
1044 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
1045 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if (have_E) {
1048 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
1049 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 TEST_ASSERT(mbedtls_rsa_import(&ctx,
1052 strlen(input_N) ? &N : NULL,
1053 strlen(input_P) ? &P : NULL,
1054 strlen(input_Q) ? &Q : NULL,
1055 strlen(input_D) ? &D : NULL,
1056 strlen(input_E) ? &E : NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001059
1060 /*
1061 * Export parameters and compare to original ones.
1062 */
1063
1064 /* N and E must always be present. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (!successive) {
1066 TEST_ASSERT(mbedtls_rsa_export(&ctx, &Ne, NULL, NULL, NULL, &Ee) == 0);
1067 } else {
1068 TEST_ASSERT(mbedtls_rsa_export(&ctx, &Ne, NULL, NULL, NULL, NULL) == 0);
1069 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, NULL, NULL, &Ee) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001070 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&N, &Ne) == 0);
1072 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&E, &Ee) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001073
1074 /* If we were providing enough information to setup a complete private context,
1075 * we expect to be able to export all core parameters. */
1076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if (is_priv) {
1078 if (!successive) {
1079 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, &Pe, &Qe,
1080 &De, NULL) == 0);
1081 } else {
1082 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, &Pe, NULL,
1083 NULL, NULL) == 0);
1084 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, &Qe,
1085 NULL, NULL) == 0);
1086 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, NULL,
1087 &De, NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001088 }
1089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if (have_P) {
1091 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P, &Pe) == 0);
1092 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if (have_Q) {
1095 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Q, &Qe) == 0);
1096 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if (have_D) {
1099 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&D, &De) == 0);
1100 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001101
1102 /* While at it, perform a sanity check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 TEST_ASSERT(mbedtls_rsa_validate_params(&Ne, &Pe, &Qe, &De, &Ee,
1104 NULL, NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001105 }
1106
1107exit:
1108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 mbedtls_rsa_free(&ctx);
Hanno Becker417f2d62017-08-23 11:44:51 +01001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 mbedtls_mpi_free(&N);
1112 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
1113 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Becker417f2d62017-08-23 11:44:51 +01001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 mbedtls_mpi_free(&Ne);
1116 mbedtls_mpi_free(&Pe); mbedtls_mpi_free(&Qe);
1117 mbedtls_mpi_free(&De); mbedtls_mpi_free(&Ee);
Hanno Becker417f2d62017-08-23 11:44:51 +01001118}
1119/* END_CASE */
1120
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001121/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122void mbedtls_rsa_validate_params(char *input_N,
1123 char *input_P,
1124 char *input_Q,
1125 char *input_D,
1126 char *input_E,
1127 int prng, int result)
Hanno Beckerce002632017-08-23 13:22:36 +01001128{
1129 /* Original MPI's with which we set up the RSA context */
1130 mbedtls_mpi N, P, Q, D, E;
1131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 const int have_N = (strlen(input_N) > 0);
1133 const int have_P = (strlen(input_P) > 0);
1134 const int have_Q = (strlen(input_Q) > 0);
1135 const int have_D = (strlen(input_D) > 0);
1136 const int have_E = (strlen(input_E) > 0);
Hanno Beckerce002632017-08-23 13:22:36 +01001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 mbedtls_mpi_init(&N);
1139 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
1140 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerce002632017-08-23 13:22:36 +01001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (have_N) {
1143 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
1144 }
Hanno Beckerce002632017-08-23 13:22:36 +01001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (have_P) {
1147 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
1148 }
Hanno Beckerce002632017-08-23 13:22:36 +01001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if (have_Q) {
1151 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
1152 }
Hanno Beckerce002632017-08-23 13:22:36 +01001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (have_D) {
1155 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
1156 }
Hanno Beckerce002632017-08-23 13:22:36 +01001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (have_E) {
1159 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
1160 }
Hanno Beckerce002632017-08-23 13:22:36 +01001161
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001162 /* This test uses an insecure RNG, suitable only for testing.
1163 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 TEST_ASSERT(mbedtls_rsa_validate_params(have_N ? &N : NULL,
1165 have_P ? &P : NULL,
1166 have_Q ? &Q : NULL,
1167 have_D ? &D : NULL,
1168 have_E ? &E : NULL,
1169 prng ? mbedtls_test_rnd_std_rand : NULL,
1170 prng ? NULL : NULL) == result);
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001171
Hanno Beckerce002632017-08-23 13:22:36 +01001172exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 mbedtls_mpi_free(&N);
1174 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
1175 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerce002632017-08-23 13:22:36 +01001176}
1177/* END_CASE */
1178
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +02001179/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001180void mbedtls_rsa_export_raw(data_t *input_N, data_t *input_P,
1181 data_t *input_Q, data_t *input_D,
1182 data_t *input_E, int is_priv,
1183 int successive)
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001184{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001185 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001186 unsigned char bufNe[256];
1187 unsigned char bufPe[128];
1188 unsigned char bufQe[128];
1189 unsigned char bufDe[256];
1190 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001191
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001192 mbedtls_rsa_context ctx;
1193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 mbedtls_rsa_init(&ctx);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001195
1196 /* Setup RSA context */
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1198 input_N->len ? input_N->x : NULL, input_N->len,
1199 input_P->len ? input_P->x : NULL, input_P->len,
1200 input_Q->len ? input_Q->x : NULL, input_Q->len,
1201 input_D->len ? input_D->x : NULL, input_D->len,
1202 input_E->len ? input_E->x : NULL, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001205
1206 /*
1207 * Export parameters and compare to original ones.
1208 */
1209
1210 /* N and E must always be present. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if (!successive) {
1212 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, bufNe, input_N->len,
1213 NULL, 0, NULL, 0, NULL, 0,
1214 bufEe, input_E->len) == 0);
1215 } else {
1216 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, bufNe, input_N->len,
1217 NULL, 0, NULL, 0, NULL, 0,
1218 NULL, 0) == 0);
1219 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1220 NULL, 0, NULL, 0, NULL, 0,
1221 bufEe, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001222 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 TEST_ASSERT(memcmp(input_N->x, bufNe, input_N->len) == 0);
1224 TEST_ASSERT(memcmp(input_E->x, bufEe, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001225
1226 /* If we were providing enough information to setup a complete private context,
1227 * we expect to be able to export all core parameters. */
1228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if (is_priv) {
1230 if (!successive) {
1231 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1232 bufPe, input_P->len ? input_P->len : sizeof(bufPe),
1233 bufQe, input_Q->len ? input_Q->len : sizeof(bufQe),
1234 bufDe, input_D->len ? input_D->len : sizeof(bufDe),
1235 NULL, 0) == 0);
1236 } else {
1237 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1238 bufPe, input_P->len ? input_P->len : sizeof(bufPe),
1239 NULL, 0, NULL, 0,
1240 NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0, NULL, 0,
1243 bufQe, input_Q->len ? input_Q->len : sizeof(bufQe),
1244 NULL, 0, NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0, NULL, 0, NULL, 0,
1247 bufDe, input_D->len ? input_D->len : sizeof(bufDe),
1248 NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001249 }
1250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (input_P->len) {
1252 TEST_ASSERT(memcmp(input_P->x, bufPe, input_P->len) == 0);
1253 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 if (input_Q->len) {
1256 TEST_ASSERT(memcmp(input_Q->x, bufQe, input_Q->len) == 0);
1257 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 if (input_D->len) {
1260 TEST_ASSERT(memcmp(input_D->x, bufDe, input_D->len) == 0);
1261 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001262
1263 }
1264
1265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 mbedtls_rsa_free(&ctx);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001267}
1268/* END_CASE */
1269
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001270/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001271void mbedtls_rsa_import_raw(data_t *input_N,
1272 data_t *input_P, data_t *input_Q,
1273 data_t *input_D, data_t *input_E,
1274 int successive,
1275 int is_priv,
1276 int res_check,
1277 int res_complete)
Hanno Beckerc77ab892017-08-23 11:01:06 +01001278{
Hanno Beckere1582a82017-09-29 11:51:05 +01001279 /* Buffers used for encryption-decryption test */
1280 unsigned char *buf_orig = NULL;
1281 unsigned char *buf_enc = NULL;
1282 unsigned char *buf_dec = NULL;
1283
Hanno Beckerc77ab892017-08-23 11:01:06 +01001284 mbedtls_rsa_context ctx;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 mbedtls_rsa_init(&ctx);
Hanno Becker3f3ae852017-10-02 10:08:39 +01001287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (!successive) {
1289 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1290 (input_N->len > 0) ? input_N->x : NULL, input_N->len,
1291 (input_P->len > 0) ? input_P->x : NULL, input_P->len,
1292 (input_Q->len > 0) ? input_Q->x : NULL, input_Q->len,
1293 (input_D->len > 0) ? input_D->x : NULL, input_D->len,
1294 (input_E->len > 0) ? input_E->x : NULL,
1295 input_E->len) == 0);
1296 } else {
Hanno Beckerc77ab892017-08-23 11:01:06 +01001297 /* Import N, P, Q, D, E separately.
1298 * This should make no functional difference. */
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1301 (input_N->len > 0) ? input_N->x : NULL, input_N->len,
1302 NULL, 0, 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,
1306 (input_P->len > 0) ? input_P->x : NULL, input_P->len,
1307 NULL, 0, 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,
1311 (input_Q->len > 0) ? input_Q->x : NULL, input_Q->len,
1312 NULL, 0, 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,
1316 (input_D->len > 0) ? input_D->x : NULL, input_D->len,
1317 NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1320 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1321 (input_E->len > 0) ? input_E->x : NULL,
1322 input_E->len) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001323 }
1324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == res_complete);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001326
Hanno Beckere1582a82017-09-29 11:51:05 +01001327 /* On expected success, perform some public and private
1328 * key operations to check if the key is working properly. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 if (res_complete == 0) {
1330 if (is_priv) {
1331 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == res_check);
1332 } else {
1333 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == res_check);
1334 }
Hanno Becker04877a42017-10-11 10:01:33 +01001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (res_check != 0) {
Hanno Becker04877a42017-10-11 10:01:33 +01001337 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 }
Hanno Beckere1582a82017-09-29 11:51:05 +01001339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 buf_orig = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1341 buf_enc = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1342 buf_dec = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1343 if (buf_orig == NULL || buf_enc == NULL || buf_dec == NULL) {
Hanno Beckere1582a82017-09-29 11:51:05 +01001344 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 }
Hanno Beckere1582a82017-09-29 11:51:05 +01001346
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001347 /* This test uses an insecure RNG, suitable only for testing.
1348 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL,
1350 buf_orig, mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001351
1352 /* Make sure the number we're generating is smaller than the modulus */
1353 buf_orig[0] = 0x00;
1354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 TEST_ASSERT(mbedtls_rsa_public(&ctx, buf_orig, buf_enc) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 if (is_priv) {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001358 /* This test uses an insecure RNG, suitable only for testing.
1359 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_std_rand,
1361 NULL, buf_enc,
1362 buf_dec) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 TEST_ASSERT(memcmp(buf_orig, buf_dec,
1365 mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001366 }
1367 }
1368
Hanno Beckerc77ab892017-08-23 11:01:06 +01001369exit:
1370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 mbedtls_free(buf_orig);
1372 mbedtls_free(buf_enc);
1373 mbedtls_free(buf_dec);
Hanno Becker3f3ae852017-10-02 10:08:39 +01001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 mbedtls_rsa_free(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001376}
1377/* END_CASE */
1378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +01001380void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +00001381{
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 TEST_ASSERT(mbedtls_rsa_self_test(1) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +00001383}
Paul Bakker33b43f12013-08-20 11:48:36 +02001384/* END_CASE */