blob: 96fc59b9abce72bcbbb8808b41bc107ec95e06c4 [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
Gilles Peskine449bd832023-01-11 14:50:10 +010029 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
30 MBEDTLS_RSA_PKCS_V21,
31 invalid_hash_id),
32 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cronea7631b2021-06-03 18:51:59 +020033
Gilles Peskine449bd832023-01-11 14:50:10 +010034 TEST_EQUAL(mbedtls_rsa_pkcs1_sign(&ctx, NULL,
35 NULL, MBEDTLS_MD_NONE,
36 buf_len,
37 NULL, buf),
38 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010039
Gilles Peskine449bd832023-01-11 14:50:10 +010040 TEST_EQUAL(mbedtls_rsa_pkcs1_sign(&ctx, NULL,
41 NULL, MBEDTLS_MD_SHA256,
42 0,
43 NULL, buf),
44 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 TEST_EQUAL(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE,
47 buf_len,
48 NULL, buf),
49 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010050
Gilles Peskine449bd832023-01-11 14:50:10 +010051 TEST_EQUAL(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_SHA256,
52 0,
53 NULL, buf),
54 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010055
Ronald Cron3a0375f2021-06-08 10:22:28 +020056#if !defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +010057 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
58 MBEDTLS_RSA_PKCS_V15,
59 MBEDTLS_MD_NONE),
60 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cron3a0375f2021-06-08 10:22:28 +020061#endif
62
Tuvshinzaya Erdenekhuufe7524d2022-09-01 16:07:18 +010063#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine449bd832023-01-11 14:50:10 +010064 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
65 NULL, MBEDTLS_MD_NONE,
66 buf_len,
67 NULL, buf),
68 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
71 NULL, MBEDTLS_MD_SHA256,
72 0,
73 NULL, buf),
74 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_NONE,
77 buf_len,
78 NULL, buf),
79 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_SHA256,
82 0,
83 NULL, buf),
84 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +010085
86
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010087#endif
88
Ronald Cron3a0375f2021-06-08 10:22:28 +020089#if !defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +010090 TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
91 MBEDTLS_RSA_PKCS_V21,
92 MBEDTLS_MD_NONE),
93 MBEDTLS_ERR_RSA_INVALID_PADDING);
Ronald Cron3a0375f2021-06-08 10:22:28 +020094#endif
95
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +010096#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine449bd832023-01-11 14:50:10 +010097 TEST_EQUAL(mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
98 MBEDTLS_MD_NONE, buf_len,
99 NULL, buf_len,
100 buf),
101 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 TEST_EQUAL(mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
104 MBEDTLS_MD_SHA256, 0,
105 NULL, buf_len,
106 buf),
107 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu08b22342022-09-01 16:18:00 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_NONE,
110 buf_len, NULL,
111 MBEDTLS_MD_NONE,
112 buf_len, 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_verify_ext(&ctx, MBEDTLS_MD_SHA256,
116 0, NULL,
117 MBEDTLS_MD_NONE,
118 buf_len, 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(&ctx, MBEDTLS_MD_NONE,
122 buf_len,
123 NULL, buf),
124 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_SHA256,
127 0,
128 NULL, buf),
129 MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
Tuvshinzaya Erdenekhuu7e2e2a92022-07-26 10:09:24 +0100130#endif
131
Ronald Cronea7631b2021-06-03 18:51:59 +0200132exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 mbedtls_rsa_free(&ctx);
Ronald Cronea7631b2021-06-03 18:51:59 +0200134}
135/* END_CASE */
136
137/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100138void rsa_init_free(int reinit)
Gilles Peskine914afe12021-02-01 17:55:24 +0100139{
140 mbedtls_rsa_context ctx;
141
142 /* Double free is not explicitly documented to work, but we rely on it
143 * even inside the library so that you can call mbedtls_rsa_free()
144 * unconditionally on an error path without checking whether it has
145 * already been called in the success path. */
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_rsa_init(&ctx);
148 mbedtls_rsa_free(&ctx);
Gilles Peskine914afe12021-02-01 17:55:24 +0100149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (reinit) {
151 mbedtls_rsa_init(&ctx);
152 }
153 mbedtls_rsa_free(&ctx);
Gilles Peskine914afe12021-02-01 17:55:24 +0100154
155 /* This test case always succeeds, functionally speaking. A plausible
156 * bug might trigger an invalid pointer dereference or a memory leak. */
157 goto exit;
158}
159/* END_CASE */
160
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200161/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162void mbedtls_rsa_pkcs1_sign(data_t *message_str, int padding_mode,
163 int digest, int mod, char *input_P,
164 char *input_Q, char *input_N, char *input_E,
165 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000166{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200167 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100169 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200170 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
173 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
174 mbedtls_rsa_init(&ctx);
175 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
176 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 memset(output, 0x00, sizeof(output));
179 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
182 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
183 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
184 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
187 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
188 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
189 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 TEST_ASSERT(mbedtls_rsa_pkcs1_sign(
192 &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
193 digest, message_str->len, message_str->x,
194 output) == result);
195 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
198 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000199 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000200
Paul Bakkerbd51b262014-07-10 15:26:12 +0200201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
203 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
204 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000205}
Paul Bakker33b43f12013-08-20 11:48:36 +0200206/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000207
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200208/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209void mbedtls_rsa_pkcs1_verify(data_t *message_str, int padding_mode,
210 int digest, int mod,
211 char *input_N, char *input_E,
212 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000213{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100215 mbedtls_mpi N, E;
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
218 mbedtls_rsa_init(&ctx);
219 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
220 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
223 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
224 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
225 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
226 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 TEST_ASSERT(mbedtls_rsa_pkcs1_verify(&ctx, digest, message_str->len, message_str->x,
229 result_str->x) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100230
Paul Bakkerbd51b262014-07-10 15:26:12 +0200231exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
233 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000234}
Paul Bakker33b43f12013-08-20 11:48:36 +0200235/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000236
Paul Bakker821fb082009-07-12 13:26:42 +0000237
Paul Bakker33b43f12013-08-20 11:48:36 +0200238/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239void rsa_pkcs1_sign_raw(data_t *hash_result,
240 int padding_mode, int mod,
241 char *input_P, char *input_Q,
242 char *input_N, char *input_E,
243 data_t *result_str)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000244{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200245 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100247 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200248 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 mbedtls_rsa_init(&ctx);
251 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
252 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
Paul Bakker821fb082009-07-12 13:26:42 +0000253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
255 MBEDTLS_MD_NONE) == 0);
Paul Elliotte57dd2d2021-06-25 11:13:24 +0100256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 memset(output, 0x00, sizeof(output));
258 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
261 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
262 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
263 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
266 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
267 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
268 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000269
Paul Bakker821fb082009-07-12 13:26:42 +0000270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 TEST_ASSERT(mbedtls_rsa_pkcs1_sign(&ctx, &mbedtls_test_rnd_pseudo_rand,
272 &rnd_info, MBEDTLS_MD_NONE,
273 hash_result->len,
274 hash_result->x, output) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000275
Paul Bakker821fb082009-07-12 13:26:42 +0000276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
278 ctx.len, result_str->len) == 0);
Paul Bakker6c591fa2011-05-05 11:49:20 +0000279
Paul Bakkerbd51b262014-07-10 15:26:12 +0200280exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
282 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000285}
Paul Bakker33b43f12013-08-20 11:48:36 +0200286/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000287
Paul Bakker33b43f12013-08-20 11:48:36 +0200288/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289void rsa_pkcs1_verify_raw(data_t *hash_result,
290 int padding_mode, int mod,
291 char *input_N, char *input_E,
292 data_t *result_str, int correct)
Paul Bakker821fb082009-07-12 13:26:42 +0000293{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200294 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000296
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100297 mbedtls_mpi N, E;
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 mbedtls_rsa_init(&ctx);
301 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
302 MBEDTLS_MD_NONE) == 0);
303 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
306 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
309 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
310 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000311
Paul Bakker821fb082009-07-12 13:26:42 +0000312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 TEST_ASSERT(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE, hash_result->len, hash_result->x,
314 result_str->x) == correct);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100315
Paul Bakkerbd51b262014-07-10 15:26:12 +0200316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
318 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000319}
Paul Bakker33b43f12013-08-20 11:48:36 +0200320/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000321
Paul Bakker33b43f12013-08-20 11:48:36 +0200322/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323void mbedtls_rsa_pkcs1_encrypt(data_t *message_str, int padding_mode,
324 int mod, char *input_N, char *input_E,
325 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000326{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200327 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200329 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000330
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100331 mbedtls_mpi N, E;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker821fb082009-07-12 13:26:42 +0000335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 mbedtls_rsa_init(&ctx);
337 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
338 MBEDTLS_MD_NONE) == 0);
339 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
342 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
345 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
346 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000347
Paul Bakker42a29bf2009-07-07 20:18:41 +0000348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 TEST_ASSERT(mbedtls_rsa_pkcs1_encrypt(&ctx,
350 &mbedtls_test_rnd_pseudo_rand,
351 &rnd_info, message_str->len,
352 message_str->x,
353 output) == result);
354 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
357 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000358 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100359
Paul Bakkerbd51b262014-07-10 15:26:12 +0200360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
362 mbedtls_rsa_free(&ctx);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000363}
Paul Bakker33b43f12013-08-20 11:48:36 +0200364/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000365
Paul Bakker33b43f12013-08-20 11:48:36 +0200366/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367void rsa_pkcs1_encrypt_bad_rng(data_t *message_str, int padding_mode,
368 int mod, char *input_N, char *input_E,
369 data_t *result_str, int result)
Paul Bakkera6656852010-07-18 19:47:14 +0000370{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200371 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000373
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100374 mbedtls_mpi N, E;
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
377 mbedtls_rsa_init(&ctx);
378 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
379 MBEDTLS_MD_NONE) == 0);
380 memset(output, 0x00, sizeof(output));
Paul Bakkera6656852010-07-18 19:47:14 +0000381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
383 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
386 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
387 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000388
Paul Bakkera6656852010-07-18 19:47:14 +0000389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 TEST_ASSERT(mbedtls_rsa_pkcs1_encrypt(&ctx, &mbedtls_test_rnd_zero_rand,
391 NULL, message_str->len,
392 message_str->x,
393 output) == result);
394 if (result == 0) {
Paul Bakkera6656852010-07-18 19:47:14 +0000395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
397 ctx.len, result_str->len) == 0);
Paul Bakkera6656852010-07-18 19:47:14 +0000398 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100399
Paul Bakkerbd51b262014-07-10 15:26:12 +0200400exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
402 mbedtls_rsa_free(&ctx);
Paul Bakkera6656852010-07-18 19:47:14 +0000403}
Paul Bakker33b43f12013-08-20 11:48:36 +0200404/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000405
Paul Bakker33b43f12013-08-20 11:48:36 +0200406/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407void mbedtls_rsa_pkcs1_decrypt(data_t *message_str, int padding_mode,
408 int mod, char *input_P,
409 char *input_Q, char *input_N,
410 char *input_E, int max_output,
411 data_t *result_str, int result)
Paul Bakker42a29bf2009-07-07 20:18:41 +0000412{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200413 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000415 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200416 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100417 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
420 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 mbedtls_rsa_init(&ctx);
423 TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
424 MBEDTLS_MD_NONE) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 memset(output, 0x00, sizeof(output));
427 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker42a29bf2009-07-07 20:18:41 +0000428
Paul Bakker42a29bf2009-07-07 20:18:41 +0000429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
431 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
432 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
433 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
436 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
437 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
438 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000439
Paul Bakker69998dd2009-07-11 19:15:20 +0000440 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 TEST_ASSERT(mbedtls_rsa_pkcs1_decrypt(&ctx, mbedtls_test_rnd_pseudo_rand,
443 &rnd_info,
444 &output_len, message_str->x, output,
445 max_output) == result);
446 if (result == 0) {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
449 output_len,
450 result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000451 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000452
Paul Bakkerbd51b262014-07-10 15:26:12 +0200453exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
455 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
456 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000457}
Paul Bakker33b43f12013-08-20 11:48:36 +0200458/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000459
Paul Bakker33b43f12013-08-20 11:48:36 +0200460/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461void mbedtls_rsa_public(data_t *message_str, int mod,
462 char *input_N, char *input_E,
463 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000464{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200465 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000467
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100468 mbedtls_mpi N, E;
469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
471 mbedtls_rsa_init(&ctx);
472 mbedtls_rsa_init(&ctx2);
473 memset(output, 0x00, sizeof(output));
Paul Bakker821fb082009-07-12 13:26:42 +0000474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
476 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
Gilles Peskine058d0092021-06-09 16:24:35 +0200479
480 /* Check test data consistency */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 TEST_ASSERT(message_str->len == (size_t) (mod / 8));
482 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
483 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 TEST_ASSERT(mbedtls_rsa_public(&ctx, message_str->x, output) == result);
486 if (result == 0) {
Paul Bakker821fb082009-07-12 13:26:42 +0000487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
489 ctx.len, result_str->len) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000490 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100491
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100492 /* And now with the copy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 TEST_ASSERT(mbedtls_rsa_copy(&ctx2, &ctx) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200494 /* clear the original to be sure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 mbedtls_rsa_free(&ctx);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx2) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 memset(output, 0x00, sizeof(output));
500 TEST_ASSERT(mbedtls_rsa_public(&ctx2, message_str->x, output) == result);
501 if (result == 0) {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
504 ctx.len, result_str->len) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100505 }
506
Paul Bakkerbd51b262014-07-10 15:26:12 +0200507exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
509 mbedtls_rsa_free(&ctx);
510 mbedtls_rsa_free(&ctx2);
Paul Bakker821fb082009-07-12 13:26:42 +0000511}
Paul Bakker33b43f12013-08-20 11:48:36 +0200512/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000513
Paul Bakker33b43f12013-08-20 11:48:36 +0200514/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100515void mbedtls_rsa_private(data_t *message_str, int mod,
516 char *input_P, char *input_Q,
517 char *input_N, char *input_E,
518 data_t *result_str, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000519{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200520 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100522 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200523 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200524 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
527 mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
528 mbedtls_rsa_init(&ctx);
529 mbedtls_rsa_init(&ctx2);
Paul Bakker821fb082009-07-12 13:26:42 +0000530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
Paul Bakker821fb082009-07-12 13:26:42 +0000532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
534 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
535 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
536 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
Gilles Peskine058d0092021-06-09 16:24:35 +0200539
540 /* Check test data consistency */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 TEST_ASSERT(message_str->len == (size_t) (mod / 8));
542 TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
543 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
544 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000545
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200546 /* repeat three times to test updating of blinding values */
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 for (i = 0; i < 3; i++) {
548 memset(output, 0x00, sizeof(output));
549 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_pseudo_rand,
550 &rnd_info, message_str->x,
551 output) == result);
552 if (result == 0) {
Paul Bakker821fb082009-07-12 13:26:42 +0000553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
555 ctx.len,
556 result_str->len) == 0);
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200557 }
Paul Bakker821fb082009-07-12 13:26:42 +0000558 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000559
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100560 /* And now one more time with the copy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 TEST_ASSERT(mbedtls_rsa_copy(&ctx2, &ctx) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200562 /* clear the original to be sure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 mbedtls_rsa_free(&ctx);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx2) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 memset(output, 0x00, sizeof(output));
568 TEST_ASSERT(mbedtls_rsa_private(&ctx2, mbedtls_test_rnd_pseudo_rand,
569 &rnd_info, message_str->x,
570 output) == result);
571 if (result == 0) {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
574 ctx2.len,
575 result_str->len) == 0);
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100576 }
577
Paul Bakkerbd51b262014-07-10 15:26:12 +0200578exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
580 mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 mbedtls_rsa_free(&ctx); mbedtls_rsa_free(&ctx2);
Paul Bakker42a29bf2009-07-07 20:18:41 +0000583}
Paul Bakker33b43f12013-08-20 11:48:36 +0200584/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000585
Paul Bakker33b43f12013-08-20 11:48:36 +0200586/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100587void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000588{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_rsa_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 memset(&ctx, 0x00, sizeof(mbedtls_rsa_context));
Paul Bakker37940d9f2009-07-10 22:38:58 +0000591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
Paul Bakker37940d9f2009-07-10 22:38:58 +0000593}
Paul Bakker33b43f12013-08-20 11:48:36 +0200594/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000595
Paul Bakker33b43f12013-08-20 11:48:36 +0200596/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100597void mbedtls_rsa_check_pubkey(char *input_N, char *input_E, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100600 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
603 mbedtls_rsa_init(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if (strlen(input_N)) {
606 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000607 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (strlen(input_E)) {
609 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000610 }
611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
613 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100614
Paul Bakkerbd51b262014-07-10 15:26:12 +0200615exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
617 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000618}
Paul Bakker33b43f12013-08-20 11:48:36 +0200619/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000620
Paul Bakker33b43f12013-08-20 11:48:36 +0200621/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100622void mbedtls_rsa_check_privkey(int mod, char *input_P, char *input_Q,
623 char *input_N, char *input_E, char *input_D,
624 char *input_DP, char *input_DQ, char *input_QP,
625 int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 mbedtls_rsa_init(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000630
Paul Bakker33b43f12013-08-20 11:48:36 +0200631 ctx.len = mod / 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (strlen(input_P)) {
633 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.P, input_P) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000634 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (strlen(input_Q)) {
636 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.Q, input_Q) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000637 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (strlen(input_N)) {
639 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.N, input_N) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000640 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (strlen(input_E)) {
642 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.E, input_E) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000643 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (strlen(input_D)) {
645 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.D, input_D) == 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000646 }
Hanno Becker131134f2017-08-23 08:31:07 +0100647#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (strlen(input_DP)) {
649 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.DP, input_DP) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000650 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (strlen(input_DQ)) {
652 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.DQ, input_DQ) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000653 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (strlen(input_QP)) {
655 TEST_ASSERT(mbedtls_test_read_mpi(&ctx.QP, input_QP) == 0);
Paul Bakker31417a72012-09-27 20:41:37 +0000656 }
Hanno Becker131134f2017-08-23 08:31:07 +0100657#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100658 ((void) input_DP);
659 ((void) input_DQ);
660 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100661#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == result);
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100664
Paul Bakkerbd51b262014-07-10 15:26:12 +0200665exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000667}
Paul Bakker33b43f12013-08-20 11:48:36 +0200668/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000669
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100670/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671void rsa_check_pubpriv(int mod, char *input_Npub, char *input_Epub,
672 char *input_P, char *input_Q, char *input_N,
673 char *input_E, char *input_D, char *input_DP,
674 char *input_DQ, char *input_QP, int result)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 mbedtls_rsa_init(&pub);
679 mbedtls_rsa_init(&prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100680
681 pub.len = mod / 8;
682 prv.len = mod / 8;
683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if (strlen(input_Npub)) {
685 TEST_ASSERT(mbedtls_test_read_mpi(&pub.N, input_Npub) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100686 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (strlen(input_Epub)) {
688 TEST_ASSERT(mbedtls_test_read_mpi(&pub.E, input_Epub) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100689 }
690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (strlen(input_P)) {
692 TEST_ASSERT(mbedtls_test_read_mpi(&prv.P, input_P) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100693 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (strlen(input_Q)) {
695 TEST_ASSERT(mbedtls_test_read_mpi(&prv.Q, input_Q) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (strlen(input_N)) {
698 TEST_ASSERT(mbedtls_test_read_mpi(&prv.N, input_N) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100699 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (strlen(input_E)) {
701 TEST_ASSERT(mbedtls_test_read_mpi(&prv.E, input_E) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100702 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (strlen(input_D)) {
704 TEST_ASSERT(mbedtls_test_read_mpi(&prv.D, input_D) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100705 }
Hanno Becker131134f2017-08-23 08:31:07 +0100706#if !defined(MBEDTLS_RSA_NO_CRT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if (strlen(input_DP)) {
708 TEST_ASSERT(mbedtls_test_read_mpi(&prv.DP, input_DP) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100709 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if (strlen(input_DQ)) {
711 TEST_ASSERT(mbedtls_test_read_mpi(&prv.DQ, input_DQ) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100712 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (strlen(input_QP)) {
714 TEST_ASSERT(mbedtls_test_read_mpi(&prv.QP, input_QP) == 0);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100715 }
Hanno Becker131134f2017-08-23 08:31:07 +0100716#else
Werner Lewisf65a3272022-07-07 11:38:44 +0100717 ((void) input_DP);
718 ((void) input_DQ);
719 ((void) input_QP);
Hanno Becker131134f2017-08-23 08:31:07 +0100720#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 TEST_ASSERT(mbedtls_rsa_check_pub_priv(&pub, &prv) == result);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100723
724exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 mbedtls_rsa_free(&pub);
726 mbedtls_rsa_free(&prv);
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100727}
728/* END_CASE */
729
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200730/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731void mbedtls_rsa_gen_key(int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000732{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_rsa_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 mbedtls_rsa_init(&ctx);
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000735
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200736 /* This test uses an insecure RNG, suitable only for testing.
737 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 TEST_ASSERT(mbedtls_rsa_gen_key(&ctx, mbedtls_test_rnd_std_rand, NULL, nrbits,
739 exponent) == result);
740 if (result == 0) {
741 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
742 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&ctx.P, &ctx.Q) > 0);
Paul Bakker821fb082009-07-12 13:26:42 +0000743 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100744
Paul Bakkerbd51b262014-07-10 15:26:12 +0200745exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 mbedtls_rsa_free(&ctx);
Paul Bakker821fb082009-07-12 13:26:42 +0000747}
Paul Bakker33b43f12013-08-20 11:48:36 +0200748/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000749
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +0200750/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751void mbedtls_rsa_deduce_primes(char *input_N,
752 char *input_D,
753 char *input_E,
754 char *output_P,
755 char *output_Q,
756 int corrupt, int result)
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100757{
758 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 mbedtls_mpi_init(&N);
761 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
762 mbedtls_mpi_init(&Pp); mbedtls_mpi_init(&Qp);
763 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
766 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
767 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
768 TEST_ASSERT(mbedtls_test_read_mpi(&Qp, output_P) == 0);
769 TEST_ASSERT(mbedtls_test_read_mpi(&Pp, output_Q) == 0);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (corrupt) {
772 TEST_ASSERT(mbedtls_mpi_add_int(&D, &D, 2) == 0);
773 }
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100774
775 /* Try to deduce P, Q from N, D, E only. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 TEST_ASSERT(mbedtls_rsa_deduce_primes(&N, &D, &E, &P, &Q) == result);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (!corrupt) {
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100779 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 TEST_ASSERT((mbedtls_mpi_cmp_mpi(&P, &Pp) == 0 && mbedtls_mpi_cmp_mpi(&Q, &Qp) == 0) ||
781 (mbedtls_mpi_cmp_mpi(&P, &Qp) == 0 && mbedtls_mpi_cmp_mpi(&Q, &Pp) == 0));
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100782 }
783
784exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 mbedtls_mpi_free(&N);
786 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
787 mbedtls_mpi_free(&Pp); mbedtls_mpi_free(&Qp);
788 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100789}
790/* END_CASE */
791
Hanno Becker6b4ce492017-08-23 11:00:21 +0100792/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793void mbedtls_rsa_deduce_private_exponent(char *input_P,
794 char *input_Q,
795 char *input_E,
796 char *output_D,
797 int corrupt, int result)
Hanno Becker6b4ce492017-08-23 11:00:21 +0100798{
799 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
802 mbedtls_mpi_init(&D); mbedtls_mpi_init(&Dp);
803 mbedtls_mpi_init(&E);
804 mbedtls_mpi_init(&R); mbedtls_mpi_init(&Rp);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
807 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
808 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
809 TEST_ASSERT(mbedtls_test_read_mpi(&Dp, output_D) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if (corrupt) {
Hanno Becker6b4ce492017-08-23 11:00:21 +0100812 /* Make E even */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 TEST_ASSERT(mbedtls_mpi_set_bit(&E, 0, 0) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100814 }
815
816 /* Try to deduce D from N, P, Q, E. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 TEST_ASSERT(mbedtls_rsa_deduce_private_exponent(&P, &Q,
818 &E, &D) == result);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (!corrupt) {
Hanno Becker6b4ce492017-08-23 11:00:21 +0100821 /*
822 * Check that D and Dp agree modulo LCM(P-1, Q-1).
823 */
824
825 /* Replace P,Q by P-1, Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 TEST_ASSERT(mbedtls_mpi_sub_int(&P, &P, 1) == 0);
827 TEST_ASSERT(mbedtls_mpi_sub_int(&Q, &Q, 1) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100828
829 /* Check D == Dp modulo P-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &D, &P) == 0);
831 TEST_ASSERT(mbedtls_mpi_mod_mpi(&Rp, &Dp, &P) == 0);
832 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &Rp) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100833
834 /* Check D == Dp modulo Q-1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &D, &Q) == 0);
836 TEST_ASSERT(mbedtls_mpi_mod_mpi(&Rp, &Dp, &Q) == 0);
837 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &Rp) == 0);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100838 }
839
840exit:
841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
843 mbedtls_mpi_free(&D); mbedtls_mpi_free(&Dp);
844 mbedtls_mpi_free(&E);
845 mbedtls_mpi_free(&R); mbedtls_mpi_free(&Rp);
Hanno Becker6b4ce492017-08-23 11:00:21 +0100846}
847/* END_CASE */
848
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200849/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850void mbedtls_rsa_import(char *input_N,
851 char *input_P,
852 char *input_Q,
853 char *input_D,
854 char *input_E,
855 int successive,
856 int is_priv,
857 int res_check,
858 int res_complete)
Hanno Beckerc77ab892017-08-23 11:01:06 +0100859{
860 mbedtls_mpi N, P, Q, D, E;
861 mbedtls_rsa_context ctx;
862
Hanno Beckere1582a82017-09-29 11:51:05 +0100863 /* Buffers used for encryption-decryption test */
864 unsigned char *buf_orig = NULL;
865 unsigned char *buf_enc = NULL;
866 unsigned char *buf_dec = NULL;
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 const int have_N = (strlen(input_N) > 0);
869 const int have_P = (strlen(input_P) > 0);
870 const int have_Q = (strlen(input_Q) > 0);
871 const int have_D = (strlen(input_D) > 0);
872 const int have_E = (strlen(input_E) > 0);
Hanno Becker4d6e8342017-09-29 11:50:18 +0100873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 mbedtls_rsa_init(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 mbedtls_mpi_init(&N);
877 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
878 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 if (have_N) {
881 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100882 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100883
884 if (have_P) {
885 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
886 }
887
888 if (have_Q) {
889 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
890 }
891
892 if (have_D) {
893 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
894 }
895
896 if (have_E) {
897 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
898 }
899
900 if (!successive) {
901 TEST_ASSERT(mbedtls_rsa_import(&ctx,
902 have_N ? &N : NULL,
903 have_P ? &P : NULL,
904 have_Q ? &Q : NULL,
905 have_D ? &D : NULL,
906 have_E ? &E : NULL) == 0);
907 } else {
Hanno Beckerc77ab892017-08-23 11:01:06 +0100908 /* Import N, P, Q, D, E separately.
909 * This should make no functional difference. */
910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 TEST_ASSERT(mbedtls_rsa_import(&ctx,
912 have_N ? &N : NULL,
913 NULL, NULL, NULL, NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 TEST_ASSERT(mbedtls_rsa_import(&ctx,
916 NULL,
917 have_P ? &P : NULL,
918 NULL, NULL, NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 TEST_ASSERT(mbedtls_rsa_import(&ctx,
921 NULL, NULL,
922 have_Q ? &Q : NULL,
923 NULL, NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 TEST_ASSERT(mbedtls_rsa_import(&ctx,
926 NULL, NULL, NULL,
927 have_D ? &D : NULL,
928 NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 TEST_ASSERT(mbedtls_rsa_import(&ctx,
931 NULL, NULL, NULL, NULL,
932 have_E ? &E : NULL) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100933 }
934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == res_complete);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100936
Hanno Beckere1582a82017-09-29 11:51:05 +0100937 /* On expected success, perform some public and private
938 * key operations to check if the key is working properly. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (res_complete == 0) {
940 if (is_priv) {
941 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == res_check);
942 } else {
943 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == res_check);
944 }
Hanno Becker04877a42017-10-11 10:01:33 +0100945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (res_check != 0) {
Hanno Becker04877a42017-10-11 10:01:33 +0100947 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 }
Hanno Beckere1582a82017-09-29 11:51:05 +0100949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 buf_orig = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
951 buf_enc = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
952 buf_dec = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
953 if (buf_orig == NULL || buf_enc == NULL || buf_dec == NULL) {
Hanno Beckere1582a82017-09-29 11:51:05 +0100954 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 }
Hanno Beckere1582a82017-09-29 11:51:05 +0100956
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200957 /* This test uses an insecure RNG, suitable only for testing.
958 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL,
960 buf_orig, mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100961
962 /* Make sure the number we're generating is smaller than the modulus */
963 buf_orig[0] = 0x00;
964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 TEST_ASSERT(mbedtls_rsa_public(&ctx, buf_orig, buf_enc) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (is_priv) {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +0200968 /* This test uses an insecure RNG, suitable only for testing.
969 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_std_rand,
971 NULL, buf_enc,
972 buf_dec) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 TEST_ASSERT(memcmp(buf_orig, buf_dec,
975 mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +0100976 }
977 }
978
Hanno Beckerc77ab892017-08-23 11:01:06 +0100979exit:
980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 mbedtls_free(buf_orig);
982 mbedtls_free(buf_enc);
983 mbedtls_free(buf_dec);
Hanno Beckere1582a82017-09-29 11:51:05 +0100984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 mbedtls_rsa_free(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 mbedtls_mpi_free(&N);
988 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
989 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerc77ab892017-08-23 11:01:06 +0100990}
991/* END_CASE */
992
Hanno Becker417f2d62017-08-23 11:44:51 +0100993/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100994void mbedtls_rsa_export(char *input_N,
995 char *input_P,
996 char *input_Q,
997 char *input_D,
998 char *input_E,
999 int is_priv,
1000 int successive)
Hanno Becker417f2d62017-08-23 11:44:51 +01001001{
1002 /* Original MPI's with which we set up the RSA context */
1003 mbedtls_mpi N, P, Q, D, E;
1004
1005 /* Exported MPI's */
1006 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 const int have_N = (strlen(input_N) > 0);
1009 const int have_P = (strlen(input_P) > 0);
1010 const int have_Q = (strlen(input_Q) > 0);
1011 const int have_D = (strlen(input_D) > 0);
1012 const int have_E = (strlen(input_E) > 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001013
Hanno Becker417f2d62017-08-23 11:44:51 +01001014 mbedtls_rsa_context ctx;
1015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 mbedtls_rsa_init(&ctx);
Hanno Becker417f2d62017-08-23 11:44:51 +01001017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 mbedtls_mpi_init(&N);
1019 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
1020 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Becker417f2d62017-08-23 11:44:51 +01001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 mbedtls_mpi_init(&Ne);
1023 mbedtls_mpi_init(&Pe); mbedtls_mpi_init(&Qe);
1024 mbedtls_mpi_init(&De); mbedtls_mpi_init(&Ee);
Hanno Becker417f2d62017-08-23 11:44:51 +01001025
1026 /* Setup RSA context */
1027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (have_N) {
1029 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
1030 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if (have_P) {
1033 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
1034 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if (have_Q) {
1037 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
1038 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if (have_D) {
1041 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
1042 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if (have_E) {
1045 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
1046 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 TEST_ASSERT(mbedtls_rsa_import(&ctx,
1049 strlen(input_N) ? &N : NULL,
1050 strlen(input_P) ? &P : NULL,
1051 strlen(input_Q) ? &Q : NULL,
1052 strlen(input_D) ? &D : NULL,
1053 strlen(input_E) ? &E : NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001056
1057 /*
1058 * Export parameters and compare to original ones.
1059 */
1060
1061 /* N and E must always be present. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (!successive) {
1063 TEST_ASSERT(mbedtls_rsa_export(&ctx, &Ne, NULL, NULL, NULL, &Ee) == 0);
1064 } else {
1065 TEST_ASSERT(mbedtls_rsa_export(&ctx, &Ne, NULL, NULL, NULL, NULL) == 0);
1066 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, NULL, NULL, &Ee) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001067 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&N, &Ne) == 0);
1069 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&E, &Ee) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001070
1071 /* If we were providing enough information to setup a complete private context,
1072 * we expect to be able to export all core parameters. */
1073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if (is_priv) {
1075 if (!successive) {
1076 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, &Pe, &Qe,
1077 &De, NULL) == 0);
1078 } else {
1079 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, &Pe, NULL,
1080 NULL, NULL) == 0);
1081 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, &Qe,
1082 NULL, NULL) == 0);
1083 TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, NULL,
1084 &De, NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001085 }
1086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (have_P) {
1088 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P, &Pe) == 0);
1089 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (have_Q) {
1092 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Q, &Qe) == 0);
1093 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (have_D) {
1096 TEST_ASSERT(mbedtls_mpi_cmp_mpi(&D, &De) == 0);
1097 }
Hanno Becker417f2d62017-08-23 11:44:51 +01001098
1099 /* While at it, perform a sanity check */
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 TEST_ASSERT(mbedtls_rsa_validate_params(&Ne, &Pe, &Qe, &De, &Ee,
1101 NULL, NULL) == 0);
Hanno Becker417f2d62017-08-23 11:44:51 +01001102 }
1103
1104exit:
1105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 mbedtls_rsa_free(&ctx);
Hanno Becker417f2d62017-08-23 11:44:51 +01001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 mbedtls_mpi_free(&N);
1109 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
1110 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Becker417f2d62017-08-23 11:44:51 +01001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 mbedtls_mpi_free(&Ne);
1113 mbedtls_mpi_free(&Pe); mbedtls_mpi_free(&Qe);
1114 mbedtls_mpi_free(&De); mbedtls_mpi_free(&Ee);
Hanno Becker417f2d62017-08-23 11:44:51 +01001115}
1116/* END_CASE */
1117
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001118/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001119void mbedtls_rsa_validate_params(char *input_N,
1120 char *input_P,
1121 char *input_Q,
1122 char *input_D,
1123 char *input_E,
1124 int prng, int result)
Hanno Beckerce002632017-08-23 13:22:36 +01001125{
1126 /* Original MPI's with which we set up the RSA context */
1127 mbedtls_mpi N, P, Q, D, E;
1128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 const int have_N = (strlen(input_N) > 0);
1130 const int have_P = (strlen(input_P) > 0);
1131 const int have_Q = (strlen(input_Q) > 0);
1132 const int have_D = (strlen(input_D) > 0);
1133 const int have_E = (strlen(input_E) > 0);
Hanno Beckerce002632017-08-23 13:22:36 +01001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 mbedtls_mpi_init(&N);
1136 mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
1137 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerce002632017-08-23 13:22:36 +01001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 if (have_N) {
1140 TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
1141 }
Hanno Beckerce002632017-08-23 13:22:36 +01001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if (have_P) {
1144 TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
1145 }
Hanno Beckerce002632017-08-23 13:22:36 +01001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if (have_Q) {
1148 TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
1149 }
Hanno Beckerce002632017-08-23 13:22:36 +01001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if (have_D) {
1152 TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
1153 }
Hanno Beckerce002632017-08-23 13:22:36 +01001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if (have_E) {
1156 TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
1157 }
Hanno Beckerce002632017-08-23 13:22:36 +01001158
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001159 /* This test uses an insecure RNG, suitable only for testing.
1160 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 TEST_ASSERT(mbedtls_rsa_validate_params(have_N ? &N : NULL,
1162 have_P ? &P : NULL,
1163 have_Q ? &Q : NULL,
1164 have_D ? &D : NULL,
1165 have_E ? &E : NULL,
1166 prng ? mbedtls_test_rnd_std_rand : NULL,
1167 prng ? NULL : NULL) == result);
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001168
Hanno Beckerce002632017-08-23 13:22:36 +01001169exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 mbedtls_mpi_free(&N);
1171 mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
1172 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerce002632017-08-23 13:22:36 +01001173}
1174/* END_CASE */
1175
Manuel Pégourié-Gonnard1d1174a2022-07-16 08:41:34 +02001176/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001177void mbedtls_rsa_export_raw(data_t *input_N, data_t *input_P,
1178 data_t *input_Q, data_t *input_D,
1179 data_t *input_E, int is_priv,
1180 int successive)
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001181{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001182 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001183 unsigned char bufNe[256];
1184 unsigned char bufPe[128];
1185 unsigned char bufQe[128];
1186 unsigned char bufDe[256];
1187 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001188
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001189 mbedtls_rsa_context ctx;
1190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 mbedtls_rsa_init(&ctx);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001192
1193 /* Setup RSA context */
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1195 input_N->len ? input_N->x : NULL, input_N->len,
1196 input_P->len ? input_P->x : NULL, input_P->len,
1197 input_Q->len ? input_Q->x : NULL, input_Q->len,
1198 input_D->len ? input_D->x : NULL, input_D->len,
1199 input_E->len ? input_E->x : NULL, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001202
1203 /*
1204 * Export parameters and compare to original ones.
1205 */
1206
1207 /* N and E must always be present. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if (!successive) {
1209 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, bufNe, input_N->len,
1210 NULL, 0, NULL, 0, NULL, 0,
1211 bufEe, input_E->len) == 0);
1212 } else {
1213 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, bufNe, input_N->len,
1214 NULL, 0, NULL, 0, NULL, 0,
1215 NULL, 0) == 0);
1216 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1217 NULL, 0, NULL, 0, NULL, 0,
1218 bufEe, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001219 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 TEST_ASSERT(memcmp(input_N->x, bufNe, input_N->len) == 0);
1221 TEST_ASSERT(memcmp(input_E->x, bufEe, input_E->len) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001222
1223 /* If we were providing enough information to setup a complete private context,
1224 * we expect to be able to export all core parameters. */
1225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 if (is_priv) {
1227 if (!successive) {
1228 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1229 bufPe, input_P->len ? input_P->len : sizeof(bufPe),
1230 bufQe, input_Q->len ? input_Q->len : sizeof(bufQe),
1231 bufDe, input_D->len ? input_D->len : sizeof(bufDe),
1232 NULL, 0) == 0);
1233 } else {
1234 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
1235 bufPe, input_P->len ? input_P->len : sizeof(bufPe),
1236 NULL, 0, NULL, 0,
1237 NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0, NULL, 0,
1240 bufQe, input_Q->len ? input_Q->len : sizeof(bufQe),
1241 NULL, 0, NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0, NULL, 0, NULL, 0,
1244 bufDe, input_D->len ? input_D->len : sizeof(bufDe),
1245 NULL, 0) == 0);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001246 }
1247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (input_P->len) {
1249 TEST_ASSERT(memcmp(input_P->x, bufPe, input_P->len) == 0);
1250 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if (input_Q->len) {
1253 TEST_ASSERT(memcmp(input_Q->x, bufQe, input_Q->len) == 0);
1254 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (input_D->len) {
1257 TEST_ASSERT(memcmp(input_D->x, bufDe, input_D->len) == 0);
1258 }
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001259
1260 }
1261
1262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 mbedtls_rsa_free(&ctx);
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001264}
1265/* END_CASE */
1266
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001267/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001268void mbedtls_rsa_import_raw(data_t *input_N,
1269 data_t *input_P, data_t *input_Q,
1270 data_t *input_D, data_t *input_E,
1271 int successive,
1272 int is_priv,
1273 int res_check,
1274 int res_complete)
Hanno Beckerc77ab892017-08-23 11:01:06 +01001275{
Hanno Beckere1582a82017-09-29 11:51:05 +01001276 /* Buffers used for encryption-decryption test */
1277 unsigned char *buf_orig = NULL;
1278 unsigned char *buf_enc = NULL;
1279 unsigned char *buf_dec = NULL;
1280
Hanno Beckerc77ab892017-08-23 11:01:06 +01001281 mbedtls_rsa_context ctx;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 mbedtls_rsa_init(&ctx);
Hanno Becker3f3ae852017-10-02 10:08:39 +01001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (!successive) {
1286 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1287 (input_N->len > 0) ? input_N->x : NULL, input_N->len,
1288 (input_P->len > 0) ? input_P->x : NULL, input_P->len,
1289 (input_Q->len > 0) ? input_Q->x : NULL, input_Q->len,
1290 (input_D->len > 0) ? input_D->x : NULL, input_D->len,
1291 (input_E->len > 0) ? input_E->x : NULL,
1292 input_E->len) == 0);
1293 } else {
Hanno Beckerc77ab892017-08-23 11:01:06 +01001294 /* Import N, P, Q, D, E separately.
1295 * This should make no functional difference. */
1296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1298 (input_N->len > 0) ? input_N->x : NULL, input_N->len,
1299 NULL, 0, NULL, 0, NULL, 0, NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1302 NULL, 0,
1303 (input_P->len > 0) ? input_P->x : NULL, input_P->len,
1304 NULL, 0, NULL, 0, NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1307 NULL, 0, NULL, 0,
1308 (input_Q->len > 0) ? input_Q->x : NULL, input_Q->len,
1309 NULL, 0, NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1312 NULL, 0, NULL, 0, NULL, 0,
1313 (input_D->len > 0) ? input_D->x : NULL, input_D->len,
1314 NULL, 0) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
1317 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1318 (input_E->len > 0) ? input_E->x : NULL,
1319 input_E->len) == 0);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001320 }
1321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 TEST_ASSERT(mbedtls_rsa_complete(&ctx) == res_complete);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001323
Hanno Beckere1582a82017-09-29 11:51:05 +01001324 /* On expected success, perform some public and private
1325 * key operations to check if the key is working properly. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 if (res_complete == 0) {
1327 if (is_priv) {
1328 TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == res_check);
1329 } else {
1330 TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == res_check);
1331 }
Hanno Becker04877a42017-10-11 10:01:33 +01001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if (res_check != 0) {
Hanno Becker04877a42017-10-11 10:01:33 +01001334 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 }
Hanno Beckere1582a82017-09-29 11:51:05 +01001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 buf_orig = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1338 buf_enc = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1339 buf_dec = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
1340 if (buf_orig == NULL || buf_enc == NULL || buf_dec == NULL) {
Hanno Beckere1582a82017-09-29 11:51:05 +01001341 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 }
Hanno Beckere1582a82017-09-29 11:51:05 +01001343
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001344 /* This test uses an insecure RNG, suitable only for testing.
1345 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL,
1347 buf_orig, mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001348
1349 /* Make sure the number we're generating is smaller than the modulus */
1350 buf_orig[0] = 0x00;
1351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 TEST_ASSERT(mbedtls_rsa_public(&ctx, buf_orig, buf_enc) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 if (is_priv) {
Manuel Pégourié-Gonnard5ef4e8d2022-07-16 08:57:19 +02001355 /* This test uses an insecure RNG, suitable only for testing.
1356 * In production, always use a cryptographically strong RNG! */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_std_rand,
1358 NULL, buf_enc,
1359 buf_dec) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 TEST_ASSERT(memcmp(buf_orig, buf_dec,
1362 mbedtls_rsa_get_len(&ctx)) == 0);
Hanno Beckere1582a82017-09-29 11:51:05 +01001363 }
1364 }
1365
Hanno Beckerc77ab892017-08-23 11:01:06 +01001366exit:
1367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 mbedtls_free(buf_orig);
1369 mbedtls_free(buf_enc);
1370 mbedtls_free(buf_dec);
Hanno Becker3f3ae852017-10-02 10:08:39 +01001371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 mbedtls_rsa_free(&ctx);
Hanno Beckerc77ab892017-08-23 11:01:06 +01001373}
1374/* END_CASE */
1375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +01001377void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +00001378{
Manuel Pégourié-Gonnardfb8d90a2023-03-16 10:47:59 +01001379 MD_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 TEST_ASSERT(mbedtls_rsa_self_test(1) == 0);
Manuel Pégourié-Gonnardfb8d90a2023-03-16 10:47:59 +01001381
1382exit:
1383 MD_PSA_DONE();
Paul Bakker42a29bf2009-07-07 20:18:41 +00001384}
Paul Bakker33b43f12013-08-20 11:48:36 +02001385/* END_CASE */