blob: a0ab86cb9ee4015b954dac37b3f66cba666dbe7e [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,
55 mbedtls_rsa_export( NULL, NULL, NULL,
56 NULL, NULL, NULL ) );
57 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
58 mbedtls_rsa_export_raw( NULL,
59 NULL, 0,
60 NULL, 0,
61 NULL, 0,
62 NULL, 0,
63 NULL, 0 ) );
64 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
65 mbedtls_rsa_export_crt( NULL, NULL, NULL, NULL ) );
66
67 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( NULL,
68 valid_padding, 0 ) );
69 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( &ctx,
70 invalid_padding, 0 ) );
71
72 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
73 mbedtls_rsa_gen_key( NULL, NULL, NULL, 0, 0 ) );
74
75 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
76 mbedtls_rsa_check_pubkey( NULL ) );
77 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
78 mbedtls_rsa_check_privkey( NULL ) );
79
80 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
81 mbedtls_rsa_check_pub_priv( NULL, &ctx ) );
82 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
83 mbedtls_rsa_check_pub_priv( &ctx, NULL ) );
84
85 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
86 mbedtls_rsa_public( NULL, buf, buf ) );
87 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
88 mbedtls_rsa_public( &ctx, NULL, buf ) );
89 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
90 mbedtls_rsa_public( &ctx, buf, NULL ) );
91
92 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
93 mbedtls_rsa_private( NULL, NULL, NULL,
94 buf, buf ) );
95 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
96 mbedtls_rsa_private( &ctx, NULL, NULL,
97 NULL, buf ) );
98 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
99 mbedtls_rsa_private( &ctx, NULL, NULL,
100 buf, NULL ) );
101
102 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
103 mbedtls_rsa_pkcs1_encrypt( NULL, NULL, NULL,
104 valid_mode,
105 sizeof( buf ), buf,
106 buf ) );
107 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
108 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
109 invalid_mode,
110 sizeof( buf ), buf,
111 buf ) );
112 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
113 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
114 valid_mode,
115 sizeof( buf ), NULL,
116 buf ) );
117 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
118 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
119 valid_mode,
120 sizeof( buf ), buf,
121 NULL ) );
122
123 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
124 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
125 NULL,
126 valid_mode,
127 sizeof( buf ), buf,
128 buf ) );
129 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
130 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
131 NULL,
132 invalid_mode,
133 sizeof( buf ), buf,
134 buf ) );
135 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
136 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
137 NULL,
138 valid_mode,
139 sizeof( buf ), NULL,
140 buf ) );
141 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
142 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
143 NULL,
144 valid_mode,
145 sizeof( buf ), buf,
146 NULL ) );
147
148 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
149 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
150 valid_mode,
151 buf, sizeof( buf ),
152 sizeof( buf ), buf,
153 buf ) );
154 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
155 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
156 invalid_mode,
157 buf, sizeof( buf ),
158 sizeof( buf ), buf,
159 buf ) );
160 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
161 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
162 valid_mode,
163 NULL, sizeof( buf ),
164 sizeof( buf ), buf,
165 buf ) );
166 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
167 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
168 valid_mode,
169 buf, sizeof( buf ),
170 sizeof( buf ), NULL,
171 buf ) );
172 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
173 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
174 valid_mode,
175 buf, sizeof( buf ),
176 sizeof( buf ), buf,
177 NULL ) );
178
179 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
180 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
181 valid_mode, &olen,
182 buf, buf, 42 ) );
183 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
184 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
185 invalid_mode, &olen,
186 buf, buf, 42 ) );
187 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
188 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
189 valid_mode, NULL,
190 buf, buf, 42 ) );
191 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
192 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
193 valid_mode, &olen,
194 NULL, buf, 42 ) );
195 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
196 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
197 valid_mode, &olen,
198 buf, NULL, 42 ) );
199
200 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
201 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
202 NULL,
203 valid_mode, &olen,
204 buf, buf, 42 ) );
205 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
206 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
207 NULL,
208 invalid_mode, &olen,
209 buf, buf, 42 ) );
210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
211 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
212 NULL,
213 valid_mode, NULL,
214 buf, buf, 42 ) );
215 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
216 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
217 NULL,
218 valid_mode, &olen,
219 NULL, buf, 42 ) );
220 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
221 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
222 NULL,
223 valid_mode, &olen,
224 buf, NULL, 42 ) );
225
226 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
227 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
228 valid_mode,
229 buf, sizeof( buf ),
230 &olen,
231 buf, buf, 42 ) );
232 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
233 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
234 invalid_mode,
235 buf, sizeof( buf ),
236 &olen,
237 buf, buf, 42 ) );
238 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
239 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
240 valid_mode,
241 NULL, sizeof( buf ),
242 NULL,
243 buf, buf, 42 ) );
244 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
245 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
246 valid_mode,
247 buf, sizeof( buf ),
248 &olen,
249 NULL, buf, 42 ) );
250 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
251 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
252 valid_mode,
253 buf, sizeof( buf ),
254 &olen,
255 buf, NULL, 42 ) );
256
257 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
258 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
259 valid_mode,
260 0, sizeof( buf ), buf,
261 buf ) );
262 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
263 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
264 invalid_mode,
265 0, sizeof( buf ), buf,
266 buf ) );
267 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
268 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
269 valid_mode,
270 0, sizeof( buf ), NULL,
271 buf ) );
272 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
273 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
274 valid_mode,
275 0, sizeof( buf ), buf,
276 NULL ) );
277
278 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
279 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
280 valid_mode,
281 0, sizeof( buf ), buf,
282 buf ) );
283 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
284 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
285 invalid_mode,
286 0, sizeof( buf ), buf,
287 buf ) );
288 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
289 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
290 valid_mode,
291 0, sizeof( buf ), NULL,
292 buf ) );
293 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
294 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
295 valid_mode,
296 0, sizeof( buf ), buf,
297 NULL ) );
298
299 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
300 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
301 valid_mode,
302 0, sizeof( buf ), buf,
303 buf ) );
304 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
305 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
306 invalid_mode,
307 0, sizeof( buf ), buf,
308 buf ) );
309 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
310 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
311 valid_mode,
312 0, sizeof( buf ), NULL,
313 buf ) );
314 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
315 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
316 valid_mode,
317 0, sizeof( buf ), buf,
318 NULL ) );
319
320 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
321 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
322 valid_mode,
323 0, sizeof( buf ), buf,
324 buf ) );
325 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
326 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
327 invalid_mode,
328 0, sizeof( buf ), buf,
329 buf ) );
330 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
331 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
332 valid_mode,
333 0, sizeof( buf ), NULL,
334 buf ) );
335 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
336 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
337 valid_mode,
338 0, sizeof( buf ), buf,
339 NULL ) );
340
341 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
342 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
343 NULL,
344 valid_mode,
345 0, sizeof( buf ), buf,
346 buf ) );
347 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
348 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
349 NULL,
350 invalid_mode,
351 0, sizeof( buf ), buf,
352 buf ) );
353 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
354 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
355 NULL,
356 valid_mode,
357 0, sizeof( buf ),
358 NULL, buf ) );
359 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
360 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
361 NULL,
362 valid_mode,
363 0, sizeof( buf ), buf,
364 NULL ) );
365
366 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
367 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
368 valid_mode,
369 0, sizeof( buf ),
370 buf, buf ) );
371 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
372 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
373 invalid_mode,
374 0, sizeof( buf ),
375 buf, buf ) );
376 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
377 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
378 valid_mode,
379 0, sizeof( buf ),
380 NULL, buf ) );
381 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
382 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
383 valid_mode,
384 0, sizeof( buf ),
385 buf, NULL ) );
386
387 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
388 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
389 valid_mode,
390 0, sizeof( buf ),
391 buf,
392 0, 0,
393 buf ) );
394 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
395 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
396 invalid_mode,
397 0, sizeof( buf ),
398 buf,
399 0, 0,
400 buf ) );
401 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
402 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
403 valid_mode,
404 0, sizeof( buf ),
405 NULL, 0, 0,
406 buf ) );
407 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
408 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
409 valid_mode,
410 0, sizeof( buf ),
411 buf, 0, 0,
412 NULL ) );
413
414 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
415 mbedtls_rsa_copy( NULL, &ctx ) );
416 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
417 mbedtls_rsa_copy( &ctx, NULL ) );
418
419exit:
420 return;
421}
422/* END_CASE */
423
Paul Bakker33b43f12013-08-20 11:48:36 +0200424/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100425void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100426 int digest, int mod, int radix_P, char * input_P,
427 int radix_Q, char * input_Q, int radix_N,
428 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100429 data_t * result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000430{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000431 unsigned char hash_result[1000];
432 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100434 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200435 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000436
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100437 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
438 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000440
Paul Bakker42a29bf2009-07-07 20:18:41 +0000441 memset( hash_result, 0x00, 1000 );
442 memset( output, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200443 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000444
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100445 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
446 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
447 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
448 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000449
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100450 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
451 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100452 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000454
Paul Bakker42a29bf2009-07-07 20:18:41 +0000455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100457 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 +0000458
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100459 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
460 MBEDTLS_RSA_PRIVATE, digest, 0,
461 hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200462 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000463 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000464
Azim Khand30ca132017-06-09 04:32:58 +0100465 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000466 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000467
Paul Bakkerbd51b262014-07-10 15:26:12 +0200468exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100469 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
470 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000472}
Paul Bakker33b43f12013-08-20 11:48:36 +0200473/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000474
Paul Bakker33b43f12013-08-20 11:48:36 +0200475/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100476void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100477 int digest, int mod, int radix_N,
478 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100479 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000480{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000481 unsigned char hash_result[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000483
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100484 mbedtls_mpi N, E;
485
486 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000488 memset( hash_result, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000489
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100490 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
491 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
492 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
493 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000495
Paul Bakker42a29bf2009-07-07 20:18:41 +0000496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100498 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 +0000499
Azim Khand30ca132017-06-09 04:32:58 +0100500 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 +0100501
Paul Bakkerbd51b262014-07-10 15:26:12 +0200502exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100503 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000505}
Paul Bakker33b43f12013-08-20 11:48:36 +0200506/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000507
Paul Bakker821fb082009-07-12 13:26:42 +0000508
Paul Bakker33b43f12013-08-20 11:48:36 +0200509/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100510void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100511 int padding_mode, int mod, int radix_P,
512 char * input_P, int radix_Q, char * input_Q,
513 int radix_N, char * input_N, int radix_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100514 char * input_E, data_t * result_hex_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000515{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000516 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100518 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200519 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100522 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
523 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000524
Paul Bakker42a29bf2009-07-07 20:18:41 +0000525 memset( output, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200526 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000527
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100528 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
529 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
530 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
531 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000532
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100533 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
534 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100535 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000537
Paul Bakker821fb082009-07-12 13:26:42 +0000538
Hanno Becker8fd55482017-08-23 14:07:48 +0100539 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
540 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
Azim Khand30ca132017-06-09 04:32:58 +0100541 hash_result->len, hash_result->x,
542 output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000543
Paul Bakker821fb082009-07-12 13:26:42 +0000544
Azim Khand30ca132017-06-09 04:32:58 +0100545 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000546
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200547#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100548 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100550 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100551 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100552 memset( output, 0x00, 1000 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100553
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100554 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Azim Khand30ca132017-06-09 04:32:58 +0100556 hash_result->len, hash_result->x, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100557
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100558#if !defined(MBEDTLS_RSA_ALT)
559 TEST_ASSERT( res == 0 );
560#else
561 TEST_ASSERT( ( res == 0 ) ||
562 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
563#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100564
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100565 if( res == 0 )
566 {
Azim Khand30ca132017-06-09 04:32:58 +0100567 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100568 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100569 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200570#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100571
Paul Bakkerbd51b262014-07-10 15:26:12 +0200572exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100573 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
574 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000577}
Paul Bakker33b43f12013-08-20 11:48:36 +0200578/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000579
Paul Bakker33b43f12013-08-20 11:48:36 +0200580/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100581void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200582 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100583 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100584 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000585{
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100586 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000588
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100589 mbedtls_mpi N, E;
590 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100593 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000594
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100595 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
596 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000597
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100598 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
599 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000601
Paul Bakker821fb082009-07-12 13:26:42 +0000602
Azim Khand30ca132017-06-09 04:32:58 +0100603 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 +0100604
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200605#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100606 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100608 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100609 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100610 int ok;
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200611 size_t olen;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100612
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100613 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Azim Khand30ca132017-06-09 04:32:58 +0100615 &olen, result_str->x, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100616
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100617#if !defined(MBEDTLS_RSA_ALT)
618 TEST_ASSERT( res == 0 );
619#else
620 TEST_ASSERT( ( res == 0 ) ||
621 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
622#endif
623
624 if( res == 0 )
625 {
Azim Khand30ca132017-06-09 04:32:58 +0100626 ok = olen == hash_result->len && memcmp( output, hash_result->x, olen ) == 0;
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100627 if( correct == 0 )
628 TEST_ASSERT( ok == 1 );
629 else
630 TEST_ASSERT( ok == 0 );
631 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100632 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200633#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100634
Paul Bakkerbd51b262014-07-10 15:26:12 +0200635exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100636 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000638}
Paul Bakker33b43f12013-08-20 11:48:36 +0200639/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000640
Paul Bakker33b43f12013-08-20 11:48:36 +0200641/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100642void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100643 int mod, int radix_N, char * input_N,
644 int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100645 data_t * result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000646{
Paul Bakker821fb082009-07-12 13:26:42 +0000647 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_rsa_context ctx;
Paul Bakker997bbd12011-03-13 15:45:42 +0000649 rnd_pseudo_info rnd_info;
650
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100651 mbedtls_mpi N, E;
652 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
653
Paul Bakker997bbd12011-03-13 15:45:42 +0000654 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000657 memset( output, 0x00, 1000 );
Paul Bakker821fb082009-07-12 13:26:42 +0000658
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100659 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
660 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000661
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100662 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
663 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000665
Paul Bakker42a29bf2009-07-07 20:18:41 +0000666
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100667 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100668 MBEDTLS_RSA_PUBLIC, message_str->len,
669 message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200670 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000671 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000672
Azim Khand30ca132017-06-09 04:32:58 +0100673 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +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 Bakker42a29bf2009-07-07 20:18:41 +0000679}
Paul Bakker33b43f12013-08-20 11:48:36 +0200680/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000681
Paul Bakker33b43f12013-08-20 11:48:36 +0200682/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100683void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100684 int mod, int radix_N, char * input_N,
685 int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100686 data_t * result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000687{
Paul Bakkera6656852010-07-18 19:47:14 +0000688 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000690
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100691 mbedtls_mpi N, E;
692
693 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000695 memset( output, 0x00, 1000 );
Paul Bakkera6656852010-07-18 19:47:14 +0000696
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100697 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
698 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000699
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100700 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
701 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000703
Paul Bakkera6656852010-07-18 19:47:14 +0000704
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100705 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL,
Azim Khand30ca132017-06-09 04:32:58 +0100706 MBEDTLS_RSA_PUBLIC, message_str->len,
707 message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200708 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000709 {
Paul Bakkera6656852010-07-18 19:47:14 +0000710
Azim Khand30ca132017-06-09 04:32:58 +0100711 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000712 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100713
Paul Bakkerbd51b262014-07-10 15:26:12 +0200714exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100715 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000717}
Paul Bakker33b43f12013-08-20 11:48:36 +0200718/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000719
Paul Bakker33b43f12013-08-20 11:48:36 +0200720/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100721void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100722 int mod, int radix_P, char * input_P,
723 int radix_Q, char * input_Q, int radix_N,
724 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100725 int max_output, data_t * result_hex_str,
Azim Khand30ca132017-06-09 04:32:58 +0100726 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000727{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000728 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000730 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200731 rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100732 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000733
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100734 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
735 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000738
Paul Bakker42a29bf2009-07-07 20:18:41 +0000739 memset( output, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200740 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000741
Paul Bakker42a29bf2009-07-07 20:18:41 +0000742
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100743 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
744 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
745 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
746 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000747
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100748 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
749 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100750 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000752
Paul Bakker69998dd2009-07-11 19:15:20 +0000753 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000754
Azim Khand30ca132017-06-09 04:32:58 +0100755 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 +0200756 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000757 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000758
Azim Khand30ca132017-06-09 04:32:58 +0100759 TEST_ASSERT( hexcmp( output, result_hex_str->x, output_len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000760 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000761
Paul Bakkerbd51b262014-07-10 15:26:12 +0200762exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100763 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
764 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000766}
Paul Bakker33b43f12013-08-20 11:48:36 +0200767/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000768
Paul Bakker33b43f12013-08-20 11:48:36 +0200769/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100770void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100771 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100772 data_t * result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000773{
Paul Bakker821fb082009-07-12 13:26:42 +0000774 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000776
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100777 mbedtls_mpi N, E;
778
779 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
781 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000782 memset( output, 0x00, 1000 );
Paul Bakker821fb082009-07-12 13:26:42 +0000783
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100784 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
785 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000786
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100787 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
788 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000790
Paul Bakker821fb082009-07-12 13:26:42 +0000791
Azim Khand30ca132017-06-09 04:32:58 +0100792 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200793 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000794 {
Paul Bakker821fb082009-07-12 13:26:42 +0000795
Azim Khand30ca132017-06-09 04:32:58 +0100796 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000797 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100798
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100799 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200801 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100805
806 memset( output, 0x00, 1000 );
Azim Khand30ca132017-06-09 04:32:58 +0100807 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100808 if( result == 0 )
809 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100810
Azim Khand30ca132017-06-09 04:32:58 +0100811 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100812 }
813
Paul Bakkerbd51b262014-07-10 15:26:12 +0200814exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100815 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_rsa_free( &ctx );
817 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000818}
Paul Bakker33b43f12013-08-20 11:48:36 +0200819/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000820
Paul Bakker33b43f12013-08-20 11:48:36 +0200821/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100822void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100823 char * input_P, int radix_Q, char * input_Q,
824 int radix_N, char * input_N, int radix_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100825 char * input_E, data_t * result_hex_str,
Azim Khand30ca132017-06-09 04:32:58 +0100826 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000827{
Paul Bakker821fb082009-07-12 13:26:42 +0000828 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100830 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200831 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200832 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000833
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100834 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
835 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
837 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000838
Paul Bakker548957d2013-08-30 10:30:02 +0200839 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000840
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100841 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
842 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
843 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
844 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000845
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100846 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
847 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100848 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000850
Paul Bakker821fb082009-07-12 13:26:42 +0000851
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200852 /* repeat three times to test updating of blinding values */
853 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000854 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200855 memset( output, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100857 message_str->x, output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200858 if( result == 0 )
859 {
Paul Bakker821fb082009-07-12 13:26:42 +0000860
Azim Khand30ca132017-06-09 04:32:58 +0100861 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200862 }
Paul Bakker821fb082009-07-12 13:26:42 +0000863 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000864
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100865 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200867 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100871
872 memset( output, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100874 message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100875 if( result == 0 )
876 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100877
Azim Khand30ca132017-06-09 04:32:58 +0100878 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx2.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100879 }
880
Paul Bakkerbd51b262014-07-10 15:26:12 +0200881exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100882 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
883 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000886}
Paul Bakker33b43f12013-08-20 11:48:36 +0200887/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000888
Paul Bakker33b43f12013-08-20 11:48:36 +0200889/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100890void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000891{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 mbedtls_rsa_context ctx;
893 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000896}
Paul Bakker33b43f12013-08-20 11:48:36 +0200897/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000898
Paul Bakker33b43f12013-08-20 11:48:36 +0200899/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100900void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
901 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000902{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100904 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000905
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100906 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000908
Paul Bakker33b43f12013-08-20 11:48:36 +0200909 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000910 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100911 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000912 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200913 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000914 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100915 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000916 }
917
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100918 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100920
Paul Bakkerbd51b262014-07-10 15:26:12 +0200921exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100922 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000924}
Paul Bakker33b43f12013-08-20 11:48:36 +0200925/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000926
Paul Bakker33b43f12013-08-20 11:48:36 +0200927/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100928void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
929 int radix_Q, char * input_Q, int radix_N,
930 char * input_N, int radix_E, char * input_E,
931 int radix_D, char * input_D, int radix_DP,
932 char * input_DP, int radix_DQ,
933 char * input_DQ, int radix_QP,
934 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000935{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000939
Paul Bakker33b43f12013-08-20 11:48:36 +0200940 ctx.len = mod / 8;
941 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000944 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200945 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000948 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200949 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000952 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200953 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000956 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200957 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000960 }
Hanno Becker131134f2017-08-23 08:31:07 +0100961#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200962 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000965 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200966 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000969 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200970 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000973 }
Hanno Becker131134f2017-08-23 08:31:07 +0100974#else
975 ((void) radix_DP); ((void) input_DP);
976 ((void) radix_DQ); ((void) input_DQ);
977 ((void) radix_QP); ((void) input_QP);
978#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100981
Paul Bakkerbd51b262014-07-10 15:26:12 +0200982exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000984}
Paul Bakker33b43f12013-08-20 11:48:36 +0200985/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000986
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100987/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100988void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
989 int radix_Epub, char * input_Epub, int radix_P,
990 char * input_P, int radix_Q, char * input_Q,
991 int radix_N, char * input_N, int radix_E,
992 char * input_E, int radix_D, char * input_D,
993 int radix_DP, char * input_DP, int radix_DQ,
994 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100995 int result )
996{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
1000 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001001
1002 pub.len = mod / 8;
1003 prv.len = mod / 8;
1004
1005 if( strlen( input_Npub ) )
1006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001008 }
1009 if( strlen( input_Epub ) )
1010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001012 }
1013
1014 if( strlen( input_P ) )
1015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001017 }
1018 if( strlen( input_Q ) )
1019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001021 }
1022 if( strlen( input_N ) )
1023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001025 }
1026 if( strlen( input_E ) )
1027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001029 }
1030 if( strlen( input_D ) )
1031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001033 }
Hanno Becker131134f2017-08-23 08:31:07 +01001034#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001035 if( strlen( input_DP ) )
1036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001038 }
1039 if( strlen( input_DQ ) )
1040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001042 }
1043 if( strlen( input_QP ) )
1044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001046 }
Hanno Becker131134f2017-08-23 08:31:07 +01001047#else
1048 ((void) radix_DP); ((void) input_DP);
1049 ((void) radix_DQ); ((void) input_DQ);
1050 ((void) radix_QP); ((void) input_QP);
1051#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001054
1055exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_rsa_free( &pub );
1057 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001058}
1059/* END_CASE */
1060
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001061/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_rsa_context ctx;
1065 mbedtls_entropy_context entropy;
1066 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001067 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001068
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001069 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001071 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001072
Hanno Beckera47023e2017-12-22 17:08:03 +00001073 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1074 &entropy, (const unsigned char *) pers,
1075 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001078 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001081 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001082 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001083
Paul Bakkerbd51b262014-07-10 15:26:12 +02001084exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_rsa_free( &ctx );
1086 mbedtls_ctr_drbg_free( &ctr_drbg );
1087 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001088}
Paul Bakker33b43f12013-08-20 11:48:36 +02001089/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001090
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001091/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001092void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001093 int radix_D, char *input_D,
1094 int radix_E, char *input_E,
1095 int radix_P, char *output_P,
1096 int radix_Q, char *output_Q,
1097 int corrupt, int result )
1098{
1099 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1100
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001101 mbedtls_mpi_init( &N );
1102 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1103 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1104 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1105
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001106 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1107 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1108 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1109 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1110 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1111
1112 if( corrupt )
1113 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1114
1115 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001116 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001117
1118 if( !corrupt )
1119 {
1120 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1121 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1122 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1123 }
1124
1125exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001126 mbedtls_mpi_free( &N );
1127 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1128 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1129 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001130}
1131/* END_CASE */
1132
Hanno Becker6b4ce492017-08-23 11:00:21 +01001133/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001134void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1135 int radix_Q, char *input_Q,
1136 int radix_E, char *input_E,
1137 int radix_D, char *output_D,
1138 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001139{
1140 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1141
1142 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1143 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1144 mbedtls_mpi_init( &E );
1145 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1146
1147 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1148 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1149 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1150 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1151
1152 if( corrupt )
1153 {
1154 /* Make E even */
1155 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1156 }
1157
1158 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001159 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1160 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001161
1162 if( !corrupt )
1163 {
1164 /*
1165 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1166 */
1167
1168 /* Replace P,Q by P-1, Q-1 */
1169 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1170 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1171
1172 /* Check D == Dp modulo P-1 */
1173 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1174 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1175 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1176
1177 /* Check D == Dp modulo Q-1 */
1178 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1179 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1180 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1181 }
1182
1183exit:
1184
1185 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1186 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1187 mbedtls_mpi_free( &E );
1188 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1189}
1190/* END_CASE */
1191
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001192/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001193void mbedtls_rsa_import( int radix_N, char *input_N,
1194 int radix_P, char *input_P,
1195 int radix_Q, char *input_Q,
1196 int radix_D, char *input_D,
1197 int radix_E, char *input_E,
1198 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001199 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001200 int res_check,
1201 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001202{
1203 mbedtls_mpi N, P, Q, D, E;
1204 mbedtls_rsa_context ctx;
1205
Hanno Beckere1582a82017-09-29 11:51:05 +01001206 /* Buffers used for encryption-decryption test */
1207 unsigned char *buf_orig = NULL;
1208 unsigned char *buf_enc = NULL;
1209 unsigned char *buf_dec = NULL;
1210
Hanno Beckerc77ab892017-08-23 11:01:06 +01001211 mbedtls_entropy_context entropy;
1212 mbedtls_ctr_drbg_context ctr_drbg;
1213 const char *pers = "test_suite_rsa";
1214
Hanno Becker4d6e8342017-09-29 11:50:18 +01001215 const int have_N = ( strlen( input_N ) > 0 );
1216 const int have_P = ( strlen( input_P ) > 0 );
1217 const int have_Q = ( strlen( input_Q ) > 0 );
1218 const int have_D = ( strlen( input_D ) > 0 );
1219 const int have_E = ( strlen( input_E ) > 0 );
1220
Hanno Beckerc77ab892017-08-23 11:01:06 +01001221 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001222 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001223 mbedtls_rsa_init( &ctx, 0, 0 );
1224
1225 mbedtls_mpi_init( &N );
1226 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1227 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1228
Hanno Beckerd4d60572018-01-10 07:12:01 +00001229 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1230 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1231
Hanno Becker4d6e8342017-09-29 11:50:18 +01001232 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001233 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1234
Hanno Becker4d6e8342017-09-29 11:50:18 +01001235 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001236 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1237
Hanno Becker4d6e8342017-09-29 11:50:18 +01001238 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001239 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1240
Hanno Becker4d6e8342017-09-29 11:50:18 +01001241 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001242 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1243
Hanno Becker4d6e8342017-09-29 11:50:18 +01001244 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001245 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1246
1247 if( !successive )
1248 {
1249 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001250 have_N ? &N : NULL,
1251 have_P ? &P : NULL,
1252 have_Q ? &Q : NULL,
1253 have_D ? &D : NULL,
1254 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001255 }
1256 else
1257 {
1258 /* Import N, P, Q, D, E separately.
1259 * This should make no functional difference. */
1260
1261 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001262 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001263 NULL, NULL, NULL, NULL ) == 0 );
1264
1265 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1266 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001267 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001268 NULL, NULL, NULL ) == 0 );
1269
1270 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1271 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001272 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001273 NULL, NULL ) == 0 );
1274
1275 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1276 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001277 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001278 NULL ) == 0 );
1279
1280 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1281 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001282 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001283 }
1284
Hanno Becker04877a42017-10-11 10:01:33 +01001285 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001286
Hanno Beckere1582a82017-09-29 11:51:05 +01001287 /* On expected success, perform some public and private
1288 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001289 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001290 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001291 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001292 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1293 else
1294 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1295
1296 if( res_check != 0 )
1297 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001298
1299 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1300 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1301 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1302 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1303 goto exit;
1304
1305 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1306 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1307
1308 /* Make sure the number we're generating is smaller than the modulus */
1309 buf_orig[0] = 0x00;
1310
1311 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1312
1313 if( is_priv )
1314 {
1315 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1316 &ctr_drbg, buf_enc,
1317 buf_dec ) == 0 );
1318
1319 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1320 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1321 }
1322 }
1323
Hanno Beckerc77ab892017-08-23 11:01:06 +01001324exit:
1325
Hanno Beckere1582a82017-09-29 11:51:05 +01001326 mbedtls_free( buf_orig );
1327 mbedtls_free( buf_enc );
1328 mbedtls_free( buf_dec );
1329
Hanno Beckerc77ab892017-08-23 11:01:06 +01001330 mbedtls_rsa_free( &ctx );
1331
1332 mbedtls_ctr_drbg_free( &ctr_drbg );
1333 mbedtls_entropy_free( &entropy );
1334
1335 mbedtls_mpi_free( &N );
1336 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1337 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1338}
1339/* END_CASE */
1340
Hanno Becker417f2d62017-08-23 11:44:51 +01001341/* BEGIN_CASE */
1342void mbedtls_rsa_export( int radix_N, char *input_N,
1343 int radix_P, char *input_P,
1344 int radix_Q, char *input_Q,
1345 int radix_D, char *input_D,
1346 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001347 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001348 int successive )
1349{
1350 /* Original MPI's with which we set up the RSA context */
1351 mbedtls_mpi N, P, Q, D, E;
1352
1353 /* Exported MPI's */
1354 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1355
1356 const int have_N = ( strlen( input_N ) > 0 );
1357 const int have_P = ( strlen( input_P ) > 0 );
1358 const int have_Q = ( strlen( input_Q ) > 0 );
1359 const int have_D = ( strlen( input_D ) > 0 );
1360 const int have_E = ( strlen( input_E ) > 0 );
1361
Hanno Becker417f2d62017-08-23 11:44:51 +01001362 mbedtls_rsa_context ctx;
1363
1364 mbedtls_rsa_init( &ctx, 0, 0 );
1365
1366 mbedtls_mpi_init( &N );
1367 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1368 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1369
1370 mbedtls_mpi_init( &Ne );
1371 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1372 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1373
1374 /* Setup RSA context */
1375
1376 if( have_N )
1377 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1378
1379 if( have_P )
1380 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1381
1382 if( have_Q )
1383 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1384
1385 if( have_D )
1386 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1387
1388 if( have_E )
1389 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1390
1391 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1392 strlen( input_N ) ? &N : NULL,
1393 strlen( input_P ) ? &P : NULL,
1394 strlen( input_Q ) ? &Q : NULL,
1395 strlen( input_D ) ? &D : NULL,
1396 strlen( input_E ) ? &E : NULL ) == 0 );
1397
Hanno Becker7f25f852017-10-10 16:56:22 +01001398 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001399
1400 /*
1401 * Export parameters and compare to original ones.
1402 */
1403
1404 /* N and E must always be present. */
1405 if( !successive )
1406 {
1407 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1408 }
1409 else
1410 {
1411 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1412 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1413 }
1414 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1415 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1416
1417 /* If we were providing enough information to setup a complete private context,
1418 * we expect to be able to export all core parameters. */
1419
1420 if( is_priv )
1421 {
1422 if( !successive )
1423 {
1424 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1425 &De, NULL ) == 0 );
1426 }
1427 else
1428 {
1429 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1430 NULL, NULL ) == 0 );
1431 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1432 NULL, NULL ) == 0 );
1433 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1434 &De, NULL ) == 0 );
1435 }
1436
1437 if( have_P )
1438 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1439
1440 if( have_Q )
1441 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1442
1443 if( have_D )
1444 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1445
1446 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001447 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1448 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001449 }
1450
1451exit:
1452
1453 mbedtls_rsa_free( &ctx );
1454
1455 mbedtls_mpi_free( &N );
1456 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1457 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1458
1459 mbedtls_mpi_free( &Ne );
1460 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1461 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1462}
1463/* END_CASE */
1464
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001465/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Becker750e8b42017-08-25 07:54:27 +01001466void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1467 int radix_P, char *input_P,
1468 int radix_Q, char *input_Q,
1469 int radix_D, char *input_D,
1470 int radix_E, char *input_E,
1471 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001472{
1473 /* Original MPI's with which we set up the RSA context */
1474 mbedtls_mpi N, P, Q, D, E;
1475
1476 const int have_N = ( strlen( input_N ) > 0 );
1477 const int have_P = ( strlen( input_P ) > 0 );
1478 const int have_Q = ( strlen( input_Q ) > 0 );
1479 const int have_D = ( strlen( input_D ) > 0 );
1480 const int have_E = ( strlen( input_E ) > 0 );
1481
1482 mbedtls_entropy_context entropy;
1483 mbedtls_ctr_drbg_context ctr_drbg;
1484 const char *pers = "test_suite_rsa";
1485
1486 mbedtls_mpi_init( &N );
1487 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1488 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1489
1490 mbedtls_ctr_drbg_init( &ctr_drbg );
1491 mbedtls_entropy_init( &entropy );
1492 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1493 &entropy, (const unsigned char *) pers,
1494 strlen( pers ) ) == 0 );
1495
1496 if( have_N )
1497 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1498
1499 if( have_P )
1500 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1501
1502 if( have_Q )
1503 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1504
1505 if( have_D )
1506 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1507
1508 if( have_E )
1509 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1510
Hanno Becker750e8b42017-08-25 07:54:27 +01001511 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1512 have_P ? &P : NULL,
1513 have_Q ? &Q : NULL,
1514 have_D ? &D : NULL,
1515 have_E ? &E : NULL,
1516 prng ? mbedtls_ctr_drbg_random : NULL,
1517 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001518exit:
1519
1520 mbedtls_ctr_drbg_free( &ctr_drbg );
1521 mbedtls_entropy_free( &entropy );
1522
1523 mbedtls_mpi_free( &N );
1524 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1525 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1526}
1527/* END_CASE */
1528
Hanno Beckerc77ab892017-08-23 11:01:06 +01001529/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001530void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1531 data_t *input_Q, data_t *input_D,
1532 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001533 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001534{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001535 /* Exported buffers */
Azim Khand30ca132017-06-09 04:32:58 +01001536 unsigned char bufNe[1000];
1537 unsigned char bufPe[1000];
1538 unsigned char bufQe[1000];
1539 unsigned char bufDe[1000];
1540 unsigned char bufEe[1000];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001541
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001542 mbedtls_rsa_context ctx;
1543
1544 mbedtls_rsa_init( &ctx, 0, 0 );
1545
1546 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001547 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001548 input_N->len ? input_N->x : NULL, input_N->len,
1549 input_P->len ? input_P->x : NULL, input_P->len,
1550 input_Q->len ? input_Q->x : NULL, input_Q->len,
1551 input_D->len ? input_D->x : NULL, input_D->len,
1552 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001553
Hanno Becker7f25f852017-10-10 16:56:22 +01001554 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001555
1556 /*
1557 * Export parameters and compare to original ones.
1558 */
1559
1560 /* N and E must always be present. */
1561 if( !successive )
1562 {
Azim Khand30ca132017-06-09 04:32:58 +01001563 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001564 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001565 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001566 }
1567 else
1568 {
Azim Khand30ca132017-06-09 04:32:58 +01001569 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001570 NULL, 0, NULL, 0, NULL, 0,
1571 NULL, 0 ) == 0 );
1572 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1573 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001574 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001575 }
Azim Khand30ca132017-06-09 04:32:58 +01001576 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1577 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001578
1579 /* If we were providing enough information to setup a complete private context,
1580 * we expect to be able to export all core parameters. */
1581
1582 if( is_priv )
1583 {
1584 if( !successive )
1585 {
1586 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001587 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1588 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1589 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001590 NULL, 0 ) == 0 );
1591 }
1592 else
1593 {
1594 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001595 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001596 NULL, 0, NULL, 0,
1597 NULL, 0 ) == 0 );
1598
1599 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001600 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001601 NULL, 0, NULL, 0 ) == 0 );
1602
Azim Khand30ca132017-06-09 04:32:58 +01001603 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1604 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001605 NULL, 0 ) == 0 );
1606 }
1607
Azim Khand30ca132017-06-09 04:32:58 +01001608 if( input_P->len )
1609 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001610
Azim Khand30ca132017-06-09 04:32:58 +01001611 if( input_Q->len )
1612 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001613
Azim Khand30ca132017-06-09 04:32:58 +01001614 if( input_D->len )
1615 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001616
1617 }
1618
1619exit:
1620 mbedtls_rsa_free( &ctx );
1621}
1622/* END_CASE */
1623
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001624/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001625void mbedtls_rsa_import_raw( data_t *input_N,
1626 data_t *input_P, data_t *input_Q,
1627 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001628 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001629 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001630 int res_check,
1631 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001632{
Hanno Beckere1582a82017-09-29 11:51:05 +01001633 /* Buffers used for encryption-decryption test */
1634 unsigned char *buf_orig = NULL;
1635 unsigned char *buf_enc = NULL;
1636 unsigned char *buf_dec = NULL;
1637
Hanno Beckerc77ab892017-08-23 11:01:06 +01001638 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001639 mbedtls_entropy_context entropy;
1640 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001641
Hanno Beckerc77ab892017-08-23 11:01:06 +01001642 const char *pers = "test_suite_rsa";
1643
1644 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001645 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001646 mbedtls_rsa_init( &ctx, 0, 0 );
1647
Hanno Beckerc77ab892017-08-23 11:01:06 +01001648 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1649 &entropy, (const unsigned char *) pers,
1650 strlen( pers ) ) == 0 );
1651
Hanno Beckerc77ab892017-08-23 11:01:06 +01001652 if( !successive )
1653 {
1654 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001655 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1656 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1657 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1658 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1659 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001660 }
1661 else
1662 {
1663 /* Import N, P, Q, D, E separately.
1664 * This should make no functional difference. */
1665
1666 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001667 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001668 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1669
1670 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1671 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001672 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001673 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1674
1675 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1676 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001677 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001678 NULL, 0, NULL, 0 ) == 0 );
1679
1680 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1681 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001682 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001683 NULL, 0 ) == 0 );
1684
1685 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1686 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001687 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001688 }
1689
Hanno Becker04877a42017-10-11 10:01:33 +01001690 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001691
Hanno Beckere1582a82017-09-29 11:51:05 +01001692 /* On expected success, perform some public and private
1693 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001694 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001695 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001696 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001697 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1698 else
1699 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1700
1701 if( res_check != 0 )
1702 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001703
1704 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1705 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1706 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1707 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1708 goto exit;
1709
1710 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1711 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1712
1713 /* Make sure the number we're generating is smaller than the modulus */
1714 buf_orig[0] = 0x00;
1715
1716 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1717
1718 if( is_priv )
1719 {
1720 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1721 &ctr_drbg, buf_enc,
1722 buf_dec ) == 0 );
1723
1724 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1725 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1726 }
1727 }
1728
Hanno Beckerc77ab892017-08-23 11:01:06 +01001729exit:
1730
Hanno Becker3f3ae852017-10-02 10:08:39 +01001731 mbedtls_free( buf_orig );
1732 mbedtls_free( buf_enc );
1733 mbedtls_free( buf_dec );
1734
Hanno Beckerc77ab892017-08-23 11:01:06 +01001735 mbedtls_rsa_free( &ctx );
1736
1737 mbedtls_ctr_drbg_free( &ctr_drbg );
1738 mbedtls_entropy_free( &entropy );
1739
1740}
1741/* END_CASE */
1742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001744void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001745{
Andres AG93012e82016-09-09 09:10:28 +01001746 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001747}
Paul Bakker33b43f12013-08-20 11:48:36 +02001748/* END_CASE */