blob: efea5c169a893621693742f24816aa6965c39018 [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"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/md2.h"
5#include "mbedtls/md4.h"
6#include "mbedtls/md5.h"
7#include "mbedtls/sha1.h"
8#include "mbedtls/sha256.h"
9#include "mbedtls/sha512.h"
10#include "mbedtls/entropy.h"
11#include "mbedtls/ctr_drbg.h"
Hanno Becker47deec42017-07-24 12:27:09 +010012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +000014
Paul Bakker33b43f12013-08-20 11:48:36 +020015/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +020017 * END_DEPENDENCIES
18 */
Paul Bakker5690efc2011-05-26 13:16:06 +000019
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050020/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
21void rsa_invalid_param( )
22{
23 mbedtls_rsa_context ctx;
24 const int valid_padding = MBEDTLS_RSA_PKCS_V21;
25 const int invalid_padding = 42;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050026 unsigned char buf[42] = { 0 };
27 size_t olen;
28
29 TEST_INVALID_PARAM( mbedtls_rsa_init( NULL, valid_padding, 0 ) );
30 TEST_INVALID_PARAM( mbedtls_rsa_init( &ctx, invalid_padding, 0 ) );
31 TEST_VALID_PARAM( mbedtls_rsa_free( NULL ) );
32
33 /* No more variants because only the first argument must be non-NULL. */
34 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
35 mbedtls_rsa_import( NULL, NULL, NULL,
36 NULL, NULL, NULL ) );
37 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
38 mbedtls_rsa_import_raw( NULL,
39 NULL, 0,
40 NULL, 0,
41 NULL, 0,
42 NULL, 0,
43 NULL, 0 ) );
44
45 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
46 mbedtls_rsa_complete( NULL ) );
47
48 /* No more variants because only the first argument must be non-NULL. */
49 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
50 mbedtls_rsa_export( NULL, NULL, NULL,
51 NULL, NULL, NULL ) );
52 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
53 mbedtls_rsa_export_raw( NULL,
54 NULL, 0,
55 NULL, 0,
56 NULL, 0,
57 NULL, 0,
58 NULL, 0 ) );
59 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
60 mbedtls_rsa_export_crt( NULL, NULL, NULL, NULL ) );
61
62 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( NULL,
63 valid_padding, 0 ) );
64 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( &ctx,
65 invalid_padding, 0 ) );
66
67 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +020068 mbedtls_rsa_gen_key( NULL,
69 mbedtls_test_rnd_std_rand,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050070 NULL, 0, 0 ) );
71 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
72 mbedtls_rsa_gen_key( &ctx, NULL,
73 NULL, 0, 0 ) );
74
75 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
76 mbedtls_rsa_check_pubkey( NULL ) );
77 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
78 mbedtls_rsa_check_privkey( NULL ) );
79
80 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
81 mbedtls_rsa_check_pub_priv( NULL, &ctx ) );
82 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
83 mbedtls_rsa_check_pub_priv( &ctx, NULL ) );
84
85 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
86 mbedtls_rsa_public( NULL, buf, buf ) );
87 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
88 mbedtls_rsa_public( &ctx, NULL, buf ) );
89 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
90 mbedtls_rsa_public( &ctx, buf, NULL ) );
91
92 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
93 mbedtls_rsa_private( NULL, NULL, NULL,
94 buf, buf ) );
95 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
96 mbedtls_rsa_private( &ctx, NULL, NULL,
97 NULL, buf ) );
98 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
99 mbedtls_rsa_private( &ctx, NULL, NULL,
100 buf, NULL ) );
101
102 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
103 mbedtls_rsa_pkcs1_encrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500104 sizeof( buf ), buf,
105 buf ) );
106 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
107 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500108 sizeof( buf ), NULL,
109 buf ) );
110 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
111 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500112 sizeof( buf ), buf,
113 NULL ) );
114
115 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
116 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100117 NULL, sizeof( buf ),
118 buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500119 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
120 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100121 NULL, sizeof( buf ),
122 NULL, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500123 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
124 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100125 NULL, sizeof( buf ),
126 buf, NULL ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500127
128 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
129 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500130 buf, sizeof( buf ),
131 sizeof( buf ), buf,
132 buf ) );
133 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
134 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500135 NULL, sizeof( buf ),
136 sizeof( buf ), buf,
137 buf ) );
138 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
139 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500140 buf, sizeof( buf ),
141 sizeof( buf ), NULL,
142 buf ) );
143 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
144 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500145 buf, sizeof( buf ),
146 sizeof( buf ), buf,
147 NULL ) );
148
149 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
150 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100151 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500152 buf, buf, 42 ) );
153 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
154 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100155 NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500156 buf, buf, 42 ) );
157 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
158 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100159 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500160 NULL, buf, 42 ) );
161 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
162 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100163 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500164 buf, NULL, 42 ) );
165
166 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
167 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100168 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500169 buf, buf, 42 ) );
170 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
171 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100172 NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500173 buf, buf, 42 ) );
174 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
175 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100176 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500177 NULL, buf, 42 ) );
178 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
179 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100180 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500181 buf, NULL, 42 ) );
182
183 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
184 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500185 buf, sizeof( buf ),
186 &olen,
187 buf, buf, 42 ) );
188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
189 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500190 NULL, sizeof( buf ),
191 NULL,
192 buf, buf, 42 ) );
193 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
194 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500195 buf, sizeof( buf ),
196 &olen,
197 NULL, buf, 42 ) );
198 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
199 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500200 buf, sizeof( buf ),
201 &olen,
202 buf, NULL, 42 ) );
203
204 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
205 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500206 0, sizeof( buf ), buf,
207 buf ) );
208 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
209 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500210 0, sizeof( buf ), NULL,
211 buf ) );
212 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
213 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214 0, sizeof( buf ), buf,
215 NULL ) );
216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
217 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218 MBEDTLS_MD_SHA1,
219 0, NULL,
220 buf ) );
221
222 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
223 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500224 0, sizeof( buf ), buf,
225 buf ) );
226 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
227 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500228 0, sizeof( buf ), NULL,
229 buf ) );
230 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
231 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500232 0, sizeof( buf ), buf,
233 NULL ) );
234 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
235 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500236 MBEDTLS_MD_SHA1,
237 0, NULL,
238 buf ) );
239
240 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
241 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500242 0, sizeof( buf ), buf,
243 buf ) );
244 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
245 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500246 0, sizeof( buf ), NULL,
247 buf ) );
248 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
249 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500250 0, sizeof( buf ), buf,
251 NULL ) );
252 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
253 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500254 MBEDTLS_MD_SHA1,
255 0, NULL,
256 buf ) );
257
258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Cédric Meutera05cbec2020-04-25 15:02:34 +0200259 mbedtls_rsa_rsassa_pss_sign_ext( NULL, NULL, NULL,
260 0, sizeof( buf ), buf,
261 MBEDTLS_RSA_SALT_LEN_ANY,
262 buf ) );
263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
264 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
265 0, sizeof( buf ), NULL,
266 MBEDTLS_RSA_SALT_LEN_ANY,
267 buf ) );
268 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
269 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
270 0, sizeof( buf ), buf,
271 MBEDTLS_RSA_SALT_LEN_ANY,
272 NULL ) );
273 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
274 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
275 MBEDTLS_MD_SHA1,
276 0, NULL,
277 MBEDTLS_RSA_SALT_LEN_ANY,
278 buf ) );
279
280 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100281 mbedtls_rsa_pkcs1_verify( NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 0, sizeof( buf ), buf,
283 buf ) );
284 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100285 mbedtls_rsa_pkcs1_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286 0, sizeof( buf ), NULL,
287 buf ) );
288 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100289 mbedtls_rsa_pkcs1_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500290 0, sizeof( buf ), buf,
291 NULL ) );
292 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100293 mbedtls_rsa_pkcs1_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500294 MBEDTLS_MD_SHA1, 0, NULL,
295 buf ) );
296
297 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney475053d2021-05-19 11:44:27 +0100298 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500299 0, sizeof( buf ), buf,
300 buf ) );
301 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney475053d2021-05-19 11:44:27 +0100302 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500303 0, sizeof( buf ),
304 NULL, buf ) );
305 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney475053d2021-05-19 11:44:27 +0100306 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500307 0, sizeof( buf ), buf,
308 NULL ) );
309 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney475053d2021-05-19 11:44:27 +0100310 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500311 MBEDTLS_MD_SHA1,
312 0, NULL,
313 buf ) );
314
315 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney718a53d2021-05-19 12:01:35 +0100316 mbedtls_rsa_rsassa_pss_verify( NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500317 0, sizeof( buf ),
318 buf, buf ) );
319 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney718a53d2021-05-19 12:01:35 +0100320 mbedtls_rsa_rsassa_pss_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500321 0, sizeof( buf ),
322 NULL, buf ) );
323 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney718a53d2021-05-19 12:01:35 +0100324 mbedtls_rsa_rsassa_pss_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500325 0, sizeof( buf ),
326 buf, NULL ) );
327 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney718a53d2021-05-19 12:01:35 +0100328 mbedtls_rsa_rsassa_pss_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500329 MBEDTLS_MD_SHA1,
330 0, NULL,
331 buf ) );
332
333 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney9e65f792021-05-19 12:18:58 +0100334 mbedtls_rsa_rsassa_pss_verify_ext( NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500335 0, sizeof( buf ),
336 buf,
337 0, 0,
338 buf ) );
339 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney9e65f792021-05-19 12:18:58 +0100340 mbedtls_rsa_rsassa_pss_verify_ext( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500341 0, sizeof( buf ),
342 NULL, 0, 0,
343 buf ) );
344 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney9e65f792021-05-19 12:18:58 +0100345 mbedtls_rsa_rsassa_pss_verify_ext( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500346 0, sizeof( buf ),
347 buf, 0, 0,
348 NULL ) );
349 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney9e65f792021-05-19 12:18:58 +0100350 mbedtls_rsa_rsassa_pss_verify_ext( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500351 MBEDTLS_MD_SHA1,
352 0, NULL,
353 0, 0,
354 buf ) );
355
356 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
357 mbedtls_rsa_copy( NULL, &ctx ) );
358 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
359 mbedtls_rsa_copy( &ctx, NULL ) );
360
361exit:
362 return;
363}
364/* END_CASE */
365
Paul Bakker33b43f12013-08-20 11:48:36 +0200366/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100367void rsa_init_free( int reinit )
368{
369 mbedtls_rsa_context ctx;
370
371 /* Double free is not explicitly documented to work, but we rely on it
372 * even inside the library so that you can call mbedtls_rsa_free()
373 * unconditionally on an error path without checking whether it has
374 * already been called in the success path. */
375
376 mbedtls_rsa_init( &ctx, 0, 0 );
377 mbedtls_rsa_free( &ctx );
378
379 if( reinit )
380 mbedtls_rsa_init( &ctx, 0, 0 );
381 mbedtls_rsa_free( &ctx );
382
383 /* This test case always succeeds, functionally speaking. A plausible
384 * bug might trigger an invalid pointer dereference or a memory leak. */
385 goto exit;
386}
387/* END_CASE */
388
389/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100390void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100391 int digest, int mod, int radix_P, char * input_P,
392 int radix_Q, char * input_Q, int radix_N,
393 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200394 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000395{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200396 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
397 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100399 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200400 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000401
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100402 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
403 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000405
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200406 memset( hash_result, 0x00, sizeof( hash_result ) );
407 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200408 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000409
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100410 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
411 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
412 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
413 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000414
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100415 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
416 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100417 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000419
Paul Bakker42a29bf2009-07-07 20:18:41 +0000420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100422 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000423
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200424 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100425 &rnd_info, digest, 0, hash_result,
426 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200427 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000428 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000429
Ronald Cronac6ae352020-06-26 14:33:03 +0200430 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
431 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000432 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000433
Paul Bakkerbd51b262014-07-10 15:26:12 +0200434exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100435 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
436 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000438}
Paul Bakker33b43f12013-08-20 11:48:36 +0200439/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000440
Paul Bakker33b43f12013-08-20 11:48:36 +0200441/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100442void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100443 int digest, int mod, int radix_N,
444 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100445 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000446{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200447 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000449
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100450 mbedtls_mpi N, E;
451
452 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200454 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000455
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100456 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
457 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
458 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
459 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000461
Paul Bakker42a29bf2009-07-07 20:18:41 +0000462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100464 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000465
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100466 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, digest, 0, hash_result, result_str->x ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100467
Paul Bakkerbd51b262014-07-10 15:26:12 +0200468exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100469 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000471}
Paul Bakker33b43f12013-08-20 11:48:36 +0200472/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000473
Paul Bakker821fb082009-07-12 13:26:42 +0000474
Paul Bakker33b43f12013-08-20 11:48:36 +0200475/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100476void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100477 int padding_mode, int mod, int radix_P,
478 char * input_P, int radix_Q, char * input_Q,
479 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200480 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000481{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200482 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100484 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200485 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100488 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
489 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000490
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200491 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200492 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000493
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100494 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
495 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
496 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
497 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000498
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100499 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
500 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100501 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000503
Paul Bakker821fb082009-07-12 13:26:42 +0000504
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200505 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100506 &rnd_info, MBEDTLS_MD_NONE,
507 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200508 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000509
Paul Bakker821fb082009-07-12 13:26:42 +0000510
Ronald Cronac6ae352020-06-26 14:33:03 +0200511 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
512 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000513
Paul Bakkerbd51b262014-07-10 15:26:12 +0200514exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100515 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
516 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000519}
Paul Bakker33b43f12013-08-20 11:48:36 +0200520/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000521
Paul Bakker33b43f12013-08-20 11:48:36 +0200522/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100523void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200524 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100525 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100526 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000527{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200528 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000530
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100531 mbedtls_mpi N, E;
532 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100535 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000536
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100537 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
538 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000539
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100540 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
541 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000543
Paul Bakker821fb082009-07-12 13:26:42 +0000544
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100545 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, MBEDTLS_MD_NONE, hash_result->len, hash_result->x, result_str->x ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100546
Paul Bakkerbd51b262014-07-10 15:26:12 +0200547exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100548 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000550}
Paul Bakker33b43f12013-08-20 11:48:36 +0200551/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000552
Paul Bakker33b43f12013-08-20 11:48:36 +0200553/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100554void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100555 int mod, int radix_N, char * input_N,
556 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200557 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000558{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200559 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200561 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000562
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100563 mbedtls_mpi N, E;
564 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
565
Ronald Cron351f0ee2020-06-10 12:12:18 +0200566 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200569 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000570
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100571 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
572 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000573
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100574 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
575 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000577
Paul Bakker42a29bf2009-07-07 20:18:41 +0000578
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200579 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
580 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100581 &rnd_info, message_str->len,
582 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200583 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200584 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000585 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000586
Ronald Cronac6ae352020-06-26 14:33:03 +0200587 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
588 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000589 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100590
Paul Bakkerbd51b262014-07-10 15:26:12 +0200591exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100592 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000594}
Paul Bakker33b43f12013-08-20 11:48:36 +0200595/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000596
Paul Bakker33b43f12013-08-20 11:48:36 +0200597/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100598void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100599 int mod, int radix_N, char * input_N,
600 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200601 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000602{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200603 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000605
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100606 mbedtls_mpi N, E;
607
608 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200610 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000611
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100612 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
613 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000614
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100615 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
616 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000618
Paul Bakkera6656852010-07-18 19:47:14 +0000619
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200620 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100621 NULL, message_str->len,
622 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200623 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200624 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000625 {
Paul Bakkera6656852010-07-18 19:47:14 +0000626
Ronald Cronac6ae352020-06-26 14:33:03 +0200627 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
628 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000629 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100630
Paul Bakkerbd51b262014-07-10 15:26:12 +0200631exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100632 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000634}
Paul Bakker33b43f12013-08-20 11:48:36 +0200635/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000636
Paul Bakker33b43f12013-08-20 11:48:36 +0200637/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100638void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100639 int mod, int radix_P, char * input_P,
640 int radix_Q, char * input_Q, int radix_N,
641 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200642 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100643 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000644{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200645 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000647 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200648 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100649 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000650
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100651 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
652 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000655
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200656 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200657 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000658
Paul Bakker42a29bf2009-07-07 20:18:41 +0000659
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100660 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
661 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
662 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
663 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000664
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100665 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
666 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100667 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000669
Paul Bakker69998dd2009-07-11 19:15:20 +0000670 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000671
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200672 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100673 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200674 &output_len, message_str->x, output,
675 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200676 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000677 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000678
Ronald Cronac6ae352020-06-26 14:33:03 +0200679 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200680 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200681 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000682 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000683
Paul Bakkerbd51b262014-07-10 15:26:12 +0200684exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100685 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
686 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000688}
Paul Bakker33b43f12013-08-20 11:48:36 +0200689/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000690
Paul Bakker33b43f12013-08-20 11:48:36 +0200691/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100692void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100693 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200694 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000695{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200696 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000698
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100699 mbedtls_mpi N, E;
700
701 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
703 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200704 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000705
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100706 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
707 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000708
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100709 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
710 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000712
Paul Bakker821fb082009-07-12 13:26:42 +0000713
Azim Khand30ca132017-06-09 04:32:58 +0100714 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200715 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000716 {
Paul Bakker821fb082009-07-12 13:26:42 +0000717
Ronald Cronac6ae352020-06-26 14:33:03 +0200718 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
719 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000720 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100721
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100722 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200724 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100728
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200729 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100730 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100731 if( result == 0 )
732 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100733
Ronald Cronac6ae352020-06-26 14:33:03 +0200734 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
735 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100736 }
737
Paul Bakkerbd51b262014-07-10 15:26:12 +0200738exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100739 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_rsa_free( &ctx );
741 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000742}
Paul Bakker33b43f12013-08-20 11:48:36 +0200743/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000744
Paul Bakker33b43f12013-08-20 11:48:36 +0200745/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100746void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100747 char * input_P, int radix_Q, char * input_Q,
748 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200749 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100750 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000751{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200752 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100754 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200755 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200756 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000757
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100758 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
759 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
761 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000762
Ronald Cron351f0ee2020-06-10 12:12:18 +0200763 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000764
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100765 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
766 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
767 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
768 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000769
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100770 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
771 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100772 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000774
Paul Bakker821fb082009-07-12 13:26:42 +0000775
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200776 /* repeat three times to test updating of blinding values */
777 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000778 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200779 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200780 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
781 &rnd_info, message_str->x,
782 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200783 if( result == 0 )
784 {
Paul Bakker821fb082009-07-12 13:26:42 +0000785
Ronald Cronac6ae352020-06-26 14:33:03 +0200786 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200787 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200788 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200789 }
Paul Bakker821fb082009-07-12 13:26:42 +0000790 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000791
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100792 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200794 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100798
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200799 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200800 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
801 &rnd_info, message_str->x,
802 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100803 if( result == 0 )
804 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100805
Ronald Cronac6ae352020-06-26 14:33:03 +0200806 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200807 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200808 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100809 }
810
Paul Bakkerbd51b262014-07-10 15:26:12 +0200811exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100812 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
813 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000816}
Paul Bakker33b43f12013-08-20 11:48:36 +0200817/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000818
Paul Bakker33b43f12013-08-20 11:48:36 +0200819/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100820void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000821{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_rsa_context ctx;
823 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000826}
Paul Bakker33b43f12013-08-20 11:48:36 +0200827/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000828
Paul Bakker33b43f12013-08-20 11:48:36 +0200829/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100830void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
831 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100834 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000835
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100836 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000838
Paul Bakker33b43f12013-08-20 11:48:36 +0200839 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000840 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100841 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000842 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200843 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000844 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100845 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000846 }
847
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100848 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100850
Paul Bakkerbd51b262014-07-10 15:26:12 +0200851exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100852 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000854}
Paul Bakker33b43f12013-08-20 11:48:36 +0200855/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000856
Paul Bakker33b43f12013-08-20 11:48:36 +0200857/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100858void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
859 int radix_Q, char * input_Q, int radix_N,
860 char * input_N, int radix_E, char * input_E,
861 int radix_D, char * input_D, int radix_DP,
862 char * input_DP, int radix_DQ,
863 char * input_DQ, int radix_QP,
864 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000865{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000869
Paul Bakker33b43f12013-08-20 11:48:36 +0200870 ctx.len = mod / 8;
871 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000874 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200875 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000878 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200879 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000882 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200883 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000886 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200887 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000890 }
Hanno Becker131134f2017-08-23 08:31:07 +0100891#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200892 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000895 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200896 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000899 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200900 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000903 }
Hanno Becker131134f2017-08-23 08:31:07 +0100904#else
905 ((void) radix_DP); ((void) input_DP);
906 ((void) radix_DQ); ((void) input_DQ);
907 ((void) radix_QP); ((void) input_QP);
908#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100911
Paul Bakkerbd51b262014-07-10 15:26:12 +0200912exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000914}
Paul Bakker33b43f12013-08-20 11:48:36 +0200915/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000916
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100917/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100918void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
919 int radix_Epub, char * input_Epub, int radix_P,
920 char * input_P, int radix_Q, char * input_Q,
921 int radix_N, char * input_N, int radix_E,
922 char * input_E, int radix_D, char * input_D,
923 int radix_DP, char * input_DP, int radix_DQ,
924 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100925 int result )
926{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
930 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100931
932 pub.len = mod / 8;
933 prv.len = mod / 8;
934
935 if( strlen( input_Npub ) )
936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100938 }
939 if( strlen( input_Epub ) )
940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100942 }
943
944 if( strlen( input_P ) )
945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100947 }
948 if( strlen( input_Q ) )
949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100951 }
952 if( strlen( input_N ) )
953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100955 }
956 if( strlen( input_E ) )
957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100959 }
960 if( strlen( input_D ) )
961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100963 }
Hanno Becker131134f2017-08-23 08:31:07 +0100964#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100965 if( strlen( input_DP ) )
966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100968 }
969 if( strlen( input_DQ ) )
970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100972 }
973 if( strlen( input_QP ) )
974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100976 }
Hanno Becker131134f2017-08-23 08:31:07 +0100977#else
978 ((void) radix_DP); ((void) input_DP);
979 ((void) radix_DQ); ((void) input_DQ);
980 ((void) radix_QP); ((void) input_QP);
981#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100984
985exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_rsa_free( &pub );
987 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100988}
989/* END_CASE */
990
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100991/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000993{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_rsa_context ctx;
995 mbedtls_entropy_context entropy;
996 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200997 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000998
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200999 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001001 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001002
Hanno Beckera47023e2017-12-22 17:08:03 +00001003 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1004 &entropy, (const unsigned char *) pers,
1005 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001008 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001011 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001012 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001013
Paul Bakkerbd51b262014-07-10 15:26:12 +02001014exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_rsa_free( &ctx );
1016 mbedtls_ctr_drbg_free( &ctr_drbg );
1017 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001018}
Paul Bakker33b43f12013-08-20 11:48:36 +02001019/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001020
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001021/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001022void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001023 int radix_D, char *input_D,
1024 int radix_E, char *input_E,
1025 int radix_P, char *output_P,
1026 int radix_Q, char *output_Q,
1027 int corrupt, int result )
1028{
1029 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1030
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001031 mbedtls_mpi_init( &N );
1032 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1033 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1034 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1035
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001036 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1037 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1038 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1039 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1040 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1041
1042 if( corrupt )
1043 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1044
1045 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001046 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001047
1048 if( !corrupt )
1049 {
1050 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1051 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1052 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1053 }
1054
1055exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001056 mbedtls_mpi_free( &N );
1057 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1058 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1059 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001060}
1061/* END_CASE */
1062
Hanno Becker6b4ce492017-08-23 11:00:21 +01001063/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001064void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1065 int radix_Q, char *input_Q,
1066 int radix_E, char *input_E,
1067 int radix_D, char *output_D,
1068 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001069{
1070 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1071
1072 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1073 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1074 mbedtls_mpi_init( &E );
1075 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1076
1077 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1078 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1079 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1080 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1081
1082 if( corrupt )
1083 {
1084 /* Make E even */
1085 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1086 }
1087
1088 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001089 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1090 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001091
1092 if( !corrupt )
1093 {
1094 /*
1095 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1096 */
1097
1098 /* Replace P,Q by P-1, Q-1 */
1099 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1100 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1101
1102 /* Check D == Dp modulo P-1 */
1103 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1104 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1105 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1106
1107 /* Check D == Dp modulo Q-1 */
1108 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1109 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1110 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1111 }
1112
1113exit:
1114
1115 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1116 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1117 mbedtls_mpi_free( &E );
1118 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1119}
1120/* END_CASE */
1121
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001122/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001123void mbedtls_rsa_import( int radix_N, char *input_N,
1124 int radix_P, char *input_P,
1125 int radix_Q, char *input_Q,
1126 int radix_D, char *input_D,
1127 int radix_E, char *input_E,
1128 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001129 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001130 int res_check,
1131 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001132{
1133 mbedtls_mpi N, P, Q, D, E;
1134 mbedtls_rsa_context ctx;
1135
Hanno Beckere1582a82017-09-29 11:51:05 +01001136 /* Buffers used for encryption-decryption test */
1137 unsigned char *buf_orig = NULL;
1138 unsigned char *buf_enc = NULL;
1139 unsigned char *buf_dec = NULL;
1140
Hanno Beckerc77ab892017-08-23 11:01:06 +01001141 mbedtls_entropy_context entropy;
1142 mbedtls_ctr_drbg_context ctr_drbg;
1143 const char *pers = "test_suite_rsa";
1144
Hanno Becker4d6e8342017-09-29 11:50:18 +01001145 const int have_N = ( strlen( input_N ) > 0 );
1146 const int have_P = ( strlen( input_P ) > 0 );
1147 const int have_Q = ( strlen( input_Q ) > 0 );
1148 const int have_D = ( strlen( input_D ) > 0 );
1149 const int have_E = ( strlen( input_E ) > 0 );
1150
Hanno Beckerc77ab892017-08-23 11:01:06 +01001151 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001152 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001153 mbedtls_rsa_init( &ctx, 0, 0 );
1154
1155 mbedtls_mpi_init( &N );
1156 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1157 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1158
Hanno Beckerd4d60572018-01-10 07:12:01 +00001159 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1160 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1161
Hanno Becker4d6e8342017-09-29 11:50:18 +01001162 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001163 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1164
Hanno Becker4d6e8342017-09-29 11:50:18 +01001165 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001166 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1167
Hanno Becker4d6e8342017-09-29 11:50:18 +01001168 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001169 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1170
Hanno Becker4d6e8342017-09-29 11:50:18 +01001171 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001172 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1173
Hanno Becker4d6e8342017-09-29 11:50:18 +01001174 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001175 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1176
1177 if( !successive )
1178 {
1179 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001180 have_N ? &N : NULL,
1181 have_P ? &P : NULL,
1182 have_Q ? &Q : NULL,
1183 have_D ? &D : NULL,
1184 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001185 }
1186 else
1187 {
1188 /* Import N, P, Q, D, E separately.
1189 * This should make no functional difference. */
1190
1191 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001192 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001193 NULL, NULL, NULL, NULL ) == 0 );
1194
1195 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1196 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001197 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001198 NULL, NULL, NULL ) == 0 );
1199
1200 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1201 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001202 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001203 NULL, NULL ) == 0 );
1204
1205 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1206 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001207 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001208 NULL ) == 0 );
1209
1210 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1211 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001212 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001213 }
1214
Hanno Becker04877a42017-10-11 10:01:33 +01001215 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001216
Hanno Beckere1582a82017-09-29 11:51:05 +01001217 /* On expected success, perform some public and private
1218 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001219 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001220 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001221 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001222 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1223 else
1224 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1225
1226 if( res_check != 0 )
1227 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001228
1229 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1230 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1231 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1232 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1233 goto exit;
1234
1235 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1236 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1237
1238 /* Make sure the number we're generating is smaller than the modulus */
1239 buf_orig[0] = 0x00;
1240
1241 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1242
1243 if( is_priv )
1244 {
1245 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1246 &ctr_drbg, buf_enc,
1247 buf_dec ) == 0 );
1248
1249 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1250 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1251 }
1252 }
1253
Hanno Beckerc77ab892017-08-23 11:01:06 +01001254exit:
1255
Hanno Beckere1582a82017-09-29 11:51:05 +01001256 mbedtls_free( buf_orig );
1257 mbedtls_free( buf_enc );
1258 mbedtls_free( buf_dec );
1259
Hanno Beckerc77ab892017-08-23 11:01:06 +01001260 mbedtls_rsa_free( &ctx );
1261
1262 mbedtls_ctr_drbg_free( &ctr_drbg );
1263 mbedtls_entropy_free( &entropy );
1264
1265 mbedtls_mpi_free( &N );
1266 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1267 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1268}
1269/* END_CASE */
1270
Hanno Becker417f2d62017-08-23 11:44:51 +01001271/* BEGIN_CASE */
1272void mbedtls_rsa_export( int radix_N, char *input_N,
1273 int radix_P, char *input_P,
1274 int radix_Q, char *input_Q,
1275 int radix_D, char *input_D,
1276 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001277 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001278 int successive )
1279{
1280 /* Original MPI's with which we set up the RSA context */
1281 mbedtls_mpi N, P, Q, D, E;
1282
1283 /* Exported MPI's */
1284 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1285
1286 const int have_N = ( strlen( input_N ) > 0 );
1287 const int have_P = ( strlen( input_P ) > 0 );
1288 const int have_Q = ( strlen( input_Q ) > 0 );
1289 const int have_D = ( strlen( input_D ) > 0 );
1290 const int have_E = ( strlen( input_E ) > 0 );
1291
Hanno Becker417f2d62017-08-23 11:44:51 +01001292 mbedtls_rsa_context ctx;
1293
1294 mbedtls_rsa_init( &ctx, 0, 0 );
1295
1296 mbedtls_mpi_init( &N );
1297 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1298 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1299
1300 mbedtls_mpi_init( &Ne );
1301 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1302 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1303
1304 /* Setup RSA context */
1305
1306 if( have_N )
1307 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1308
1309 if( have_P )
1310 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1311
1312 if( have_Q )
1313 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1314
1315 if( have_D )
1316 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1317
1318 if( have_E )
1319 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1320
1321 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1322 strlen( input_N ) ? &N : NULL,
1323 strlen( input_P ) ? &P : NULL,
1324 strlen( input_Q ) ? &Q : NULL,
1325 strlen( input_D ) ? &D : NULL,
1326 strlen( input_E ) ? &E : NULL ) == 0 );
1327
Hanno Becker7f25f852017-10-10 16:56:22 +01001328 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001329
1330 /*
1331 * Export parameters and compare to original ones.
1332 */
1333
1334 /* N and E must always be present. */
1335 if( !successive )
1336 {
1337 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1338 }
1339 else
1340 {
1341 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1342 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1343 }
1344 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1345 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1346
1347 /* If we were providing enough information to setup a complete private context,
1348 * we expect to be able to export all core parameters. */
1349
1350 if( is_priv )
1351 {
1352 if( !successive )
1353 {
1354 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1355 &De, NULL ) == 0 );
1356 }
1357 else
1358 {
1359 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1360 NULL, NULL ) == 0 );
1361 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1362 NULL, NULL ) == 0 );
1363 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1364 &De, NULL ) == 0 );
1365 }
1366
1367 if( have_P )
1368 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1369
1370 if( have_Q )
1371 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1372
1373 if( have_D )
1374 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1375
1376 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001377 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1378 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001379 }
1380
1381exit:
1382
1383 mbedtls_rsa_free( &ctx );
1384
1385 mbedtls_mpi_free( &N );
1386 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1387 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1388
1389 mbedtls_mpi_free( &Ne );
1390 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1391 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1392}
1393/* END_CASE */
1394
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001395/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001396void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1397 int radix_P, char *input_P,
1398 int radix_Q, char *input_Q,
1399 int radix_D, char *input_D,
1400 int radix_E, char *input_E,
1401 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001402{
1403 /* Original MPI's with which we set up the RSA context */
1404 mbedtls_mpi N, P, Q, D, E;
1405
1406 const int have_N = ( strlen( input_N ) > 0 );
1407 const int have_P = ( strlen( input_P ) > 0 );
1408 const int have_Q = ( strlen( input_Q ) > 0 );
1409 const int have_D = ( strlen( input_D ) > 0 );
1410 const int have_E = ( strlen( input_E ) > 0 );
1411
1412 mbedtls_entropy_context entropy;
1413 mbedtls_ctr_drbg_context ctr_drbg;
1414 const char *pers = "test_suite_rsa";
1415
1416 mbedtls_mpi_init( &N );
1417 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1418 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1419
1420 mbedtls_ctr_drbg_init( &ctr_drbg );
1421 mbedtls_entropy_init( &entropy );
1422 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1423 &entropy, (const unsigned char *) pers,
1424 strlen( pers ) ) == 0 );
1425
1426 if( have_N )
1427 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1428
1429 if( have_P )
1430 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1431
1432 if( have_Q )
1433 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1434
1435 if( have_D )
1436 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1437
1438 if( have_E )
1439 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1440
Hanno Becker750e8b42017-08-25 07:54:27 +01001441 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1442 have_P ? &P : NULL,
1443 have_Q ? &Q : NULL,
1444 have_D ? &D : NULL,
1445 have_E ? &E : NULL,
1446 prng ? mbedtls_ctr_drbg_random : NULL,
1447 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001448exit:
1449
1450 mbedtls_ctr_drbg_free( &ctr_drbg );
1451 mbedtls_entropy_free( &entropy );
1452
1453 mbedtls_mpi_free( &N );
1454 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1455 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1456}
1457/* END_CASE */
1458
Hanno Beckerc77ab892017-08-23 11:01:06 +01001459/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001460void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1461 data_t *input_Q, data_t *input_D,
1462 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001463 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001464{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001465 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001466 unsigned char bufNe[256];
1467 unsigned char bufPe[128];
1468 unsigned char bufQe[128];
1469 unsigned char bufDe[256];
1470 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001471
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001472 mbedtls_rsa_context ctx;
1473
1474 mbedtls_rsa_init( &ctx, 0, 0 );
1475
1476 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001477 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001478 input_N->len ? input_N->x : NULL, input_N->len,
1479 input_P->len ? input_P->x : NULL, input_P->len,
1480 input_Q->len ? input_Q->x : NULL, input_Q->len,
1481 input_D->len ? input_D->x : NULL, input_D->len,
1482 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001483
Hanno Becker7f25f852017-10-10 16:56:22 +01001484 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001485
1486 /*
1487 * Export parameters and compare to original ones.
1488 */
1489
1490 /* N and E must always be present. */
1491 if( !successive )
1492 {
Azim Khand30ca132017-06-09 04:32:58 +01001493 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001494 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001495 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001496 }
1497 else
1498 {
Azim Khand30ca132017-06-09 04:32:58 +01001499 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001500 NULL, 0, NULL, 0, NULL, 0,
1501 NULL, 0 ) == 0 );
1502 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1503 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001504 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001505 }
Azim Khand30ca132017-06-09 04:32:58 +01001506 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1507 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001508
1509 /* If we were providing enough information to setup a complete private context,
1510 * we expect to be able to export all core parameters. */
1511
1512 if( is_priv )
1513 {
1514 if( !successive )
1515 {
1516 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001517 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1518 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1519 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001520 NULL, 0 ) == 0 );
1521 }
1522 else
1523 {
1524 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001525 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001526 NULL, 0, NULL, 0,
1527 NULL, 0 ) == 0 );
1528
1529 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001530 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001531 NULL, 0, NULL, 0 ) == 0 );
1532
Azim Khand30ca132017-06-09 04:32:58 +01001533 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1534 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001535 NULL, 0 ) == 0 );
1536 }
1537
Azim Khand30ca132017-06-09 04:32:58 +01001538 if( input_P->len )
1539 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001540
Azim Khand30ca132017-06-09 04:32:58 +01001541 if( input_Q->len )
1542 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001543
Azim Khand30ca132017-06-09 04:32:58 +01001544 if( input_D->len )
1545 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001546
1547 }
1548
1549exit:
1550 mbedtls_rsa_free( &ctx );
1551}
1552/* END_CASE */
1553
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001554/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001555void mbedtls_rsa_import_raw( data_t *input_N,
1556 data_t *input_P, data_t *input_Q,
1557 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001558 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001559 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001560 int res_check,
1561 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001562{
Hanno Beckere1582a82017-09-29 11:51:05 +01001563 /* Buffers used for encryption-decryption test */
1564 unsigned char *buf_orig = NULL;
1565 unsigned char *buf_enc = NULL;
1566 unsigned char *buf_dec = NULL;
1567
Hanno Beckerc77ab892017-08-23 11:01:06 +01001568 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001569 mbedtls_entropy_context entropy;
1570 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001571
Hanno Beckerc77ab892017-08-23 11:01:06 +01001572 const char *pers = "test_suite_rsa";
1573
1574 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001575 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001576 mbedtls_rsa_init( &ctx, 0, 0 );
1577
Hanno Beckerc77ab892017-08-23 11:01:06 +01001578 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1579 &entropy, (const unsigned char *) pers,
1580 strlen( pers ) ) == 0 );
1581
Hanno Beckerc77ab892017-08-23 11:01:06 +01001582 if( !successive )
1583 {
1584 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001585 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1586 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1587 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1588 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1589 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001590 }
1591 else
1592 {
1593 /* Import N, P, Q, D, E separately.
1594 * This should make no functional difference. */
1595
1596 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001597 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001598 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1599
1600 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1601 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001602 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001603 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1604
1605 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1606 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001607 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001608 NULL, 0, NULL, 0 ) == 0 );
1609
1610 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1611 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001612 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001613 NULL, 0 ) == 0 );
1614
1615 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1616 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001617 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001618 }
1619
Hanno Becker04877a42017-10-11 10:01:33 +01001620 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001621
Hanno Beckere1582a82017-09-29 11:51:05 +01001622 /* On expected success, perform some public and private
1623 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001624 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001625 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001626 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001627 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1628 else
1629 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1630
1631 if( res_check != 0 )
1632 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001633
1634 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1635 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1636 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1637 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1638 goto exit;
1639
1640 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1641 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1642
1643 /* Make sure the number we're generating is smaller than the modulus */
1644 buf_orig[0] = 0x00;
1645
1646 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1647
1648 if( is_priv )
1649 {
1650 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1651 &ctr_drbg, buf_enc,
1652 buf_dec ) == 0 );
1653
1654 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1655 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1656 }
1657 }
1658
Hanno Beckerc77ab892017-08-23 11:01:06 +01001659exit:
1660
Hanno Becker3f3ae852017-10-02 10:08:39 +01001661 mbedtls_free( buf_orig );
1662 mbedtls_free( buf_enc );
1663 mbedtls_free( buf_dec );
1664
Hanno Beckerc77ab892017-08-23 11:01:06 +01001665 mbedtls_rsa_free( &ctx );
1666
1667 mbedtls_ctr_drbg_free( &ctr_drbg );
1668 mbedtls_entropy_free( &entropy );
1669
1670}
1671/* END_CASE */
1672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001674void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001675{
Andres AG93012e82016-09-09 09:10:28 +01001676 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001677}
Paul Bakker33b43f12013-08-20 11:48:36 +02001678/* END_CASE */