blob: 60c13c84ee06e27a602678678308ae2321639a6a [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"
Hanno Beckera565f542017-10-11 11:00:19 +01003#include "mbedtls/rsa_internal.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
Hanno Becker046d2022018-12-13 18:07:09 +000020/* 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
Hanno Beckera7ee0022018-12-18 13:30:20 +000031 TEST_INVALID_PARAM( mbedtls_rsa_init( NULL, valid_padding, 0 ) );
32 TEST_INVALID_PARAM( mbedtls_rsa_init( &ctx, invalid_padding, 0 ) );
Hanno Becker046d2022018-12-13 18:07:09 +000033 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,
Hanno Becker046d2022018-12-13 18:07:09 +000055 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,
Hanno Beckerf04d9232018-12-18 13:30:42 +000070 mbedtls_rsa_gen_key( NULL, rnd_std_rand,
71 NULL, 0, 0 ) );
72 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
73 mbedtls_rsa_gen_key( &ctx, NULL,
74 NULL, 0, 0 ) );
Hanno Becker046d2022018-12-13 18:07:09 +000075
76 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
77 mbedtls_rsa_check_pubkey( NULL ) );
78 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
79 mbedtls_rsa_check_privkey( NULL ) );
80
81 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
82 mbedtls_rsa_check_pub_priv( NULL, &ctx ) );
83 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
84 mbedtls_rsa_check_pub_priv( &ctx, NULL ) );
85
86 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
87 mbedtls_rsa_public( NULL, buf, buf ) );
88 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
89 mbedtls_rsa_public( &ctx, NULL, buf ) );
90 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
91 mbedtls_rsa_public( &ctx, buf, NULL ) );
92
93 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
94 mbedtls_rsa_private( NULL, NULL, NULL,
95 buf, buf ) );
96 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
97 mbedtls_rsa_private( &ctx, NULL, NULL,
98 NULL, buf ) );
99 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
100 mbedtls_rsa_private( &ctx, NULL, NULL,
101 buf, NULL ) );
102
103 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
104 mbedtls_rsa_pkcs1_encrypt( NULL, NULL, NULL,
105 valid_mode,
106 sizeof( buf ), buf,
107 buf ) );
108 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
109 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
110 invalid_mode,
111 sizeof( buf ), buf,
112 buf ) );
113 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
114 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
115 valid_mode,
116 sizeof( buf ), NULL,
117 buf ) );
118 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
119 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
120 valid_mode,
121 sizeof( buf ), buf,
122 NULL ) );
123
124 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
125 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
126 NULL,
127 valid_mode,
128 sizeof( buf ), buf,
129 buf ) );
130 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
131 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
132 NULL,
133 invalid_mode,
134 sizeof( buf ), buf,
135 buf ) );
136 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
137 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
138 NULL,
139 valid_mode,
140 sizeof( buf ), NULL,
141 buf ) );
142 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
143 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
144 NULL,
145 valid_mode,
146 sizeof( buf ), buf,
147 NULL ) );
148
149 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
150 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
151 valid_mode,
152 buf, sizeof( buf ),
153 sizeof( buf ), buf,
154 buf ) );
155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
156 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
157 invalid_mode,
158 buf, sizeof( buf ),
159 sizeof( buf ), buf,
160 buf ) );
161 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
162 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
163 valid_mode,
164 NULL, sizeof( buf ),
165 sizeof( buf ), buf,
166 buf ) );
167 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
168 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
169 valid_mode,
170 buf, sizeof( buf ),
171 sizeof( buf ), NULL,
172 buf ) );
173 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
174 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
175 valid_mode,
176 buf, sizeof( buf ),
177 sizeof( buf ), buf,
178 NULL ) );
179
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
181 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
182 valid_mode, &olen,
183 buf, buf, 42 ) );
184 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
185 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
186 invalid_mode, &olen,
187 buf, buf, 42 ) );
188 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
189 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
190 valid_mode, NULL,
191 buf, buf, 42 ) );
192 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
193 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
194 valid_mode, &olen,
195 NULL, buf, 42 ) );
196 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
197 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
198 valid_mode, &olen,
199 buf, NULL, 42 ) );
200
201 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
202 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
203 NULL,
204 valid_mode, &olen,
205 buf, buf, 42 ) );
206 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
207 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
208 NULL,
209 invalid_mode, &olen,
210 buf, buf, 42 ) );
211 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
212 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
213 NULL,
214 valid_mode, NULL,
215 buf, buf, 42 ) );
216 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
217 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
218 NULL,
219 valid_mode, &olen,
220 NULL, buf, 42 ) );
221 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
222 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
223 NULL,
224 valid_mode, &olen,
225 buf, NULL, 42 ) );
226
227 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
228 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
229 valid_mode,
230 buf, sizeof( buf ),
231 &olen,
232 buf, buf, 42 ) );
233 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
234 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
235 invalid_mode,
236 buf, sizeof( buf ),
237 &olen,
238 buf, buf, 42 ) );
239 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
240 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
241 valid_mode,
242 NULL, sizeof( buf ),
243 NULL,
244 buf, buf, 42 ) );
245 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
246 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
247 valid_mode,
248 buf, sizeof( buf ),
249 &olen,
250 NULL, buf, 42 ) );
251 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
252 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
253 valid_mode,
254 buf, sizeof( buf ),
255 &olen,
256 buf, NULL, 42 ) );
257
258 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
259 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
260 valid_mode,
261 0, sizeof( buf ), buf,
262 buf ) );
263 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
264 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
265 invalid_mode,
266 0, sizeof( buf ), buf,
267 buf ) );
268 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
269 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
270 valid_mode,
271 0, sizeof( buf ), NULL,
272 buf ) );
273 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
274 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
275 valid_mode,
276 0, sizeof( buf ), buf,
277 NULL ) );
278
279 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
280 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
281 valid_mode,
282 0, sizeof( buf ), buf,
283 buf ) );
284 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
285 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
286 invalid_mode,
287 0, sizeof( buf ), buf,
288 buf ) );
289 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
290 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
291 valid_mode,
292 0, sizeof( buf ), NULL,
293 buf ) );
294 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
295 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
296 valid_mode,
297 0, sizeof( buf ), buf,
298 NULL ) );
299
300 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
301 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
302 valid_mode,
303 0, sizeof( buf ), buf,
304 buf ) );
305 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
306 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
307 invalid_mode,
308 0, sizeof( buf ), buf,
309 buf ) );
310 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
311 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
312 valid_mode,
313 0, sizeof( buf ), NULL,
314 buf ) );
315 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
316 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
317 valid_mode,
318 0, sizeof( buf ), buf,
319 NULL ) );
320
321 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
322 mbedtls_rsa_pkcs1_verify( NULL, NULL, 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_pkcs1_verify( &ctx, NULL, NULL,
328 invalid_mode,
329 0, sizeof( buf ), buf,
330 buf ) );
331 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
332 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
333 valid_mode,
334 0, sizeof( buf ), NULL,
335 buf ) );
336 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
337 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
338 valid_mode,
339 0, sizeof( buf ), buf,
340 NULL ) );
341
342 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
343 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
344 NULL,
345 valid_mode,
346 0, sizeof( buf ), buf,
347 buf ) );
348 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
349 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
350 NULL,
351 invalid_mode,
352 0, sizeof( buf ), buf,
353 buf ) );
354 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
355 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
356 NULL,
357 valid_mode,
358 0, sizeof( buf ),
359 NULL, buf ) );
360 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
361 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
362 NULL,
363 valid_mode,
364 0, sizeof( buf ), buf,
365 NULL ) );
366
367 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
368 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
369 valid_mode,
370 0, sizeof( buf ),
371 buf, buf ) );
372 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
373 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
374 invalid_mode,
375 0, sizeof( buf ),
376 buf, buf ) );
377 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
378 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
379 valid_mode,
380 0, sizeof( buf ),
381 NULL, buf ) );
382 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
383 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
384 valid_mode,
385 0, sizeof( buf ),
386 buf, NULL ) );
387
388 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
389 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
390 valid_mode,
391 0, sizeof( buf ),
392 buf,
393 0, 0,
394 buf ) );
395 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
396 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
397 invalid_mode,
398 0, sizeof( buf ),
399 buf,
400 0, 0,
401 buf ) );
402 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
403 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
404 valid_mode,
405 0, sizeof( buf ),
406 NULL, 0, 0,
407 buf ) );
408 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
409 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
410 valid_mode,
411 0, sizeof( buf ),
412 buf, 0, 0,
413 NULL ) );
414
415 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
416 mbedtls_rsa_copy( NULL, &ctx ) );
417 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
418 mbedtls_rsa_copy( &ctx, NULL ) );
419
420exit:
421 return;
422}
423/* END_CASE */
424
Paul Bakker33b43f12013-08-20 11:48:36 +0200425/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100426void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100427 int digest, int mod, int radix_P, char * input_P,
428 int radix_Q, char * input_Q, int radix_N,
429 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100430 data_t * result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000431{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000432 unsigned char hash_result[1000];
433 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100435 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200436 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000437
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100438 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
439 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000441
Paul Bakker42a29bf2009-07-07 20:18:41 +0000442 memset( hash_result, 0x00, 1000 );
443 memset( output, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200444 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000445
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100446 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
447 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
448 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
449 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000450
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100451 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
452 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100453 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000455
Paul Bakker42a29bf2009-07-07 20:18:41 +0000456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100458 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 +0000459
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100460 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
461 MBEDTLS_RSA_PRIVATE, digest, 0,
462 hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200463 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000464 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000465
Azim Khand30ca132017-06-09 04:32:58 +0100466 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000467 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000468
Paul Bakkerbd51b262014-07-10 15:26:12 +0200469exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100470 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
471 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000473}
Paul Bakker33b43f12013-08-20 11:48:36 +0200474/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000475
Paul Bakker33b43f12013-08-20 11:48:36 +0200476/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100477void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100478 int digest, int mod, int radix_N,
479 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100480 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000481{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000482 unsigned char hash_result[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000484
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100485 mbedtls_mpi N, E;
486
487 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000489 memset( hash_result, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000490
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100491 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
492 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
493 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
494 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000496
Paul Bakker42a29bf2009-07-07 20:18:41 +0000497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100499 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 +0000500
Azim Khand30ca132017-06-09 04:32:58 +0100501 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 +0100502
Paul Bakkerbd51b262014-07-10 15:26:12 +0200503exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100504 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000506}
Paul Bakker33b43f12013-08-20 11:48:36 +0200507/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000508
Paul Bakker821fb082009-07-12 13:26:42 +0000509
Paul Bakker33b43f12013-08-20 11:48:36 +0200510/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100511void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100512 int padding_mode, int mod, int radix_P,
513 char * input_P, int radix_Q, char * input_Q,
514 int radix_N, char * input_N, int radix_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100515 char * input_E, data_t * result_hex_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000516{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000517 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100519 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200520 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100523 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
524 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000525
Paul Bakker42a29bf2009-07-07 20:18:41 +0000526 memset( output, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200527 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000528
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100529 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
530 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
531 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
532 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000533
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100534 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
535 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100536 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000538
Paul Bakker821fb082009-07-12 13:26:42 +0000539
Hanno Becker8fd55482017-08-23 14:07:48 +0100540 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
541 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
Azim Khand30ca132017-06-09 04:32:58 +0100542 hash_result->len, hash_result->x,
543 output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000544
Paul Bakker821fb082009-07-12 13:26:42 +0000545
Azim Khand30ca132017-06-09 04:32:58 +0100546 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000547
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200548#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100549 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100551 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100552 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100553 memset( output, 0x00, 1000 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100554
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100555 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Azim Khand30ca132017-06-09 04:32:58 +0100557 hash_result->len, hash_result->x, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100558
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100559#if !defined(MBEDTLS_RSA_ALT)
560 TEST_ASSERT( res == 0 );
561#else
562 TEST_ASSERT( ( res == 0 ) ||
563 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
564#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100565
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100566 if( res == 0 )
567 {
Azim Khand30ca132017-06-09 04:32:58 +0100568 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100569 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100570 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200571#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100572
Paul Bakkerbd51b262014-07-10 15:26:12 +0200573exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100574 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
575 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000578}
Paul Bakker33b43f12013-08-20 11:48:36 +0200579/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000580
Paul Bakker33b43f12013-08-20 11:48:36 +0200581/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100582void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200583 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100584 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100585 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000586{
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100587 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000589
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100590 mbedtls_mpi N, E;
591 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100594 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000595
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100596 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
597 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000598
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100599 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
600 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000602
Paul Bakker821fb082009-07-12 13:26:42 +0000603
Azim Khand30ca132017-06-09 04:32:58 +0100604 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 +0100605
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200606#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100607 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100609 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100610 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100611 int ok;
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200612 size_t olen;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100613
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100614 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Azim Khand30ca132017-06-09 04:32:58 +0100616 &olen, result_str->x, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100617
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100618#if !defined(MBEDTLS_RSA_ALT)
619 TEST_ASSERT( res == 0 );
620#else
621 TEST_ASSERT( ( res == 0 ) ||
622 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
623#endif
624
625 if( res == 0 )
626 {
Azim Khand30ca132017-06-09 04:32:58 +0100627 ok = olen == hash_result->len && memcmp( output, hash_result->x, olen ) == 0;
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100628 if( correct == 0 )
629 TEST_ASSERT( ok == 1 );
630 else
631 TEST_ASSERT( ok == 0 );
632 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100633 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200634#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +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 Bakker821fb082009-07-12 13:26:42 +0000639}
Paul Bakker33b43f12013-08-20 11:48:36 +0200640/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000641
Paul Bakker33b43f12013-08-20 11:48:36 +0200642/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100643void mbedtls_rsa_pkcs1_encrypt( 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,
Azim Khan5fcca462018-06-29 11:05:32 +0100646 data_t * result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000647{
Paul Bakker821fb082009-07-12 13:26:42 +0000648 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_rsa_context ctx;
Paul Bakker997bbd12011-03-13 15:45:42 +0000650 rnd_pseudo_info rnd_info;
651
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100652 mbedtls_mpi N, E;
653 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
654
Paul Bakker997bbd12011-03-13 15:45:42 +0000655 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000658 memset( output, 0x00, 1000 );
Paul Bakker821fb082009-07-12 13:26:42 +0000659
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100660 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
661 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000662
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100663 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
664 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000666
Paul Bakker42a29bf2009-07-07 20:18:41 +0000667
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100668 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100669 MBEDTLS_RSA_PUBLIC, message_str->len,
670 message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200671 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000672 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000673
Azim Khand30ca132017-06-09 04:32:58 +0100674 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000675 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100676
Paul Bakkerbd51b262014-07-10 15:26:12 +0200677exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100678 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000680}
Paul Bakker33b43f12013-08-20 11:48:36 +0200681/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000682
Paul Bakker33b43f12013-08-20 11:48:36 +0200683/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100684void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100685 int mod, int radix_N, char * input_N,
686 int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100687 data_t * result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000688{
Paul Bakkera6656852010-07-18 19:47:14 +0000689 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000691
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100692 mbedtls_mpi N, E;
693
694 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000696 memset( output, 0x00, 1000 );
Paul Bakkera6656852010-07-18 19:47:14 +0000697
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100698 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
699 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000700
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100701 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
702 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000704
Paul Bakkera6656852010-07-18 19:47:14 +0000705
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100706 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL,
Azim Khand30ca132017-06-09 04:32:58 +0100707 MBEDTLS_RSA_PUBLIC, message_str->len,
708 message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200709 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000710 {
Paul Bakkera6656852010-07-18 19:47:14 +0000711
Azim Khand30ca132017-06-09 04:32:58 +0100712 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000713 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100714
Paul Bakkerbd51b262014-07-10 15:26:12 +0200715exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100716 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000718}
Paul Bakker33b43f12013-08-20 11:48:36 +0200719/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000720
Paul Bakker33b43f12013-08-20 11:48:36 +0200721/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100722void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100723 int mod, int radix_P, char * input_P,
724 int radix_Q, char * input_Q, int radix_N,
725 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100726 int max_output, data_t * result_hex_str,
Azim Khand30ca132017-06-09 04:32:58 +0100727 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000728{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000729 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000731 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200732 rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100733 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000734
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100735 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
736 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000739
Paul Bakker42a29bf2009-07-07 20:18:41 +0000740 memset( output, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200741 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000742
Paul Bakker42a29bf2009-07-07 20:18:41 +0000743
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100744 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
745 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
746 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
747 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000748
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100749 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
750 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100751 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000753
Paul Bakker69998dd2009-07-11 19:15:20 +0000754 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000755
Azim Khand30ca132017-06-09 04:32:58 +0100756 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str->x, output, max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200757 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000758 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000759
Azim Khand30ca132017-06-09 04:32:58 +0100760 TEST_ASSERT( hexcmp( output, result_hex_str->x, output_len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000761 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000762
Paul Bakkerbd51b262014-07-10 15:26:12 +0200763exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100764 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
765 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000767}
Paul Bakker33b43f12013-08-20 11:48:36 +0200768/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000769
Paul Bakker33b43f12013-08-20 11:48:36 +0200770/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100771void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100772 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100773 data_t * result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000774{
Paul Bakker821fb082009-07-12 13:26:42 +0000775 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000777
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100778 mbedtls_mpi N, E;
779
780 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
782 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000783 memset( output, 0x00, 1000 );
Paul Bakker821fb082009-07-12 13:26:42 +0000784
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100785 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
786 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000787
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100788 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
789 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000791
Paul Bakker821fb082009-07-12 13:26:42 +0000792
Azim Khand30ca132017-06-09 04:32:58 +0100793 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200794 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000795 {
Paul Bakker821fb082009-07-12 13:26:42 +0000796
Azim Khand30ca132017-06-09 04:32:58 +0100797 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000798 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100799
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100800 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200802 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100806
807 memset( output, 0x00, 1000 );
Azim Khand30ca132017-06-09 04:32:58 +0100808 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100809 if( result == 0 )
810 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100811
Azim Khand30ca132017-06-09 04:32:58 +0100812 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100813 }
814
Paul Bakkerbd51b262014-07-10 15:26:12 +0200815exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100816 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_rsa_free( &ctx );
818 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000819}
Paul Bakker33b43f12013-08-20 11:48:36 +0200820/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000821
Paul Bakker33b43f12013-08-20 11:48:36 +0200822/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100823void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100824 char * input_P, int radix_Q, char * input_Q,
825 int radix_N, char * input_N, int radix_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100826 char * input_E, data_t * result_hex_str,
Azim Khand30ca132017-06-09 04:32:58 +0100827 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000828{
Paul Bakker821fb082009-07-12 13:26:42 +0000829 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100831 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200832 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200833 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000834
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100835 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
836 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
838 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000839
Paul Bakker548957d2013-08-30 10:30:02 +0200840 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000841
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100842 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
843 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
844 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
845 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000846
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100847 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
848 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100849 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000851
Paul Bakker821fb082009-07-12 13:26:42 +0000852
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200853 /* repeat three times to test updating of blinding values */
854 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000855 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200856 memset( output, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100858 message_str->x, output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200859 if( result == 0 )
860 {
Paul Bakker821fb082009-07-12 13:26:42 +0000861
Azim Khand30ca132017-06-09 04:32:58 +0100862 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200863 }
Paul Bakker821fb082009-07-12 13:26:42 +0000864 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000865
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100866 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200868 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100872
873 memset( output, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100875 message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100876 if( result == 0 )
877 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100878
Azim Khand30ca132017-06-09 04:32:58 +0100879 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx2.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100880 }
881
Paul Bakkerbd51b262014-07-10 15:26:12 +0200882exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100883 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
884 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000887}
Paul Bakker33b43f12013-08-20 11:48:36 +0200888/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000889
Paul Bakker33b43f12013-08-20 11:48:36 +0200890/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100891void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000892{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_rsa_context ctx;
894 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000897}
Paul Bakker33b43f12013-08-20 11:48:36 +0200898/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000899
Paul Bakker33b43f12013-08-20 11:48:36 +0200900/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100901void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
902 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100905 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000906
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100907 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000909
Paul Bakker33b43f12013-08-20 11:48:36 +0200910 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000911 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100912 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000913 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200914 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000915 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100916 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000917 }
918
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100919 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100921
Paul Bakkerbd51b262014-07-10 15:26:12 +0200922exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100923 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000925}
Paul Bakker33b43f12013-08-20 11:48:36 +0200926/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000927
Paul Bakker33b43f12013-08-20 11:48:36 +0200928/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100929void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
930 int radix_Q, char * input_Q, int radix_N,
931 char * input_N, int radix_E, char * input_E,
932 int radix_D, char * input_D, int radix_DP,
933 char * input_DP, int radix_DQ,
934 char * input_DQ, int radix_QP,
935 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000936{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000940
Paul Bakker33b43f12013-08-20 11:48:36 +0200941 ctx.len = mod / 8;
942 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000945 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200946 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000949 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200950 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000953 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200954 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000957 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200958 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000961 }
Hanno Becker131134f2017-08-23 08:31:07 +0100962#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200963 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000966 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200967 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000970 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200971 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000974 }
Hanno Becker131134f2017-08-23 08:31:07 +0100975#else
976 ((void) radix_DP); ((void) input_DP);
977 ((void) radix_DQ); ((void) input_DQ);
978 ((void) radix_QP); ((void) input_QP);
979#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100982
Paul Bakkerbd51b262014-07-10 15:26:12 +0200983exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000985}
Paul Bakker33b43f12013-08-20 11:48:36 +0200986/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000987
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100988/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100989void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
990 int radix_Epub, char * input_Epub, int radix_P,
991 char * input_P, int radix_Q, char * input_Q,
992 int radix_N, char * input_N, int radix_E,
993 char * input_E, int radix_D, char * input_D,
994 int radix_DP, char * input_DP, int radix_DQ,
995 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100996 int result )
997{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1001 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001002
1003 pub.len = mod / 8;
1004 prv.len = mod / 8;
1005
1006 if( strlen( input_Npub ) )
1007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001009 }
1010 if( strlen( input_Epub ) )
1011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001013 }
1014
1015 if( strlen( input_P ) )
1016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001018 }
1019 if( strlen( input_Q ) )
1020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001022 }
1023 if( strlen( input_N ) )
1024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001026 }
1027 if( strlen( input_E ) )
1028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001030 }
1031 if( strlen( input_D ) )
1032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001034 }
Hanno Becker131134f2017-08-23 08:31:07 +01001035#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001036 if( strlen( input_DP ) )
1037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001039 }
1040 if( strlen( input_DQ ) )
1041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001043 }
1044 if( strlen( input_QP ) )
1045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001047 }
Hanno Becker131134f2017-08-23 08:31:07 +01001048#else
1049 ((void) radix_DP); ((void) input_DP);
1050 ((void) radix_DQ); ((void) input_DQ);
1051 ((void) radix_QP); ((void) input_QP);
1052#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001055
1056exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_rsa_free( &pub );
1058 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001059}
1060/* END_CASE */
1061
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001062/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_rsa_context ctx;
1066 mbedtls_entropy_context entropy;
1067 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001068 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001069
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001070 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001072 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001073
Hanno Beckera47023e2017-12-22 17:08:03 +00001074 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1075 &entropy, (const unsigned char *) pers,
1076 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001079 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001082 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001083 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001084
Paul Bakkerbd51b262014-07-10 15:26:12 +02001085exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 mbedtls_rsa_free( &ctx );
1087 mbedtls_ctr_drbg_free( &ctr_drbg );
1088 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001089}
Paul Bakker33b43f12013-08-20 11:48:36 +02001090/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001091
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001092/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001093void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001094 int radix_D, char *input_D,
1095 int radix_E, char *input_E,
1096 int radix_P, char *output_P,
1097 int radix_Q, char *output_Q,
1098 int corrupt, int result )
1099{
1100 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1101
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001102 mbedtls_mpi_init( &N );
1103 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1104 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1105 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1106
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001107 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1108 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1109 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1110 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1111 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1112
1113 if( corrupt )
1114 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1115
1116 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001117 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001118
1119 if( !corrupt )
1120 {
1121 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1122 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1123 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1124 }
1125
1126exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001127 mbedtls_mpi_free( &N );
1128 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1129 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1130 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001131}
1132/* END_CASE */
1133
Hanno Becker6b4ce492017-08-23 11:00:21 +01001134/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001135void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1136 int radix_Q, char *input_Q,
1137 int radix_E, char *input_E,
1138 int radix_D, char *output_D,
1139 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001140{
1141 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1142
1143 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1144 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1145 mbedtls_mpi_init( &E );
1146 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1147
1148 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1149 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1150 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1151 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1152
1153 if( corrupt )
1154 {
1155 /* Make E even */
1156 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1157 }
1158
1159 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001160 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1161 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001162
1163 if( !corrupt )
1164 {
1165 /*
1166 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1167 */
1168
1169 /* Replace P,Q by P-1, Q-1 */
1170 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1171 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1172
1173 /* Check D == Dp modulo P-1 */
1174 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1175 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1176 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1177
1178 /* Check D == Dp modulo Q-1 */
1179 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1180 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1181 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1182 }
1183
1184exit:
1185
1186 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1187 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1188 mbedtls_mpi_free( &E );
1189 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1190}
1191/* END_CASE */
1192
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001193/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001194void mbedtls_rsa_import( int radix_N, char *input_N,
1195 int radix_P, char *input_P,
1196 int radix_Q, char *input_Q,
1197 int radix_D, char *input_D,
1198 int radix_E, char *input_E,
1199 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001200 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001201 int res_check,
1202 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001203{
1204 mbedtls_mpi N, P, Q, D, E;
1205 mbedtls_rsa_context ctx;
1206
Hanno Beckere1582a82017-09-29 11:51:05 +01001207 /* Buffers used for encryption-decryption test */
1208 unsigned char *buf_orig = NULL;
1209 unsigned char *buf_enc = NULL;
1210 unsigned char *buf_dec = NULL;
1211
Hanno Beckerc77ab892017-08-23 11:01:06 +01001212 mbedtls_entropy_context entropy;
1213 mbedtls_ctr_drbg_context ctr_drbg;
1214 const char *pers = "test_suite_rsa";
1215
Hanno Becker4d6e8342017-09-29 11:50:18 +01001216 const int have_N = ( strlen( input_N ) > 0 );
1217 const int have_P = ( strlen( input_P ) > 0 );
1218 const int have_Q = ( strlen( input_Q ) > 0 );
1219 const int have_D = ( strlen( input_D ) > 0 );
1220 const int have_E = ( strlen( input_E ) > 0 );
1221
Hanno Beckerc77ab892017-08-23 11:01:06 +01001222 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001223 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001224 mbedtls_rsa_init( &ctx, 0, 0 );
1225
1226 mbedtls_mpi_init( &N );
1227 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1228 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1229
Hanno Beckerd4d60572018-01-10 07:12:01 +00001230 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1231 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1232
Hanno Becker4d6e8342017-09-29 11:50:18 +01001233 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001234 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1235
Hanno Becker4d6e8342017-09-29 11:50:18 +01001236 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001237 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1238
Hanno Becker4d6e8342017-09-29 11:50:18 +01001239 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001240 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1241
Hanno Becker4d6e8342017-09-29 11:50:18 +01001242 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001243 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1244
Hanno Becker4d6e8342017-09-29 11:50:18 +01001245 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001246 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1247
1248 if( !successive )
1249 {
1250 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001251 have_N ? &N : NULL,
1252 have_P ? &P : NULL,
1253 have_Q ? &Q : NULL,
1254 have_D ? &D : NULL,
1255 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001256 }
1257 else
1258 {
1259 /* Import N, P, Q, D, E separately.
1260 * This should make no functional difference. */
1261
1262 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001263 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001264 NULL, NULL, NULL, NULL ) == 0 );
1265
1266 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1267 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001268 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001269 NULL, NULL, NULL ) == 0 );
1270
1271 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1272 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001273 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001274 NULL, NULL ) == 0 );
1275
1276 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1277 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001278 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001279 NULL ) == 0 );
1280
1281 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1282 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001283 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001284 }
1285
Hanno Becker04877a42017-10-11 10:01:33 +01001286 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001287
Hanno Beckere1582a82017-09-29 11:51:05 +01001288 /* On expected success, perform some public and private
1289 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001290 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001291 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001292 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001293 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1294 else
1295 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1296
1297 if( res_check != 0 )
1298 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001299
1300 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1301 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1302 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1303 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1304 goto exit;
1305
1306 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1307 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1308
1309 /* Make sure the number we're generating is smaller than the modulus */
1310 buf_orig[0] = 0x00;
1311
1312 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1313
1314 if( is_priv )
1315 {
1316 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1317 &ctr_drbg, buf_enc,
1318 buf_dec ) == 0 );
1319
1320 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1321 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1322 }
1323 }
1324
Hanno Beckerc77ab892017-08-23 11:01:06 +01001325exit:
1326
Hanno Beckere1582a82017-09-29 11:51:05 +01001327 mbedtls_free( buf_orig );
1328 mbedtls_free( buf_enc );
1329 mbedtls_free( buf_dec );
1330
Hanno Beckerc77ab892017-08-23 11:01:06 +01001331 mbedtls_rsa_free( &ctx );
1332
1333 mbedtls_ctr_drbg_free( &ctr_drbg );
1334 mbedtls_entropy_free( &entropy );
1335
1336 mbedtls_mpi_free( &N );
1337 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1338 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1339}
1340/* END_CASE */
1341
Hanno Becker417f2d62017-08-23 11:44:51 +01001342/* BEGIN_CASE */
1343void mbedtls_rsa_export( int radix_N, char *input_N,
1344 int radix_P, char *input_P,
1345 int radix_Q, char *input_Q,
1346 int radix_D, char *input_D,
1347 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001348 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001349 int successive )
1350{
1351 /* Original MPI's with which we set up the RSA context */
1352 mbedtls_mpi N, P, Q, D, E;
1353
1354 /* Exported MPI's */
1355 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1356
1357 const int have_N = ( strlen( input_N ) > 0 );
1358 const int have_P = ( strlen( input_P ) > 0 );
1359 const int have_Q = ( strlen( input_Q ) > 0 );
1360 const int have_D = ( strlen( input_D ) > 0 );
1361 const int have_E = ( strlen( input_E ) > 0 );
1362
Hanno Becker417f2d62017-08-23 11:44:51 +01001363 mbedtls_rsa_context ctx;
1364
1365 mbedtls_rsa_init( &ctx, 0, 0 );
1366
1367 mbedtls_mpi_init( &N );
1368 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1369 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1370
1371 mbedtls_mpi_init( &Ne );
1372 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1373 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1374
1375 /* Setup RSA context */
1376
1377 if( have_N )
1378 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1379
1380 if( have_P )
1381 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1382
1383 if( have_Q )
1384 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1385
1386 if( have_D )
1387 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1388
1389 if( have_E )
1390 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1391
1392 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1393 strlen( input_N ) ? &N : NULL,
1394 strlen( input_P ) ? &P : NULL,
1395 strlen( input_Q ) ? &Q : NULL,
1396 strlen( input_D ) ? &D : NULL,
1397 strlen( input_E ) ? &E : NULL ) == 0 );
1398
Hanno Becker7f25f852017-10-10 16:56:22 +01001399 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001400
1401 /*
1402 * Export parameters and compare to original ones.
1403 */
1404
1405 /* N and E must always be present. */
1406 if( !successive )
1407 {
1408 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1409 }
1410 else
1411 {
1412 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1413 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1414 }
1415 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1416 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1417
1418 /* If we were providing enough information to setup a complete private context,
1419 * we expect to be able to export all core parameters. */
1420
1421 if( is_priv )
1422 {
1423 if( !successive )
1424 {
1425 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1426 &De, NULL ) == 0 );
1427 }
1428 else
1429 {
1430 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1431 NULL, NULL ) == 0 );
1432 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1433 NULL, NULL ) == 0 );
1434 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1435 &De, NULL ) == 0 );
1436 }
1437
1438 if( have_P )
1439 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1440
1441 if( have_Q )
1442 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1443
1444 if( have_D )
1445 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1446
1447 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001448 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1449 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001450 }
1451
1452exit:
1453
1454 mbedtls_rsa_free( &ctx );
1455
1456 mbedtls_mpi_free( &N );
1457 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1458 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1459
1460 mbedtls_mpi_free( &Ne );
1461 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1462 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1463}
1464/* END_CASE */
1465
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001466/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Becker750e8b42017-08-25 07:54:27 +01001467void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1468 int radix_P, char *input_P,
1469 int radix_Q, char *input_Q,
1470 int radix_D, char *input_D,
1471 int radix_E, char *input_E,
1472 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001473{
1474 /* Original MPI's with which we set up the RSA context */
1475 mbedtls_mpi N, P, Q, D, E;
1476
1477 const int have_N = ( strlen( input_N ) > 0 );
1478 const int have_P = ( strlen( input_P ) > 0 );
1479 const int have_Q = ( strlen( input_Q ) > 0 );
1480 const int have_D = ( strlen( input_D ) > 0 );
1481 const int have_E = ( strlen( input_E ) > 0 );
1482
1483 mbedtls_entropy_context entropy;
1484 mbedtls_ctr_drbg_context ctr_drbg;
1485 const char *pers = "test_suite_rsa";
1486
1487 mbedtls_mpi_init( &N );
1488 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1489 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1490
1491 mbedtls_ctr_drbg_init( &ctr_drbg );
1492 mbedtls_entropy_init( &entropy );
1493 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1494 &entropy, (const unsigned char *) pers,
1495 strlen( pers ) ) == 0 );
1496
1497 if( have_N )
1498 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1499
1500 if( have_P )
1501 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1502
1503 if( have_Q )
1504 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1505
1506 if( have_D )
1507 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1508
1509 if( have_E )
1510 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1511
Hanno Becker750e8b42017-08-25 07:54:27 +01001512 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1513 have_P ? &P : NULL,
1514 have_Q ? &Q : NULL,
1515 have_D ? &D : NULL,
1516 have_E ? &E : NULL,
1517 prng ? mbedtls_ctr_drbg_random : NULL,
1518 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001519exit:
1520
1521 mbedtls_ctr_drbg_free( &ctr_drbg );
1522 mbedtls_entropy_free( &entropy );
1523
1524 mbedtls_mpi_free( &N );
1525 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1526 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1527}
1528/* END_CASE */
1529
Hanno Beckerc77ab892017-08-23 11:01:06 +01001530/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001531void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1532 data_t *input_Q, data_t *input_D,
1533 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001534 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001535{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001536 /* Exported buffers */
Azim Khand30ca132017-06-09 04:32:58 +01001537 unsigned char bufNe[1000];
1538 unsigned char bufPe[1000];
1539 unsigned char bufQe[1000];
1540 unsigned char bufDe[1000];
1541 unsigned char bufEe[1000];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001542
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001543 mbedtls_rsa_context ctx;
1544
1545 mbedtls_rsa_init( &ctx, 0, 0 );
1546
1547 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001548 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001549 input_N->len ? input_N->x : NULL, input_N->len,
1550 input_P->len ? input_P->x : NULL, input_P->len,
1551 input_Q->len ? input_Q->x : NULL, input_Q->len,
1552 input_D->len ? input_D->x : NULL, input_D->len,
1553 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001554
Hanno Becker7f25f852017-10-10 16:56:22 +01001555 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001556
1557 /*
1558 * Export parameters and compare to original ones.
1559 */
1560
1561 /* N and E must always be present. */
1562 if( !successive )
1563 {
Azim Khand30ca132017-06-09 04:32:58 +01001564 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001565 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001566 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001567 }
1568 else
1569 {
Azim Khand30ca132017-06-09 04:32:58 +01001570 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001571 NULL, 0, NULL, 0, NULL, 0,
1572 NULL, 0 ) == 0 );
1573 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1574 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001575 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001576 }
Azim Khand30ca132017-06-09 04:32:58 +01001577 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1578 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001579
1580 /* If we were providing enough information to setup a complete private context,
1581 * we expect to be able to export all core parameters. */
1582
1583 if( is_priv )
1584 {
1585 if( !successive )
1586 {
1587 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001588 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1589 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1590 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001591 NULL, 0 ) == 0 );
1592 }
1593 else
1594 {
1595 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001596 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001597 NULL, 0, NULL, 0,
1598 NULL, 0 ) == 0 );
1599
1600 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001601 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001602 NULL, 0, NULL, 0 ) == 0 );
1603
Azim Khand30ca132017-06-09 04:32:58 +01001604 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1605 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001606 NULL, 0 ) == 0 );
1607 }
1608
Azim Khand30ca132017-06-09 04:32:58 +01001609 if( input_P->len )
1610 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001611
Azim Khand30ca132017-06-09 04:32:58 +01001612 if( input_Q->len )
1613 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001614
Azim Khand30ca132017-06-09 04:32:58 +01001615 if( input_D->len )
1616 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001617
1618 }
1619
1620exit:
1621 mbedtls_rsa_free( &ctx );
1622}
1623/* END_CASE */
1624
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001625/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001626void mbedtls_rsa_import_raw( data_t *input_N,
1627 data_t *input_P, data_t *input_Q,
1628 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001629 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001630 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001631 int res_check,
1632 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001633{
Hanno Beckere1582a82017-09-29 11:51:05 +01001634 /* Buffers used for encryption-decryption test */
1635 unsigned char *buf_orig = NULL;
1636 unsigned char *buf_enc = NULL;
1637 unsigned char *buf_dec = NULL;
1638
Hanno Beckerc77ab892017-08-23 11:01:06 +01001639 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001640 mbedtls_entropy_context entropy;
1641 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001642
Hanno Beckerc77ab892017-08-23 11:01:06 +01001643 const char *pers = "test_suite_rsa";
1644
1645 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001646 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001647 mbedtls_rsa_init( &ctx, 0, 0 );
1648
Hanno Beckerc77ab892017-08-23 11:01:06 +01001649 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1650 &entropy, (const unsigned char *) pers,
1651 strlen( pers ) ) == 0 );
1652
Hanno Beckerc77ab892017-08-23 11:01:06 +01001653 if( !successive )
1654 {
1655 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001656 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1657 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1658 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1659 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1660 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001661 }
1662 else
1663 {
1664 /* Import N, P, Q, D, E separately.
1665 * This should make no functional difference. */
1666
1667 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001668 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001669 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1670
1671 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1672 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001673 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001674 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1675
1676 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1677 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001678 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001679 NULL, 0, NULL, 0 ) == 0 );
1680
1681 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1682 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001683 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001684 NULL, 0 ) == 0 );
1685
1686 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1687 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001688 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001689 }
1690
Hanno Becker04877a42017-10-11 10:01:33 +01001691 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001692
Hanno Beckere1582a82017-09-29 11:51:05 +01001693 /* On expected success, perform some public and private
1694 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001695 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001696 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001697 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001698 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1699 else
1700 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1701
1702 if( res_check != 0 )
1703 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001704
1705 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1706 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1707 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1708 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1709 goto exit;
1710
1711 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1712 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1713
1714 /* Make sure the number we're generating is smaller than the modulus */
1715 buf_orig[0] = 0x00;
1716
1717 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1718
1719 if( is_priv )
1720 {
1721 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1722 &ctr_drbg, buf_enc,
1723 buf_dec ) == 0 );
1724
1725 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1726 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1727 }
1728 }
1729
Hanno Beckerc77ab892017-08-23 11:01:06 +01001730exit:
1731
Hanno Becker3f3ae852017-10-02 10:08:39 +01001732 mbedtls_free( buf_orig );
1733 mbedtls_free( buf_enc );
1734 mbedtls_free( buf_dec );
1735
Hanno Beckerc77ab892017-08-23 11:01:06 +01001736 mbedtls_rsa_free( &ctx );
1737
1738 mbedtls_ctr_drbg_free( &ctr_drbg );
1739 mbedtls_entropy_free( &entropy );
1740
1741}
1742/* END_CASE */
1743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001745void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001746{
Andres AG93012e82016-09-09 09:10:28 +01001747 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001748}
Paul Bakker33b43f12013-08-20 11:48:36 +02001749/* END_CASE */