blob: 814385baa4d456cc36ca8c84e0989264035b3a01 [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,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500208 0, sizeof( buf ), buf,
209 buf ) );
210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
211 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 0, sizeof( buf ), NULL,
213 buf ) );
214 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
215 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216 0, sizeof( buf ), buf,
217 NULL ) );
218 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
219 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500220 MBEDTLS_MD_SHA1,
221 0, NULL,
222 buf ) );
223
224 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
225 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500226 0, sizeof( buf ), buf,
227 buf ) );
228 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
229 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500230 0, sizeof( buf ), NULL,
231 buf ) );
232 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
233 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500234 0, sizeof( buf ), buf,
235 NULL ) );
236 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
237 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500238 MBEDTLS_MD_SHA1,
239 0, NULL,
240 buf ) );
241
242 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
243 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500244 0, sizeof( buf ), buf,
245 buf ) );
246 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
247 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500248 0, sizeof( buf ), NULL,
249 buf ) );
250 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
251 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500252 0, sizeof( buf ), buf,
253 NULL ) );
254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
255 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500256 MBEDTLS_MD_SHA1,
257 0, NULL,
258 buf ) );
259
260 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Cédric Meutera05cbec2020-04-25 15:02:34 +0200261 mbedtls_rsa_rsassa_pss_sign_ext( NULL, NULL, NULL,
262 0, sizeof( buf ), buf,
263 MBEDTLS_RSA_SALT_LEN_ANY,
264 buf ) );
265 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
266 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
267 0, sizeof( buf ), NULL,
268 MBEDTLS_RSA_SALT_LEN_ANY,
269 buf ) );
270 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
271 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
272 0, sizeof( buf ), buf,
273 MBEDTLS_RSA_SALT_LEN_ANY,
274 NULL ) );
275 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
276 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
277 MBEDTLS_MD_SHA1,
278 0, NULL,
279 MBEDTLS_RSA_SALT_LEN_ANY,
280 buf ) );
281
282 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500283 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
284 valid_mode,
285 0, sizeof( buf ), buf,
286 buf ) );
287 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
288 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
289 invalid_mode,
290 0, sizeof( buf ), buf,
291 buf ) );
292 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
293 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
294 valid_mode,
295 0, sizeof( buf ), NULL,
296 buf ) );
297 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
298 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
299 valid_mode,
300 0, sizeof( buf ), buf,
301 NULL ) );
302 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
303 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
304 valid_mode,
305 MBEDTLS_MD_SHA1, 0, NULL,
306 buf ) );
307
308 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
309 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
310 NULL,
311 valid_mode,
312 0, sizeof( buf ), buf,
313 buf ) );
314 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
315 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
316 NULL,
317 invalid_mode,
318 0, sizeof( buf ), buf,
319 buf ) );
320 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
321 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
322 NULL,
323 valid_mode,
324 0, sizeof( buf ),
325 NULL, buf ) );
326 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
327 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
328 NULL,
329 valid_mode,
330 0, sizeof( buf ), buf,
331 NULL ) );
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 MBEDTLS_MD_SHA1,
337 0, NULL,
338 buf ) );
339
340 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
341 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
342 valid_mode,
343 0, sizeof( buf ),
344 buf, buf ) );
345 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
346 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
347 invalid_mode,
348 0, sizeof( buf ),
349 buf, buf ) );
350 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
351 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
352 valid_mode,
353 0, sizeof( buf ),
354 NULL, buf ) );
355 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
356 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
357 valid_mode,
358 0, sizeof( buf ),
359 buf, NULL ) );
360 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
361 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
362 valid_mode,
363 MBEDTLS_MD_SHA1,
364 0, NULL,
365 buf ) );
366
367 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
368 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
369 valid_mode,
370 0, sizeof( buf ),
371 buf,
372 0, 0,
373 buf ) );
374 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
375 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
376 invalid_mode,
377 0, sizeof( buf ),
378 buf,
379 0, 0,
380 buf ) );
381 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
382 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
383 valid_mode,
384 0, sizeof( buf ),
385 NULL, 0, 0,
386 buf ) );
387 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
388 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
389 valid_mode,
390 0, sizeof( buf ),
391 buf, 0, 0,
392 NULL ) );
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 MBEDTLS_MD_SHA1,
397 0, NULL,
398 0, 0,
399 buf ) );
400
401 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
402 mbedtls_rsa_copy( NULL, &ctx ) );
403 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
404 mbedtls_rsa_copy( &ctx, NULL ) );
405
406exit:
407 return;
408}
409/* END_CASE */
410
Paul Bakker33b43f12013-08-20 11:48:36 +0200411/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100412void rsa_init_free( int reinit )
413{
414 mbedtls_rsa_context ctx;
415
416 /* Double free is not explicitly documented to work, but we rely on it
417 * even inside the library so that you can call mbedtls_rsa_free()
418 * unconditionally on an error path without checking whether it has
419 * already been called in the success path. */
420
421 mbedtls_rsa_init( &ctx, 0, 0 );
422 mbedtls_rsa_free( &ctx );
423
424 if( reinit )
425 mbedtls_rsa_init( &ctx, 0, 0 );
426 mbedtls_rsa_free( &ctx );
427
428 /* This test case always succeeds, functionally speaking. A plausible
429 * bug might trigger an invalid pointer dereference or a memory leak. */
430 goto exit;
431}
432/* END_CASE */
433
434/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100435void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100436 int digest, int mod, int radix_P, char * input_P,
437 int radix_Q, char * input_Q, int radix_N,
438 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200439 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000440{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200441 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
442 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100444 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200445 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000446
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100447 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
448 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000450
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200451 memset( hash_result, 0x00, sizeof( hash_result ) );
452 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200453 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000454
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100455 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
456 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
457 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
458 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000459
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100460 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
461 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100462 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000464
Paul Bakker42a29bf2009-07-07 20:18:41 +0000465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100467 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 +0000468
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200469 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100470 &rnd_info, digest, 0, hash_result,
471 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200472 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000473 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000474
Ronald Cronac6ae352020-06-26 14:33:03 +0200475 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
476 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000477 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000478
Paul Bakkerbd51b262014-07-10 15:26:12 +0200479exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100480 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
481 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000483}
Paul Bakker33b43f12013-08-20 11:48:36 +0200484/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000485
Paul Bakker33b43f12013-08-20 11:48:36 +0200486/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100487void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100488 int digest, int mod, int radix_N,
489 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100490 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000491{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200492 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000494
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100495 mbedtls_mpi N, E;
496
497 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200499 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000500
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100501 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
502 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
503 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
504 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000506
Paul Bakker42a29bf2009-07-07 20:18:41 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100509 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 +0000510
Azim Khand30ca132017-06-09 04:32:58 +0100511 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 +0100512
Paul Bakkerbd51b262014-07-10 15:26:12 +0200513exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100514 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000516}
Paul Bakker33b43f12013-08-20 11:48:36 +0200517/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000518
Paul Bakker821fb082009-07-12 13:26:42 +0000519
Paul Bakker33b43f12013-08-20 11:48:36 +0200520/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100521void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100522 int padding_mode, int mod, int radix_P,
523 char * input_P, int radix_Q, char * input_Q,
524 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200525 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000526{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200527 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100529 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200530 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100533 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
534 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000535
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200536 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200537 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000538
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100539 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
540 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
541 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
542 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000543
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100544 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
545 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100546 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000548
Paul Bakker821fb082009-07-12 13:26:42 +0000549
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200550 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100551 &rnd_info, MBEDTLS_MD_NONE,
552 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200553 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000554
Paul Bakker821fb082009-07-12 13:26:42 +0000555
Ronald Cronac6ae352020-06-26 14:33:03 +0200556 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
557 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000558
Paul Bakkerbd51b262014-07-10 15:26:12 +0200559exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100560 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
561 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000564}
Paul Bakker33b43f12013-08-20 11:48:36 +0200565/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000566
Paul Bakker33b43f12013-08-20 11:48:36 +0200567/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100568void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200569 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100570 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100571 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000572{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200573 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000575
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100576 mbedtls_mpi N, E;
577 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100580 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000581
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100582 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
583 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000584
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100585 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
586 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000588
Paul Bakker821fb082009-07-12 13:26:42 +0000589
Azim Khand30ca132017-06-09 04:32:58 +0100590 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 +0100591
Paul Bakkerbd51b262014-07-10 15:26:12 +0200592exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100593 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000595}
Paul Bakker33b43f12013-08-20 11:48:36 +0200596/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000597
Paul Bakker33b43f12013-08-20 11:48:36 +0200598/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100599void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100600 int mod, int radix_N, char * input_N,
601 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200602 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000603{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200604 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200606 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000607
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100608 mbedtls_mpi N, E;
609 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
610
Ronald Cron351f0ee2020-06-10 12:12:18 +0200611 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200614 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000615
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100616 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
617 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000618
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100619 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
620 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000622
Paul Bakker42a29bf2009-07-07 20:18:41 +0000623
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200624 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
625 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100626 &rnd_info, message_str->len,
627 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200628 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200629 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000630 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000631
Ronald Cronac6ae352020-06-26 14:33:03 +0200632 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
633 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000634 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100635
Paul Bakkerbd51b262014-07-10 15:26:12 +0200636exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100637 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000639}
Paul Bakker33b43f12013-08-20 11:48:36 +0200640/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000641
Paul Bakker33b43f12013-08-20 11:48:36 +0200642/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100643void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100644 int mod, int radix_N, char * input_N,
645 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200646 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000647{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200648 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000650
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100651 mbedtls_mpi N, E;
652
653 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200655 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000656
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100657 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
658 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000659
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100660 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
661 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000663
Paul Bakkera6656852010-07-18 19:47:14 +0000664
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200665 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100666 NULL, message_str->len,
667 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200668 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200669 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000670 {
Paul Bakkera6656852010-07-18 19:47:14 +0000671
Ronald Cronac6ae352020-06-26 14:33:03 +0200672 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
673 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000674 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100675
Paul Bakkerbd51b262014-07-10 15:26:12 +0200676exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100677 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000679}
Paul Bakker33b43f12013-08-20 11:48:36 +0200680/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000681
Paul Bakker33b43f12013-08-20 11:48:36 +0200682/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100683void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100684 int mod, int radix_P, char * input_P,
685 int radix_Q, char * input_Q, int radix_N,
686 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200687 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100688 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000689{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200690 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000692 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200693 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100694 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000695
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100696 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
697 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000700
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200701 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200702 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000703
Paul Bakker42a29bf2009-07-07 20:18:41 +0000704
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100705 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
706 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
707 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
708 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000709
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100710 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
711 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100712 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000714
Paul Bakker69998dd2009-07-11 19:15:20 +0000715 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000716
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200717 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100718 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200719 &output_len, message_str->x, output,
720 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200721 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000722 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000723
Ronald Cronac6ae352020-06-26 14:33:03 +0200724 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200725 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200726 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000727 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000728
Paul Bakkerbd51b262014-07-10 15:26:12 +0200729exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100730 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
731 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000733}
Paul Bakker33b43f12013-08-20 11:48:36 +0200734/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000735
Paul Bakker33b43f12013-08-20 11:48:36 +0200736/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100737void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100738 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200739 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000740{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200741 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000743
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100744 mbedtls_mpi N, E;
745
746 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
748 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200749 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000750
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100751 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
752 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000753
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100754 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
755 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000757
Paul Bakker821fb082009-07-12 13:26:42 +0000758
Azim Khand30ca132017-06-09 04:32:58 +0100759 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200760 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000761 {
Paul Bakker821fb082009-07-12 13:26:42 +0000762
Ronald Cronac6ae352020-06-26 14:33:03 +0200763 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
764 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000765 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100766
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100767 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200769 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100773
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200774 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100775 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100776 if( result == 0 )
777 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100778
Ronald Cronac6ae352020-06-26 14:33:03 +0200779 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
780 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100781 }
782
Paul Bakkerbd51b262014-07-10 15:26:12 +0200783exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100784 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 mbedtls_rsa_free( &ctx );
786 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000787}
Paul Bakker33b43f12013-08-20 11:48:36 +0200788/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000789
Paul Bakker33b43f12013-08-20 11:48:36 +0200790/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100791void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100792 char * input_P, int radix_Q, char * input_Q,
793 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200794 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100795 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000796{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200797 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100799 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200800 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200801 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000802
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100803 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
804 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
806 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000807
Ronald Cron351f0ee2020-06-10 12:12:18 +0200808 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000809
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100810 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
811 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
812 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
813 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000814
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100815 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
816 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100817 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000819
Paul Bakker821fb082009-07-12 13:26:42 +0000820
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200821 /* repeat three times to test updating of blinding values */
822 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000823 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200824 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200825 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
826 &rnd_info, message_str->x,
827 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200828 if( result == 0 )
829 {
Paul Bakker821fb082009-07-12 13:26:42 +0000830
Ronald Cronac6ae352020-06-26 14:33:03 +0200831 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200832 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200833 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200834 }
Paul Bakker821fb082009-07-12 13:26:42 +0000835 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000836
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100837 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200839 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100843
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200844 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200845 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
846 &rnd_info, message_str->x,
847 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100848 if( result == 0 )
849 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100850
Ronald Cronac6ae352020-06-26 14:33:03 +0200851 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200852 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200853 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100854 }
855
Paul Bakkerbd51b262014-07-10 15:26:12 +0200856exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100857 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
858 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000861}
Paul Bakker33b43f12013-08-20 11:48:36 +0200862/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000863
Paul Bakker33b43f12013-08-20 11:48:36 +0200864/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100865void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000866{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_rsa_context ctx;
868 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000871}
Paul Bakker33b43f12013-08-20 11:48:36 +0200872/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000873
Paul Bakker33b43f12013-08-20 11:48:36 +0200874/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100875void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
876 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000877{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100879 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000880
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100881 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000883
Paul Bakker33b43f12013-08-20 11:48:36 +0200884 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000885 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100886 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000887 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200888 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000889 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100890 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000891 }
892
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100893 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100895
Paul Bakkerbd51b262014-07-10 15:26:12 +0200896exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100897 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000899}
Paul Bakker33b43f12013-08-20 11:48:36 +0200900/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000901
Paul Bakker33b43f12013-08-20 11:48:36 +0200902/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100903void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
904 int radix_Q, char * input_Q, int radix_N,
905 char * input_N, int radix_E, char * input_E,
906 int radix_D, char * input_D, int radix_DP,
907 char * input_DP, int radix_DQ,
908 char * input_DQ, int radix_QP,
909 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000910{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000914
Paul Bakker33b43f12013-08-20 11:48:36 +0200915 ctx.len = mod / 8;
916 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000919 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200920 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000923 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200924 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000927 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200928 if( strlen( input_E ) )
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.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000931 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200932 if( strlen( input_D ) )
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.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000935 }
Hanno Becker131134f2017-08-23 08:31:07 +0100936#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200937 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000940 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200941 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000944 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200945 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000948 }
Hanno Becker131134f2017-08-23 08:31:07 +0100949#else
950 ((void) radix_DP); ((void) input_DP);
951 ((void) radix_DQ); ((void) input_DQ);
952 ((void) radix_QP); ((void) input_QP);
953#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100956
Paul Bakkerbd51b262014-07-10 15:26:12 +0200957exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000959}
Paul Bakker33b43f12013-08-20 11:48:36 +0200960/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000961
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100962/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100963void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
964 int radix_Epub, char * input_Epub, int radix_P,
965 char * input_P, int radix_Q, char * input_Q,
966 int radix_N, char * input_N, int radix_E,
967 char * input_E, int radix_D, char * input_D,
968 int radix_DP, char * input_DP, int radix_DQ,
969 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100970 int result )
971{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
975 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100976
977 pub.len = mod / 8;
978 prv.len = mod / 8;
979
980 if( strlen( input_Npub ) )
981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100983 }
984 if( strlen( input_Epub ) )
985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100987 }
988
989 if( strlen( input_P ) )
990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100992 }
993 if( strlen( input_Q ) )
994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100996 }
997 if( strlen( input_N ) )
998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001000 }
1001 if( strlen( input_E ) )
1002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001004 }
1005 if( strlen( input_D ) )
1006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001008 }
Hanno Becker131134f2017-08-23 08:31:07 +01001009#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001010 if( strlen( input_DP ) )
1011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001013 }
1014 if( strlen( input_DQ ) )
1015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001017 }
1018 if( strlen( input_QP ) )
1019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001021 }
Hanno Becker131134f2017-08-23 08:31:07 +01001022#else
1023 ((void) radix_DP); ((void) input_DP);
1024 ((void) radix_DQ); ((void) input_DQ);
1025 ((void) radix_QP); ((void) input_QP);
1026#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001029
1030exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_rsa_free( &pub );
1032 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001033}
1034/* END_CASE */
1035
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001036/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001038{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_rsa_context ctx;
1040 mbedtls_entropy_context entropy;
1041 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001042 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001043
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001044 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001046 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001047
Hanno Beckera47023e2017-12-22 17:08:03 +00001048 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1049 &entropy, (const unsigned char *) pers,
1050 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001053 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001056 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001057 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001058
Paul Bakkerbd51b262014-07-10 15:26:12 +02001059exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 mbedtls_rsa_free( &ctx );
1061 mbedtls_ctr_drbg_free( &ctr_drbg );
1062 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001063}
Paul Bakker33b43f12013-08-20 11:48:36 +02001064/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001065
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001066/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001067void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001068 int radix_D, char *input_D,
1069 int radix_E, char *input_E,
1070 int radix_P, char *output_P,
1071 int radix_Q, char *output_Q,
1072 int corrupt, int result )
1073{
1074 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1075
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001076 mbedtls_mpi_init( &N );
1077 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1078 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1079 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1080
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001081 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1082 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1083 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1084 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1085 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1086
1087 if( corrupt )
1088 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1089
1090 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001091 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001092
1093 if( !corrupt )
1094 {
1095 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1096 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1097 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1098 }
1099
1100exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001101 mbedtls_mpi_free( &N );
1102 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1103 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1104 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001105}
1106/* END_CASE */
1107
Hanno Becker6b4ce492017-08-23 11:00:21 +01001108/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001109void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1110 int radix_Q, char *input_Q,
1111 int radix_E, char *input_E,
1112 int radix_D, char *output_D,
1113 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001114{
1115 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1116
1117 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1118 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1119 mbedtls_mpi_init( &E );
1120 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1121
1122 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1123 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1124 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1125 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1126
1127 if( corrupt )
1128 {
1129 /* Make E even */
1130 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1131 }
1132
1133 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001134 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1135 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001136
1137 if( !corrupt )
1138 {
1139 /*
1140 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1141 */
1142
1143 /* Replace P,Q by P-1, Q-1 */
1144 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1145 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1146
1147 /* Check D == Dp modulo P-1 */
1148 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1149 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1150 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1151
1152 /* Check D == Dp modulo Q-1 */
1153 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1154 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1155 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1156 }
1157
1158exit:
1159
1160 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1161 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1162 mbedtls_mpi_free( &E );
1163 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1164}
1165/* END_CASE */
1166
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001167/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001168void mbedtls_rsa_import( int radix_N, char *input_N,
1169 int radix_P, char *input_P,
1170 int radix_Q, char *input_Q,
1171 int radix_D, char *input_D,
1172 int radix_E, char *input_E,
1173 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001174 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001175 int res_check,
1176 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001177{
1178 mbedtls_mpi N, P, Q, D, E;
1179 mbedtls_rsa_context ctx;
1180
Hanno Beckere1582a82017-09-29 11:51:05 +01001181 /* Buffers used for encryption-decryption test */
1182 unsigned char *buf_orig = NULL;
1183 unsigned char *buf_enc = NULL;
1184 unsigned char *buf_dec = NULL;
1185
Hanno Beckerc77ab892017-08-23 11:01:06 +01001186 mbedtls_entropy_context entropy;
1187 mbedtls_ctr_drbg_context ctr_drbg;
1188 const char *pers = "test_suite_rsa";
1189
Hanno Becker4d6e8342017-09-29 11:50:18 +01001190 const int have_N = ( strlen( input_N ) > 0 );
1191 const int have_P = ( strlen( input_P ) > 0 );
1192 const int have_Q = ( strlen( input_Q ) > 0 );
1193 const int have_D = ( strlen( input_D ) > 0 );
1194 const int have_E = ( strlen( input_E ) > 0 );
1195
Hanno Beckerc77ab892017-08-23 11:01:06 +01001196 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001197 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001198 mbedtls_rsa_init( &ctx, 0, 0 );
1199
1200 mbedtls_mpi_init( &N );
1201 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1202 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1203
Hanno Beckerd4d60572018-01-10 07:12:01 +00001204 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1205 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1206
Hanno Becker4d6e8342017-09-29 11:50:18 +01001207 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001208 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1209
Hanno Becker4d6e8342017-09-29 11:50:18 +01001210 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001211 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1212
Hanno Becker4d6e8342017-09-29 11:50:18 +01001213 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001214 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1215
Hanno Becker4d6e8342017-09-29 11:50:18 +01001216 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001217 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1218
Hanno Becker4d6e8342017-09-29 11:50:18 +01001219 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001220 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1221
1222 if( !successive )
1223 {
1224 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001225 have_N ? &N : NULL,
1226 have_P ? &P : NULL,
1227 have_Q ? &Q : NULL,
1228 have_D ? &D : NULL,
1229 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001230 }
1231 else
1232 {
1233 /* Import N, P, Q, D, E separately.
1234 * This should make no functional difference. */
1235
1236 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001237 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001238 NULL, NULL, NULL, NULL ) == 0 );
1239
1240 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1241 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001242 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001243 NULL, NULL, NULL ) == 0 );
1244
1245 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1246 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001247 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001248 NULL, NULL ) == 0 );
1249
1250 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1251 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001252 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001253 NULL ) == 0 );
1254
1255 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1256 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001257 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001258 }
1259
Hanno Becker04877a42017-10-11 10:01:33 +01001260 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001261
Hanno Beckere1582a82017-09-29 11:51:05 +01001262 /* On expected success, perform some public and private
1263 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001264 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001265 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001266 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001267 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1268 else
1269 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1270
1271 if( res_check != 0 )
1272 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001273
1274 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1275 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1276 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1277 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1278 goto exit;
1279
1280 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1281 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1282
1283 /* Make sure the number we're generating is smaller than the modulus */
1284 buf_orig[0] = 0x00;
1285
1286 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1287
1288 if( is_priv )
1289 {
1290 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1291 &ctr_drbg, buf_enc,
1292 buf_dec ) == 0 );
1293
1294 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1295 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1296 }
1297 }
1298
Hanno Beckerc77ab892017-08-23 11:01:06 +01001299exit:
1300
Hanno Beckere1582a82017-09-29 11:51:05 +01001301 mbedtls_free( buf_orig );
1302 mbedtls_free( buf_enc );
1303 mbedtls_free( buf_dec );
1304
Hanno Beckerc77ab892017-08-23 11:01:06 +01001305 mbedtls_rsa_free( &ctx );
1306
1307 mbedtls_ctr_drbg_free( &ctr_drbg );
1308 mbedtls_entropy_free( &entropy );
1309
1310 mbedtls_mpi_free( &N );
1311 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1312 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1313}
1314/* END_CASE */
1315
Hanno Becker417f2d62017-08-23 11:44:51 +01001316/* BEGIN_CASE */
1317void mbedtls_rsa_export( int radix_N, char *input_N,
1318 int radix_P, char *input_P,
1319 int radix_Q, char *input_Q,
1320 int radix_D, char *input_D,
1321 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001322 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001323 int successive )
1324{
1325 /* Original MPI's with which we set up the RSA context */
1326 mbedtls_mpi N, P, Q, D, E;
1327
1328 /* Exported MPI's */
1329 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1330
1331 const int have_N = ( strlen( input_N ) > 0 );
1332 const int have_P = ( strlen( input_P ) > 0 );
1333 const int have_Q = ( strlen( input_Q ) > 0 );
1334 const int have_D = ( strlen( input_D ) > 0 );
1335 const int have_E = ( strlen( input_E ) > 0 );
1336
Hanno Becker417f2d62017-08-23 11:44:51 +01001337 mbedtls_rsa_context ctx;
1338
1339 mbedtls_rsa_init( &ctx, 0, 0 );
1340
1341 mbedtls_mpi_init( &N );
1342 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1343 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1344
1345 mbedtls_mpi_init( &Ne );
1346 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1347 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1348
1349 /* Setup RSA context */
1350
1351 if( have_N )
1352 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1353
1354 if( have_P )
1355 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1356
1357 if( have_Q )
1358 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1359
1360 if( have_D )
1361 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1362
1363 if( have_E )
1364 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1365
1366 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1367 strlen( input_N ) ? &N : NULL,
1368 strlen( input_P ) ? &P : NULL,
1369 strlen( input_Q ) ? &Q : NULL,
1370 strlen( input_D ) ? &D : NULL,
1371 strlen( input_E ) ? &E : NULL ) == 0 );
1372
Hanno Becker7f25f852017-10-10 16:56:22 +01001373 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001374
1375 /*
1376 * Export parameters and compare to original ones.
1377 */
1378
1379 /* N and E must always be present. */
1380 if( !successive )
1381 {
1382 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1383 }
1384 else
1385 {
1386 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1387 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1388 }
1389 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1390 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1391
1392 /* If we were providing enough information to setup a complete private context,
1393 * we expect to be able to export all core parameters. */
1394
1395 if( is_priv )
1396 {
1397 if( !successive )
1398 {
1399 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1400 &De, NULL ) == 0 );
1401 }
1402 else
1403 {
1404 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1405 NULL, NULL ) == 0 );
1406 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1407 NULL, NULL ) == 0 );
1408 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1409 &De, NULL ) == 0 );
1410 }
1411
1412 if( have_P )
1413 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1414
1415 if( have_Q )
1416 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1417
1418 if( have_D )
1419 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1420
1421 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001422 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1423 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001424 }
1425
1426exit:
1427
1428 mbedtls_rsa_free( &ctx );
1429
1430 mbedtls_mpi_free( &N );
1431 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1432 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1433
1434 mbedtls_mpi_free( &Ne );
1435 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1436 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1437}
1438/* END_CASE */
1439
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001440/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001441void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1442 int radix_P, char *input_P,
1443 int radix_Q, char *input_Q,
1444 int radix_D, char *input_D,
1445 int radix_E, char *input_E,
1446 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001447{
1448 /* Original MPI's with which we set up the RSA context */
1449 mbedtls_mpi N, P, Q, D, E;
1450
1451 const int have_N = ( strlen( input_N ) > 0 );
1452 const int have_P = ( strlen( input_P ) > 0 );
1453 const int have_Q = ( strlen( input_Q ) > 0 );
1454 const int have_D = ( strlen( input_D ) > 0 );
1455 const int have_E = ( strlen( input_E ) > 0 );
1456
1457 mbedtls_entropy_context entropy;
1458 mbedtls_ctr_drbg_context ctr_drbg;
1459 const char *pers = "test_suite_rsa";
1460
1461 mbedtls_mpi_init( &N );
1462 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1463 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1464
1465 mbedtls_ctr_drbg_init( &ctr_drbg );
1466 mbedtls_entropy_init( &entropy );
1467 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1468 &entropy, (const unsigned char *) pers,
1469 strlen( pers ) ) == 0 );
1470
1471 if( have_N )
1472 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1473
1474 if( have_P )
1475 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1476
1477 if( have_Q )
1478 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1479
1480 if( have_D )
1481 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1482
1483 if( have_E )
1484 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1485
Hanno Becker750e8b42017-08-25 07:54:27 +01001486 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1487 have_P ? &P : NULL,
1488 have_Q ? &Q : NULL,
1489 have_D ? &D : NULL,
1490 have_E ? &E : NULL,
1491 prng ? mbedtls_ctr_drbg_random : NULL,
1492 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001493exit:
1494
1495 mbedtls_ctr_drbg_free( &ctr_drbg );
1496 mbedtls_entropy_free( &entropy );
1497
1498 mbedtls_mpi_free( &N );
1499 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1500 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1501}
1502/* END_CASE */
1503
Hanno Beckerc77ab892017-08-23 11:01:06 +01001504/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001505void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1506 data_t *input_Q, data_t *input_D,
1507 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001508 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001509{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001510 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001511 unsigned char bufNe[256];
1512 unsigned char bufPe[128];
1513 unsigned char bufQe[128];
1514 unsigned char bufDe[256];
1515 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001516
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001517 mbedtls_rsa_context ctx;
1518
1519 mbedtls_rsa_init( &ctx, 0, 0 );
1520
1521 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001522 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001523 input_N->len ? input_N->x : NULL, input_N->len,
1524 input_P->len ? input_P->x : NULL, input_P->len,
1525 input_Q->len ? input_Q->x : NULL, input_Q->len,
1526 input_D->len ? input_D->x : NULL, input_D->len,
1527 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001528
Hanno Becker7f25f852017-10-10 16:56:22 +01001529 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001530
1531 /*
1532 * Export parameters and compare to original ones.
1533 */
1534
1535 /* N and E must always be present. */
1536 if( !successive )
1537 {
Azim Khand30ca132017-06-09 04:32:58 +01001538 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001539 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001540 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001541 }
1542 else
1543 {
Azim Khand30ca132017-06-09 04:32:58 +01001544 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001545 NULL, 0, NULL, 0, NULL, 0,
1546 NULL, 0 ) == 0 );
1547 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1548 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001549 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001550 }
Azim Khand30ca132017-06-09 04:32:58 +01001551 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1552 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001553
1554 /* If we were providing enough information to setup a complete private context,
1555 * we expect to be able to export all core parameters. */
1556
1557 if( is_priv )
1558 {
1559 if( !successive )
1560 {
1561 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001562 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1563 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1564 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001565 NULL, 0 ) == 0 );
1566 }
1567 else
1568 {
1569 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001570 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001571 NULL, 0, NULL, 0,
1572 NULL, 0 ) == 0 );
1573
1574 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001575 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001576 NULL, 0, NULL, 0 ) == 0 );
1577
Azim Khand30ca132017-06-09 04:32:58 +01001578 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1579 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001580 NULL, 0 ) == 0 );
1581 }
1582
Azim Khand30ca132017-06-09 04:32:58 +01001583 if( input_P->len )
1584 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001585
Azim Khand30ca132017-06-09 04:32:58 +01001586 if( input_Q->len )
1587 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001588
Azim Khand30ca132017-06-09 04:32:58 +01001589 if( input_D->len )
1590 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001591
1592 }
1593
1594exit:
1595 mbedtls_rsa_free( &ctx );
1596}
1597/* END_CASE */
1598
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001599/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001600void mbedtls_rsa_import_raw( data_t *input_N,
1601 data_t *input_P, data_t *input_Q,
1602 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001603 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001604 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001605 int res_check,
1606 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001607{
Hanno Beckere1582a82017-09-29 11:51:05 +01001608 /* Buffers used for encryption-decryption test */
1609 unsigned char *buf_orig = NULL;
1610 unsigned char *buf_enc = NULL;
1611 unsigned char *buf_dec = NULL;
1612
Hanno Beckerc77ab892017-08-23 11:01:06 +01001613 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001614 mbedtls_entropy_context entropy;
1615 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001616
Hanno Beckerc77ab892017-08-23 11:01:06 +01001617 const char *pers = "test_suite_rsa";
1618
1619 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001620 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001621 mbedtls_rsa_init( &ctx, 0, 0 );
1622
Hanno Beckerc77ab892017-08-23 11:01:06 +01001623 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1624 &entropy, (const unsigned char *) pers,
1625 strlen( pers ) ) == 0 );
1626
Hanno Beckerc77ab892017-08-23 11:01:06 +01001627 if( !successive )
1628 {
1629 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001630 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1631 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1632 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1633 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1634 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001635 }
1636 else
1637 {
1638 /* Import N, P, Q, D, E separately.
1639 * This should make no functional difference. */
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,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001643 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1644
1645 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1646 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001647 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001648 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1649
1650 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1651 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001652 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001653 NULL, 0, NULL, 0 ) == 0 );
1654
1655 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1656 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001657 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001658 NULL, 0 ) == 0 );
1659
1660 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1661 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001662 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001663 }
1664
Hanno Becker04877a42017-10-11 10:01:33 +01001665 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001666
Hanno Beckere1582a82017-09-29 11:51:05 +01001667 /* On expected success, perform some public and private
1668 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001669 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001670 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001671 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001672 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1673 else
1674 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1675
1676 if( res_check != 0 )
1677 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001678
1679 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1680 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1681 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1682 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1683 goto exit;
1684
1685 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1686 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1687
1688 /* Make sure the number we're generating is smaller than the modulus */
1689 buf_orig[0] = 0x00;
1690
1691 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1692
1693 if( is_priv )
1694 {
1695 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1696 &ctr_drbg, buf_enc,
1697 buf_dec ) == 0 );
1698
1699 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1700 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1701 }
1702 }
1703
Hanno Beckerc77ab892017-08-23 11:01:06 +01001704exit:
1705
Hanno Becker3f3ae852017-10-02 10:08:39 +01001706 mbedtls_free( buf_orig );
1707 mbedtls_free( buf_enc );
1708 mbedtls_free( buf_dec );
1709
Hanno Beckerc77ab892017-08-23 11:01:06 +01001710 mbedtls_rsa_free( &ctx );
1711
1712 mbedtls_ctr_drbg_free( &ctr_drbg );
1713 mbedtls_entropy_free( &entropy );
1714
1715}
1716/* END_CASE */
1717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001719void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001720{
Andres AG93012e82016-09-09 09:10:28 +01001721 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001722}
Paul Bakker33b43f12013-08-20 11:48:36 +02001723/* END_CASE */