blob: 7c7d8f9b69a978ed430402d25e85aac62eee78ff [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;
26 const int valid_mode = MBEDTLS_RSA_PRIVATE;
27 const int invalid_mode = 42;
28 unsigned char buf[42] = { 0 };
29 size_t olen;
30
31 TEST_INVALID_PARAM( mbedtls_rsa_init( NULL, valid_padding, 0 ) );
32 TEST_INVALID_PARAM( mbedtls_rsa_init( &ctx, invalid_padding, 0 ) );
33 TEST_VALID_PARAM( mbedtls_rsa_free( NULL ) );
34
35 /* No more variants because only the first argument must be non-NULL. */
36 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
37 mbedtls_rsa_import( NULL, NULL, NULL,
38 NULL, NULL, NULL ) );
39 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
40 mbedtls_rsa_import_raw( NULL,
41 NULL, 0,
42 NULL, 0,
43 NULL, 0,
44 NULL, 0,
45 NULL, 0 ) );
46
47 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
48 mbedtls_rsa_complete( NULL ) );
49
50 /* No more variants because only the first argument must be non-NULL. */
51 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
52 mbedtls_rsa_export( NULL, NULL, NULL,
53 NULL, NULL, NULL ) );
54 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
55 mbedtls_rsa_export_raw( NULL,
56 NULL, 0,
57 NULL, 0,
58 NULL, 0,
59 NULL, 0,
60 NULL, 0 ) );
61 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
62 mbedtls_rsa_export_crt( NULL, NULL, NULL, NULL ) );
63
64 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( NULL,
65 valid_padding, 0 ) );
66 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( &ctx,
67 invalid_padding, 0 ) );
68
69 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +020070 mbedtls_rsa_gen_key( NULL,
71 mbedtls_test_rnd_std_rand,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072 NULL, 0, 0 ) );
73 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
74 mbedtls_rsa_gen_key( &ctx, NULL,
75 NULL, 0, 0 ) );
76
77 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
78 mbedtls_rsa_check_pubkey( NULL ) );
79 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
80 mbedtls_rsa_check_privkey( NULL ) );
81
82 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
83 mbedtls_rsa_check_pub_priv( NULL, &ctx ) );
84 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
85 mbedtls_rsa_check_pub_priv( &ctx, NULL ) );
86
87 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
88 mbedtls_rsa_public( NULL, buf, buf ) );
89 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
90 mbedtls_rsa_public( &ctx, NULL, buf ) );
91 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
92 mbedtls_rsa_public( &ctx, buf, NULL ) );
93
94 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
95 mbedtls_rsa_private( NULL, NULL, NULL,
96 buf, buf ) );
97 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
98 mbedtls_rsa_private( &ctx, NULL, NULL,
99 NULL, buf ) );
100 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
101 mbedtls_rsa_private( &ctx, NULL, NULL,
102 buf, NULL ) );
103
104 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
105 mbedtls_rsa_pkcs1_encrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500106 sizeof( buf ), buf,
107 buf ) );
108 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
109 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 sizeof( buf ), NULL,
111 buf ) );
112 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
113 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500114 sizeof( buf ), buf,
115 NULL ) );
116
117 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
118 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100119 NULL, sizeof( buf ),
120 buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
122 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100123 NULL, sizeof( buf ),
124 NULL, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500125 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
126 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100127 NULL, sizeof( buf ),
128 buf, NULL ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500129
130 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
131 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132 buf, sizeof( buf ),
133 sizeof( buf ), buf,
134 buf ) );
135 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
136 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 NULL, sizeof( buf ),
138 sizeof( buf ), buf,
139 buf ) );
140 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
141 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 buf, sizeof( buf ),
143 sizeof( buf ), NULL,
144 buf ) );
145 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
146 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500147 buf, sizeof( buf ),
148 sizeof( buf ), buf,
149 NULL ) );
150
151 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
152 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100153 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500154 buf, buf, 42 ) );
155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
156 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100157 NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500158 buf, buf, 42 ) );
159 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
160 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100161 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500162 NULL, buf, 42 ) );
163 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
164 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100165 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500166 buf, NULL, 42 ) );
167
168 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
169 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100170 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500171 buf, buf, 42 ) );
172 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
173 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100174 NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500175 buf, buf, 42 ) );
176 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
177 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100178 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500179 NULL, buf, 42 ) );
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
181 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100182 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 buf, NULL, 42 ) );
184
185 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
186 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187 buf, sizeof( buf ),
188 &olen,
189 buf, buf, 42 ) );
190 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
191 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500192 NULL, sizeof( buf ),
193 NULL,
194 buf, buf, 42 ) );
195 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
196 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 buf, sizeof( buf ),
198 &olen,
199 NULL, buf, 42 ) );
200 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
201 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 buf, sizeof( buf ),
203 &olen,
204 buf, NULL, 42 ) );
205
206 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
207 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
Thomas Daubney11425342021-05-18 11:57:05 +0100208 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500209 0, sizeof( buf ), buf,
210 buf ) );
211 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
212 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Thomas Daubney11425342021-05-18 11:57:05 +0100213 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214 0, sizeof( buf ), NULL,
215 buf ) );
216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
217 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Thomas Daubney11425342021-05-18 11:57:05 +0100218 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500219 0, sizeof( buf ), buf,
220 NULL ) );
221 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
222 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Thomas Daubney11425342021-05-18 11:57:05 +0100223 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500224 MBEDTLS_MD_SHA1,
225 0, NULL,
226 buf ) );
227
228 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
229 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
Thomas Daubneyb9eaa732021-05-18 15:42:16 +0100230 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500231 0, sizeof( buf ), buf,
232 buf ) );
233 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
234 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
Thomas Daubneyb9eaa732021-05-18 15:42:16 +0100235 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500236 0, sizeof( buf ), NULL,
237 buf ) );
238 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
239 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
Thomas Daubneyb9eaa732021-05-18 15:42:16 +0100240 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500241 0, sizeof( buf ), buf,
242 NULL ) );
243 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
244 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
Thomas Daubneyb9eaa732021-05-18 15:42:16 +0100245 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500246 MBEDTLS_MD_SHA1,
247 0, NULL,
248 buf ) );
249
250 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
251 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
Thomas Daubney9a66d5c2021-05-18 15:50:21 +0100252 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500253 0, sizeof( buf ), buf,
254 buf ) );
255 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
256 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
Thomas Daubney9a66d5c2021-05-18 15:50:21 +0100257 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500258 0, sizeof( buf ), NULL,
259 buf ) );
260 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
261 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
Thomas Daubney9a66d5c2021-05-18 15:50:21 +0100262 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500263 0, sizeof( buf ), buf,
264 NULL ) );
265 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
266 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
Thomas Daubney9a66d5c2021-05-18 15:50:21 +0100267 MBEDTLS_RSA_PRIVATE,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500268 MBEDTLS_MD_SHA1,
269 0, NULL,
270 buf ) );
271
272 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Cédric Meutera05cbec2020-04-25 15:02:34 +0200273 mbedtls_rsa_rsassa_pss_sign_ext( NULL, NULL, NULL,
274 0, sizeof( buf ), buf,
275 MBEDTLS_RSA_SALT_LEN_ANY,
276 buf ) );
277 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
278 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
279 0, sizeof( buf ), NULL,
280 MBEDTLS_RSA_SALT_LEN_ANY,
281 buf ) );
282 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
283 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
284 0, sizeof( buf ), buf,
285 MBEDTLS_RSA_SALT_LEN_ANY,
286 NULL ) );
287 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
288 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
289 MBEDTLS_MD_SHA1,
290 0, NULL,
291 MBEDTLS_RSA_SALT_LEN_ANY,
292 buf ) );
293
294 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500295 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
296 valid_mode,
297 0, sizeof( buf ), buf,
298 buf ) );
299 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
300 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
301 invalid_mode,
302 0, sizeof( buf ), buf,
303 buf ) );
304 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
305 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
306 valid_mode,
307 0, sizeof( buf ), NULL,
308 buf ) );
309 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
310 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
311 valid_mode,
312 0, sizeof( buf ), buf,
313 NULL ) );
314 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
315 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
316 valid_mode,
317 MBEDTLS_MD_SHA1, 0, NULL,
318 buf ) );
319
320 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
321 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
322 NULL,
323 valid_mode,
324 0, sizeof( buf ), buf,
325 buf ) );
326 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
327 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
328 NULL,
329 invalid_mode,
330 0, sizeof( buf ), buf,
331 buf ) );
332 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
333 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
334 NULL,
335 valid_mode,
336 0, sizeof( buf ),
337 NULL, buf ) );
338 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
339 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
340 NULL,
341 valid_mode,
342 0, sizeof( buf ), buf,
343 NULL ) );
344 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
345 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
346 NULL,
347 valid_mode,
348 MBEDTLS_MD_SHA1,
349 0, NULL,
350 buf ) );
351
352 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
353 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
354 valid_mode,
355 0, sizeof( buf ),
356 buf, buf ) );
357 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
358 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
359 invalid_mode,
360 0, sizeof( buf ),
361 buf, buf ) );
362 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
363 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
364 valid_mode,
365 0, sizeof( buf ),
366 NULL, buf ) );
367 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
368 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
369 valid_mode,
370 0, sizeof( buf ),
371 buf, NULL ) );
372 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
373 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
374 valid_mode,
375 MBEDTLS_MD_SHA1,
376 0, NULL,
377 buf ) );
378
379 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
380 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
381 valid_mode,
382 0, sizeof( buf ),
383 buf,
384 0, 0,
385 buf ) );
386 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
387 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
388 invalid_mode,
389 0, sizeof( buf ),
390 buf,
391 0, 0,
392 buf ) );
393 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
394 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
395 valid_mode,
396 0, sizeof( buf ),
397 NULL, 0, 0,
398 buf ) );
399 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
400 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
401 valid_mode,
402 0, sizeof( buf ),
403 buf, 0, 0,
404 NULL ) );
405 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
406 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
407 valid_mode,
408 MBEDTLS_MD_SHA1,
409 0, NULL,
410 0, 0,
411 buf ) );
412
413 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
414 mbedtls_rsa_copy( NULL, &ctx ) );
415 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
416 mbedtls_rsa_copy( &ctx, NULL ) );
417
418exit:
419 return;
420}
421/* END_CASE */
422
Paul Bakker33b43f12013-08-20 11:48:36 +0200423/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100424void rsa_init_free( int reinit )
425{
426 mbedtls_rsa_context ctx;
427
428 /* Double free is not explicitly documented to work, but we rely on it
429 * even inside the library so that you can call mbedtls_rsa_free()
430 * unconditionally on an error path without checking whether it has
431 * already been called in the success path. */
432
433 mbedtls_rsa_init( &ctx, 0, 0 );
434 mbedtls_rsa_free( &ctx );
435
436 if( reinit )
437 mbedtls_rsa_init( &ctx, 0, 0 );
438 mbedtls_rsa_free( &ctx );
439
440 /* This test case always succeeds, functionally speaking. A plausible
441 * bug might trigger an invalid pointer dereference or a memory leak. */
442 goto exit;
443}
444/* END_CASE */
445
446/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100447void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100448 int digest, int mod, int radix_P, char * input_P,
449 int radix_Q, char * input_Q, int radix_N,
450 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200451 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000452{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200453 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
454 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100456 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200457 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000458
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100459 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
460 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000462
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200463 memset( hash_result, 0x00, sizeof( hash_result ) );
464 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200465 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000466
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100467 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
468 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
469 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
470 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000471
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100472 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
473 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100474 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000476
Paul Bakker42a29bf2009-07-07 20:18:41 +0000477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100479 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 +0000480
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200481 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
482 &rnd_info, MBEDTLS_RSA_PRIVATE, digest,
483 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200484 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000485 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000486
Ronald Cronac6ae352020-06-26 14:33:03 +0200487 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
488 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000489 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000490
Paul Bakkerbd51b262014-07-10 15:26:12 +0200491exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100492 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
493 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000495}
Paul Bakker33b43f12013-08-20 11:48:36 +0200496/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000497
Paul Bakker33b43f12013-08-20 11:48:36 +0200498/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100499void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100500 int digest, int mod, int radix_N,
501 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100502 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000503{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200504 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000506
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100507 mbedtls_mpi N, E;
508
509 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200511 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000512
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100513 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
514 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
515 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
516 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000518
Paul Bakker42a29bf2009-07-07 20:18:41 +0000519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100521 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 +0000522
Azim Khand30ca132017-06-09 04:32:58 +0100523 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str->x ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100524
Paul Bakkerbd51b262014-07-10 15:26:12 +0200525exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100526 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000528}
Paul Bakker33b43f12013-08-20 11:48:36 +0200529/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000530
Paul Bakker821fb082009-07-12 13:26:42 +0000531
Paul Bakker33b43f12013-08-20 11:48:36 +0200532/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100533void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100534 int padding_mode, int mod, int radix_P,
535 char * input_P, int radix_Q, char * input_Q,
536 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200537 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000538{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200539 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100541 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200542 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100545 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
546 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000547
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200548 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200549 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000550
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100551 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
552 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
553 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
554 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000555
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100556 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
557 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100558 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000560
Paul Bakker821fb082009-07-12 13:26:42 +0000561
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200562 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
563 &rnd_info, MBEDTLS_RSA_PRIVATE,
564 MBEDTLS_MD_NONE, hash_result->len,
565 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000566
Paul Bakker821fb082009-07-12 13:26:42 +0000567
Ronald Cronac6ae352020-06-26 14:33:03 +0200568 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
569 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000570
Paul Bakkerbd51b262014-07-10 15:26:12 +0200571exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100572 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
573 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000576}
Paul Bakker33b43f12013-08-20 11:48:36 +0200577/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000578
Paul Bakker33b43f12013-08-20 11:48:36 +0200579/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100580void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200581 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100582 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100583 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000584{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200585 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000587
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100588 mbedtls_mpi N, E;
589 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100592 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000593
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100594 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
595 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000596
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100597 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
598 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000600
Paul Bakker821fb082009-07-12 13:26:42 +0000601
Azim Khand30ca132017-06-09 04:32:58 +0100602 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE, hash_result->len, hash_result->x, result_str->x ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100603
Paul Bakkerbd51b262014-07-10 15:26:12 +0200604exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100605 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000607}
Paul Bakker33b43f12013-08-20 11:48:36 +0200608/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000609
Paul Bakker33b43f12013-08-20 11:48:36 +0200610/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100611void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100612 int mod, int radix_N, char * input_N,
613 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200614 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000615{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200616 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200618 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000619
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100620 mbedtls_mpi N, E;
621 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
622
Ronald Cron351f0ee2020-06-10 12:12:18 +0200623 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200626 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000627
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100628 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
629 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000630
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100631 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
632 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000634
Paul Bakker42a29bf2009-07-07 20:18:41 +0000635
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200636 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
637 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100638 &rnd_info, message_str->len,
639 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200640 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200641 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000642 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000643
Ronald Cronac6ae352020-06-26 14:33:03 +0200644 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
645 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000646 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100647
Paul Bakkerbd51b262014-07-10 15:26:12 +0200648exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100649 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000651}
Paul Bakker33b43f12013-08-20 11:48:36 +0200652/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000653
Paul Bakker33b43f12013-08-20 11:48:36 +0200654/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100655void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100656 int mod, int radix_N, char * input_N,
657 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200658 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000659{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200660 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000662
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100663 mbedtls_mpi N, E;
664
665 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200667 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000668
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100669 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
670 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000671
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100672 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
673 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000675
Paul Bakkera6656852010-07-18 19:47:14 +0000676
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200677 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100678 NULL, message_str->len,
679 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200680 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200681 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000682 {
Paul Bakkera6656852010-07-18 19:47:14 +0000683
Ronald Cronac6ae352020-06-26 14:33:03 +0200684 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
685 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000686 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100687
Paul Bakkerbd51b262014-07-10 15:26:12 +0200688exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100689 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000691}
Paul Bakker33b43f12013-08-20 11:48:36 +0200692/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000693
Paul Bakker33b43f12013-08-20 11:48:36 +0200694/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100695void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100696 int mod, int radix_P, char * input_P,
697 int radix_Q, char * input_Q, int radix_N,
698 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200699 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100700 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000701{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200702 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000704 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200705 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100706 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000707
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100708 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
709 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000712
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200713 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200714 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000715
Paul Bakker42a29bf2009-07-07 20:18:41 +0000716
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100717 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
718 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
719 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
720 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000721
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100722 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
723 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100724 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000726
Paul Bakker69998dd2009-07-11 19:15:20 +0000727 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000728
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200729 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100730 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200731 &output_len, message_str->x, output,
732 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200733 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000734 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000735
Ronald Cronac6ae352020-06-26 14:33:03 +0200736 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200737 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200738 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000739 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000740
Paul Bakkerbd51b262014-07-10 15:26:12 +0200741exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100742 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
743 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000745}
Paul Bakker33b43f12013-08-20 11:48:36 +0200746/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000747
Paul Bakker33b43f12013-08-20 11:48:36 +0200748/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100749void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100750 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200751 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000752{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200753 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000755
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100756 mbedtls_mpi N, E;
757
758 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
760 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200761 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000762
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100763 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
764 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000765
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100766 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
767 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000769
Paul Bakker821fb082009-07-12 13:26:42 +0000770
Azim Khand30ca132017-06-09 04:32:58 +0100771 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200772 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000773 {
Paul Bakker821fb082009-07-12 13:26:42 +0000774
Ronald Cronac6ae352020-06-26 14:33:03 +0200775 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
776 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000777 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100778
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100779 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200781 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100785
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200786 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100787 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100788 if( result == 0 )
789 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100790
Ronald Cronac6ae352020-06-26 14:33:03 +0200791 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
792 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100793 }
794
Paul Bakkerbd51b262014-07-10 15:26:12 +0200795exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100796 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_rsa_free( &ctx );
798 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000799}
Paul Bakker33b43f12013-08-20 11:48:36 +0200800/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000801
Paul Bakker33b43f12013-08-20 11:48:36 +0200802/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100803void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100804 char * input_P, int radix_Q, char * input_Q,
805 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200806 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100807 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000808{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200809 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100811 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200812 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200813 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000814
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100815 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
816 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
818 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000819
Ronald Cron351f0ee2020-06-10 12:12:18 +0200820 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000821
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100822 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
823 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
824 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
825 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000826
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100827 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
828 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100829 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000831
Paul Bakker821fb082009-07-12 13:26:42 +0000832
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200833 /* repeat three times to test updating of blinding values */
834 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000835 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200836 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200837 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
838 &rnd_info, message_str->x,
839 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200840 if( result == 0 )
841 {
Paul Bakker821fb082009-07-12 13:26:42 +0000842
Ronald Cronac6ae352020-06-26 14:33:03 +0200843 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200844 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200845 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200846 }
Paul Bakker821fb082009-07-12 13:26:42 +0000847 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000848
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100849 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200851 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100855
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200856 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200857 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
858 &rnd_info, message_str->x,
859 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100860 if( result == 0 )
861 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100862
Ronald Cronac6ae352020-06-26 14:33:03 +0200863 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200864 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200865 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100866 }
867
Paul Bakkerbd51b262014-07-10 15:26:12 +0200868exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100869 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
870 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000873}
Paul Bakker33b43f12013-08-20 11:48:36 +0200874/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000875
Paul Bakker33b43f12013-08-20 11:48:36 +0200876/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100877void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000878{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_rsa_context ctx;
880 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000883}
Paul Bakker33b43f12013-08-20 11:48:36 +0200884/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000885
Paul Bakker33b43f12013-08-20 11:48:36 +0200886/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100887void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
888 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100891 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000892
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100893 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000895
Paul Bakker33b43f12013-08-20 11:48:36 +0200896 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000897 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100898 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000899 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200900 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000901 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100902 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000903 }
904
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100905 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100907
Paul Bakkerbd51b262014-07-10 15:26:12 +0200908exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100909 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000911}
Paul Bakker33b43f12013-08-20 11:48:36 +0200912/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000913
Paul Bakker33b43f12013-08-20 11:48:36 +0200914/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100915void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
916 int radix_Q, char * input_Q, int radix_N,
917 char * input_N, int radix_E, char * input_E,
918 int radix_D, char * input_D, int radix_DP,
919 char * input_DP, int radix_DQ,
920 char * input_DQ, int radix_QP,
921 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000922{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000926
Paul Bakker33b43f12013-08-20 11:48:36 +0200927 ctx.len = mod / 8;
928 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000931 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200932 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000935 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200936 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000939 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200940 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000943 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200944 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000947 }
Hanno Becker131134f2017-08-23 08:31:07 +0100948#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200949 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000952 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200953 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000956 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200957 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000960 }
Hanno Becker131134f2017-08-23 08:31:07 +0100961#else
962 ((void) radix_DP); ((void) input_DP);
963 ((void) radix_DQ); ((void) input_DQ);
964 ((void) radix_QP); ((void) input_QP);
965#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100968
Paul Bakkerbd51b262014-07-10 15:26:12 +0200969exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000971}
Paul Bakker33b43f12013-08-20 11:48:36 +0200972/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000973
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100974/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100975void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
976 int radix_Epub, char * input_Epub, int radix_P,
977 char * input_P, int radix_Q, char * input_Q,
978 int radix_N, char * input_N, int radix_E,
979 char * input_E, int radix_D, char * input_D,
980 int radix_DP, char * input_DP, int radix_DQ,
981 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100982 int result )
983{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
987 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100988
989 pub.len = mod / 8;
990 prv.len = mod / 8;
991
992 if( strlen( input_Npub ) )
993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100995 }
996 if( strlen( input_Epub ) )
997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100999 }
1000
1001 if( strlen( input_P ) )
1002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001004 }
1005 if( strlen( input_Q ) )
1006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001008 }
1009 if( strlen( input_N ) )
1010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001012 }
1013 if( strlen( input_E ) )
1014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001016 }
1017 if( strlen( input_D ) )
1018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001020 }
Hanno Becker131134f2017-08-23 08:31:07 +01001021#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001022 if( strlen( input_DP ) )
1023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001025 }
1026 if( strlen( input_DQ ) )
1027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001029 }
1030 if( strlen( input_QP ) )
1031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001033 }
Hanno Becker131134f2017-08-23 08:31:07 +01001034#else
1035 ((void) radix_DP); ((void) input_DP);
1036 ((void) radix_DQ); ((void) input_DQ);
1037 ((void) radix_QP); ((void) input_QP);
1038#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001041
1042exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 mbedtls_rsa_free( &pub );
1044 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001045}
1046/* END_CASE */
1047
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001048/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_rsa_context ctx;
1052 mbedtls_entropy_context entropy;
1053 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001054 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001055
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001056 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001058 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001059
Hanno Beckera47023e2017-12-22 17:08:03 +00001060 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1061 &entropy, (const unsigned char *) pers,
1062 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001065 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001068 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001069 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001070
Paul Bakkerbd51b262014-07-10 15:26:12 +02001071exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 mbedtls_rsa_free( &ctx );
1073 mbedtls_ctr_drbg_free( &ctr_drbg );
1074 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001075}
Paul Bakker33b43f12013-08-20 11:48:36 +02001076/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001077
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001078/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001079void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001080 int radix_D, char *input_D,
1081 int radix_E, char *input_E,
1082 int radix_P, char *output_P,
1083 int radix_Q, char *output_Q,
1084 int corrupt, int result )
1085{
1086 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1087
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001088 mbedtls_mpi_init( &N );
1089 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1090 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1091 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1092
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001093 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1094 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1095 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1096 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1097 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1098
1099 if( corrupt )
1100 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1101
1102 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001103 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001104
1105 if( !corrupt )
1106 {
1107 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1108 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1109 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1110 }
1111
1112exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001113 mbedtls_mpi_free( &N );
1114 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1115 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1116 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001117}
1118/* END_CASE */
1119
Hanno Becker6b4ce492017-08-23 11:00:21 +01001120/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001121void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1122 int radix_Q, char *input_Q,
1123 int radix_E, char *input_E,
1124 int radix_D, char *output_D,
1125 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001126{
1127 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1128
1129 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1130 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1131 mbedtls_mpi_init( &E );
1132 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1133
1134 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1135 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1136 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1137 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1138
1139 if( corrupt )
1140 {
1141 /* Make E even */
1142 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1143 }
1144
1145 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001146 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1147 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001148
1149 if( !corrupt )
1150 {
1151 /*
1152 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1153 */
1154
1155 /* Replace P,Q by P-1, Q-1 */
1156 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1157 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1158
1159 /* Check D == Dp modulo P-1 */
1160 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1161 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1162 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1163
1164 /* Check D == Dp modulo Q-1 */
1165 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1166 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1167 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1168 }
1169
1170exit:
1171
1172 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1173 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1174 mbedtls_mpi_free( &E );
1175 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1176}
1177/* END_CASE */
1178
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001179/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001180void mbedtls_rsa_import( int radix_N, char *input_N,
1181 int radix_P, char *input_P,
1182 int radix_Q, char *input_Q,
1183 int radix_D, char *input_D,
1184 int radix_E, char *input_E,
1185 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001186 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001187 int res_check,
1188 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001189{
1190 mbedtls_mpi N, P, Q, D, E;
1191 mbedtls_rsa_context ctx;
1192
Hanno Beckere1582a82017-09-29 11:51:05 +01001193 /* Buffers used for encryption-decryption test */
1194 unsigned char *buf_orig = NULL;
1195 unsigned char *buf_enc = NULL;
1196 unsigned char *buf_dec = NULL;
1197
Hanno Beckerc77ab892017-08-23 11:01:06 +01001198 mbedtls_entropy_context entropy;
1199 mbedtls_ctr_drbg_context ctr_drbg;
1200 const char *pers = "test_suite_rsa";
1201
Hanno Becker4d6e8342017-09-29 11:50:18 +01001202 const int have_N = ( strlen( input_N ) > 0 );
1203 const int have_P = ( strlen( input_P ) > 0 );
1204 const int have_Q = ( strlen( input_Q ) > 0 );
1205 const int have_D = ( strlen( input_D ) > 0 );
1206 const int have_E = ( strlen( input_E ) > 0 );
1207
Hanno Beckerc77ab892017-08-23 11:01:06 +01001208 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001209 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001210 mbedtls_rsa_init( &ctx, 0, 0 );
1211
1212 mbedtls_mpi_init( &N );
1213 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1214 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1215
Hanno Beckerd4d60572018-01-10 07:12:01 +00001216 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1217 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1218
Hanno Becker4d6e8342017-09-29 11:50:18 +01001219 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001220 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1221
Hanno Becker4d6e8342017-09-29 11:50:18 +01001222 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001223 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1224
Hanno Becker4d6e8342017-09-29 11:50:18 +01001225 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001226 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1227
Hanno Becker4d6e8342017-09-29 11:50:18 +01001228 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001229 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1230
Hanno Becker4d6e8342017-09-29 11:50:18 +01001231 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001232 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1233
1234 if( !successive )
1235 {
1236 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001237 have_N ? &N : NULL,
1238 have_P ? &P : NULL,
1239 have_Q ? &Q : NULL,
1240 have_D ? &D : NULL,
1241 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001242 }
1243 else
1244 {
1245 /* Import N, P, Q, D, E separately.
1246 * This should make no functional difference. */
1247
1248 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001249 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001250 NULL, NULL, NULL, NULL ) == 0 );
1251
1252 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1253 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001254 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001255 NULL, NULL, NULL ) == 0 );
1256
1257 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1258 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001259 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001260 NULL, NULL ) == 0 );
1261
1262 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1263 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001264 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001265 NULL ) == 0 );
1266
1267 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1268 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001269 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001270 }
1271
Hanno Becker04877a42017-10-11 10:01:33 +01001272 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001273
Hanno Beckere1582a82017-09-29 11:51:05 +01001274 /* On expected success, perform some public and private
1275 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001276 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001277 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001278 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001279 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1280 else
1281 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1282
1283 if( res_check != 0 )
1284 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001285
1286 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1287 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1288 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1289 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1290 goto exit;
1291
1292 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1293 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1294
1295 /* Make sure the number we're generating is smaller than the modulus */
1296 buf_orig[0] = 0x00;
1297
1298 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1299
1300 if( is_priv )
1301 {
1302 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1303 &ctr_drbg, buf_enc,
1304 buf_dec ) == 0 );
1305
1306 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1307 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1308 }
1309 }
1310
Hanno Beckerc77ab892017-08-23 11:01:06 +01001311exit:
1312
Hanno Beckere1582a82017-09-29 11:51:05 +01001313 mbedtls_free( buf_orig );
1314 mbedtls_free( buf_enc );
1315 mbedtls_free( buf_dec );
1316
Hanno Beckerc77ab892017-08-23 11:01:06 +01001317 mbedtls_rsa_free( &ctx );
1318
1319 mbedtls_ctr_drbg_free( &ctr_drbg );
1320 mbedtls_entropy_free( &entropy );
1321
1322 mbedtls_mpi_free( &N );
1323 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1324 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1325}
1326/* END_CASE */
1327
Hanno Becker417f2d62017-08-23 11:44:51 +01001328/* BEGIN_CASE */
1329void mbedtls_rsa_export( int radix_N, char *input_N,
1330 int radix_P, char *input_P,
1331 int radix_Q, char *input_Q,
1332 int radix_D, char *input_D,
1333 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001334 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001335 int successive )
1336{
1337 /* Original MPI's with which we set up the RSA context */
1338 mbedtls_mpi N, P, Q, D, E;
1339
1340 /* Exported MPI's */
1341 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1342
1343 const int have_N = ( strlen( input_N ) > 0 );
1344 const int have_P = ( strlen( input_P ) > 0 );
1345 const int have_Q = ( strlen( input_Q ) > 0 );
1346 const int have_D = ( strlen( input_D ) > 0 );
1347 const int have_E = ( strlen( input_E ) > 0 );
1348
Hanno Becker417f2d62017-08-23 11:44:51 +01001349 mbedtls_rsa_context ctx;
1350
1351 mbedtls_rsa_init( &ctx, 0, 0 );
1352
1353 mbedtls_mpi_init( &N );
1354 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1355 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1356
1357 mbedtls_mpi_init( &Ne );
1358 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1359 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1360
1361 /* Setup RSA context */
1362
1363 if( have_N )
1364 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1365
1366 if( have_P )
1367 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1368
1369 if( have_Q )
1370 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1371
1372 if( have_D )
1373 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1374
1375 if( have_E )
1376 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1377
1378 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1379 strlen( input_N ) ? &N : NULL,
1380 strlen( input_P ) ? &P : NULL,
1381 strlen( input_Q ) ? &Q : NULL,
1382 strlen( input_D ) ? &D : NULL,
1383 strlen( input_E ) ? &E : NULL ) == 0 );
1384
Hanno Becker7f25f852017-10-10 16:56:22 +01001385 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001386
1387 /*
1388 * Export parameters and compare to original ones.
1389 */
1390
1391 /* N and E must always be present. */
1392 if( !successive )
1393 {
1394 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1395 }
1396 else
1397 {
1398 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1399 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1400 }
1401 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1402 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1403
1404 /* If we were providing enough information to setup a complete private context,
1405 * we expect to be able to export all core parameters. */
1406
1407 if( is_priv )
1408 {
1409 if( !successive )
1410 {
1411 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1412 &De, NULL ) == 0 );
1413 }
1414 else
1415 {
1416 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1417 NULL, NULL ) == 0 );
1418 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1419 NULL, NULL ) == 0 );
1420 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1421 &De, NULL ) == 0 );
1422 }
1423
1424 if( have_P )
1425 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1426
1427 if( have_Q )
1428 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1429
1430 if( have_D )
1431 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1432
1433 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001434 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1435 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001436 }
1437
1438exit:
1439
1440 mbedtls_rsa_free( &ctx );
1441
1442 mbedtls_mpi_free( &N );
1443 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1444 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1445
1446 mbedtls_mpi_free( &Ne );
1447 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1448 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1449}
1450/* END_CASE */
1451
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001452/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001453void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1454 int radix_P, char *input_P,
1455 int radix_Q, char *input_Q,
1456 int radix_D, char *input_D,
1457 int radix_E, char *input_E,
1458 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001459{
1460 /* Original MPI's with which we set up the RSA context */
1461 mbedtls_mpi N, P, Q, D, E;
1462
1463 const int have_N = ( strlen( input_N ) > 0 );
1464 const int have_P = ( strlen( input_P ) > 0 );
1465 const int have_Q = ( strlen( input_Q ) > 0 );
1466 const int have_D = ( strlen( input_D ) > 0 );
1467 const int have_E = ( strlen( input_E ) > 0 );
1468
1469 mbedtls_entropy_context entropy;
1470 mbedtls_ctr_drbg_context ctr_drbg;
1471 const char *pers = "test_suite_rsa";
1472
1473 mbedtls_mpi_init( &N );
1474 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1475 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1476
1477 mbedtls_ctr_drbg_init( &ctr_drbg );
1478 mbedtls_entropy_init( &entropy );
1479 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1480 &entropy, (const unsigned char *) pers,
1481 strlen( pers ) ) == 0 );
1482
1483 if( have_N )
1484 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1485
1486 if( have_P )
1487 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1488
1489 if( have_Q )
1490 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1491
1492 if( have_D )
1493 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1494
1495 if( have_E )
1496 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1497
Hanno Becker750e8b42017-08-25 07:54:27 +01001498 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1499 have_P ? &P : NULL,
1500 have_Q ? &Q : NULL,
1501 have_D ? &D : NULL,
1502 have_E ? &E : NULL,
1503 prng ? mbedtls_ctr_drbg_random : NULL,
1504 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001505exit:
1506
1507 mbedtls_ctr_drbg_free( &ctr_drbg );
1508 mbedtls_entropy_free( &entropy );
1509
1510 mbedtls_mpi_free( &N );
1511 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1512 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1513}
1514/* END_CASE */
1515
Hanno Beckerc77ab892017-08-23 11:01:06 +01001516/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001517void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1518 data_t *input_Q, data_t *input_D,
1519 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001520 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001521{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001522 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001523 unsigned char bufNe[256];
1524 unsigned char bufPe[128];
1525 unsigned char bufQe[128];
1526 unsigned char bufDe[256];
1527 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001528
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001529 mbedtls_rsa_context ctx;
1530
1531 mbedtls_rsa_init( &ctx, 0, 0 );
1532
1533 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001534 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001535 input_N->len ? input_N->x : NULL, input_N->len,
1536 input_P->len ? input_P->x : NULL, input_P->len,
1537 input_Q->len ? input_Q->x : NULL, input_Q->len,
1538 input_D->len ? input_D->x : NULL, input_D->len,
1539 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001540
Hanno Becker7f25f852017-10-10 16:56:22 +01001541 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001542
1543 /*
1544 * Export parameters and compare to original ones.
1545 */
1546
1547 /* N and E must always be present. */
1548 if( !successive )
1549 {
Azim Khand30ca132017-06-09 04:32:58 +01001550 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001551 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001552 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001553 }
1554 else
1555 {
Azim Khand30ca132017-06-09 04:32:58 +01001556 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001557 NULL, 0, NULL, 0, NULL, 0,
1558 NULL, 0 ) == 0 );
1559 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1560 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001561 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001562 }
Azim Khand30ca132017-06-09 04:32:58 +01001563 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1564 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001565
1566 /* If we were providing enough information to setup a complete private context,
1567 * we expect to be able to export all core parameters. */
1568
1569 if( is_priv )
1570 {
1571 if( !successive )
1572 {
1573 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001574 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1575 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1576 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001577 NULL, 0 ) == 0 );
1578 }
1579 else
1580 {
1581 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001582 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001583 NULL, 0, NULL, 0,
1584 NULL, 0 ) == 0 );
1585
1586 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001587 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001588 NULL, 0, NULL, 0 ) == 0 );
1589
Azim Khand30ca132017-06-09 04:32:58 +01001590 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1591 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001592 NULL, 0 ) == 0 );
1593 }
1594
Azim Khand30ca132017-06-09 04:32:58 +01001595 if( input_P->len )
1596 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001597
Azim Khand30ca132017-06-09 04:32:58 +01001598 if( input_Q->len )
1599 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001600
Azim Khand30ca132017-06-09 04:32:58 +01001601 if( input_D->len )
1602 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001603
1604 }
1605
1606exit:
1607 mbedtls_rsa_free( &ctx );
1608}
1609/* END_CASE */
1610
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001611/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001612void mbedtls_rsa_import_raw( data_t *input_N,
1613 data_t *input_P, data_t *input_Q,
1614 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001615 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001616 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001617 int res_check,
1618 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001619{
Hanno Beckere1582a82017-09-29 11:51:05 +01001620 /* Buffers used for encryption-decryption test */
1621 unsigned char *buf_orig = NULL;
1622 unsigned char *buf_enc = NULL;
1623 unsigned char *buf_dec = NULL;
1624
Hanno Beckerc77ab892017-08-23 11:01:06 +01001625 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001626 mbedtls_entropy_context entropy;
1627 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001628
Hanno Beckerc77ab892017-08-23 11:01:06 +01001629 const char *pers = "test_suite_rsa";
1630
1631 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001632 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001633 mbedtls_rsa_init( &ctx, 0, 0 );
1634
Hanno Beckerc77ab892017-08-23 11:01:06 +01001635 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1636 &entropy, (const unsigned char *) pers,
1637 strlen( pers ) ) == 0 );
1638
Hanno Beckerc77ab892017-08-23 11:01:06 +01001639 if( !successive )
1640 {
1641 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001642 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1643 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1644 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1645 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1646 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001647 }
1648 else
1649 {
1650 /* Import N, P, Q, D, E separately.
1651 * This should make no functional difference. */
1652
1653 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001654 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001655 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1656
1657 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1658 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001659 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001660 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1661
1662 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1663 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001664 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001665 NULL, 0, NULL, 0 ) == 0 );
1666
1667 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1668 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001669 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001670 NULL, 0 ) == 0 );
1671
1672 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1673 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001674 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001675 }
1676
Hanno Becker04877a42017-10-11 10:01:33 +01001677 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001678
Hanno Beckere1582a82017-09-29 11:51:05 +01001679 /* On expected success, perform some public and private
1680 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001681 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001682 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001683 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001684 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1685 else
1686 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1687
1688 if( res_check != 0 )
1689 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001690
1691 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1692 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1693 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1694 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1695 goto exit;
1696
1697 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1698 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1699
1700 /* Make sure the number we're generating is smaller than the modulus */
1701 buf_orig[0] = 0x00;
1702
1703 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1704
1705 if( is_priv )
1706 {
1707 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1708 &ctr_drbg, buf_enc,
1709 buf_dec ) == 0 );
1710
1711 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1712 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1713 }
1714 }
1715
Hanno Beckerc77ab892017-08-23 11:01:06 +01001716exit:
1717
Hanno Becker3f3ae852017-10-02 10:08:39 +01001718 mbedtls_free( buf_orig );
1719 mbedtls_free( buf_enc );
1720 mbedtls_free( buf_dec );
1721
Hanno Beckerc77ab892017-08-23 11:01:06 +01001722 mbedtls_rsa_free( &ctx );
1723
1724 mbedtls_ctr_drbg_free( &ctr_drbg );
1725 mbedtls_entropy_free( &entropy );
1726
1727}
1728/* END_CASE */
1729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001731void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001732{
Andres AG93012e82016-09-09 09:10:28 +01001733 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001734}
Paul Bakker33b43f12013-08-20 11:48:36 +02001735/* END_CASE */