blob: 2da01efe449a1c22034b2f938468fcad7a3f191b [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,
70 mbedtls_rsa_gen_key( NULL, NULL, NULL, 0, 0 ) );
71
72 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
73 mbedtls_rsa_check_pubkey( NULL ) );
74 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
75 mbedtls_rsa_check_privkey( NULL ) );
76
77 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
78 mbedtls_rsa_check_pub_priv( NULL, &ctx ) );
79 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
80 mbedtls_rsa_check_pub_priv( &ctx, NULL ) );
81
82 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
83 mbedtls_rsa_public( NULL, buf, buf ) );
84 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
85 mbedtls_rsa_public( &ctx, NULL, buf ) );
86 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
87 mbedtls_rsa_public( &ctx, buf, NULL ) );
88
89 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
90 mbedtls_rsa_private( NULL, NULL, NULL,
91 buf, buf ) );
92 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
93 mbedtls_rsa_private( &ctx, NULL, NULL,
94 NULL, buf ) );
95 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
96 mbedtls_rsa_private( &ctx, NULL, NULL,
97 buf, NULL ) );
98
99 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
100 mbedtls_rsa_pkcs1_encrypt( NULL, NULL, NULL,
101 valid_mode,
102 sizeof( buf ), buf,
103 buf ) );
104 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
105 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
106 invalid_mode,
107 sizeof( buf ), buf,
108 buf ) );
109 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
110 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
111 valid_mode,
112 sizeof( buf ), NULL,
113 buf ) );
114 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
115 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
116 valid_mode,
117 sizeof( buf ), buf,
118 NULL ) );
119
120 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
121 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
122 NULL,
123 valid_mode,
124 sizeof( buf ), buf,
125 buf ) );
126 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
127 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
128 NULL,
129 invalid_mode,
130 sizeof( buf ), buf,
131 buf ) );
132 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
133 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
134 NULL,
135 valid_mode,
136 sizeof( buf ), NULL,
137 buf ) );
138 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
139 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
140 NULL,
141 valid_mode,
142 sizeof( buf ), buf,
143 NULL ) );
144
145 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
146 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
147 valid_mode,
148 buf, sizeof( buf ),
149 sizeof( buf ), buf,
150 buf ) );
151 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
152 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
153 invalid_mode,
154 buf, sizeof( buf ),
155 sizeof( buf ), buf,
156 buf ) );
157 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
158 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
159 valid_mode,
160 NULL, sizeof( buf ),
161 sizeof( buf ), buf,
162 buf ) );
163 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
164 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
165 valid_mode,
166 buf, sizeof( buf ),
167 sizeof( buf ), NULL,
168 buf ) );
169 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
170 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
171 valid_mode,
172 buf, sizeof( buf ),
173 sizeof( buf ), buf,
174 NULL ) );
175
176 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
177 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
178 valid_mode, &olen,
179 buf, buf, 42 ) );
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
181 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
182 invalid_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 valid_mode, NULL,
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, &olen,
191 NULL, 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 buf, NULL, 42 ) );
196
197 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
198 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
199 NULL,
200 valid_mode, &olen,
201 buf, buf, 42 ) );
202 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
203 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
204 NULL,
205 invalid_mode, &olen,
206 buf, buf, 42 ) );
207 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
208 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
209 NULL,
210 valid_mode, NULL,
211 buf, buf, 42 ) );
212 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
213 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
214 NULL,
215 valid_mode, &olen,
216 NULL, buf, 42 ) );
217 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
218 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
219 NULL,
220 valid_mode, &olen,
221 buf, NULL, 42 ) );
222
223 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
224 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
225 valid_mode,
226 buf, sizeof( buf ),
227 &olen,
228 buf, buf, 42 ) );
229 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
230 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
231 invalid_mode,
232 buf, sizeof( buf ),
233 &olen,
234 buf, buf, 42 ) );
235 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
236 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
237 valid_mode,
238 NULL, sizeof( buf ),
239 NULL,
240 buf, buf, 42 ) );
241 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
242 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
243 valid_mode,
244 buf, sizeof( buf ),
245 &olen,
246 NULL, buf, 42 ) );
247 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
248 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
249 valid_mode,
250 buf, sizeof( buf ),
251 &olen,
252 buf, NULL, 42 ) );
253
254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
255 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
256 valid_mode,
257 0, sizeof( buf ), buf,
258 buf ) );
259 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
260 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
261 invalid_mode,
262 0, sizeof( buf ), buf,
263 buf ) );
264 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
265 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
266 valid_mode,
267 0, sizeof( buf ), NULL,
268 buf ) );
269 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
270 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
271 valid_mode,
272 0, sizeof( buf ), buf,
273 NULL ) );
274
275 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
276 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
277 valid_mode,
278 0, sizeof( buf ), buf,
279 buf ) );
280 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
281 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
282 invalid_mode,
283 0, sizeof( buf ), buf,
284 buf ) );
285 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
286 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
287 valid_mode,
288 0, sizeof( buf ), NULL,
289 buf ) );
290 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
291 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
292 valid_mode,
293 0, sizeof( buf ), buf,
294 NULL ) );
295
296 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
297 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
298 valid_mode,
299 0, sizeof( buf ), buf,
300 buf ) );
301 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
302 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
303 invalid_mode,
304 0, sizeof( buf ), buf,
305 buf ) );
306 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
307 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
308 valid_mode,
309 0, sizeof( buf ), NULL,
310 buf ) );
311 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
312 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
313 valid_mode,
314 0, sizeof( buf ), buf,
315 NULL ) );
316
317 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
318 mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
319 valid_mode,
320 0, sizeof( buf ), buf,
321 buf ) );
322 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
323 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
324 invalid_mode,
325 0, sizeof( buf ), buf,
326 buf ) );
327 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
328 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
329 valid_mode,
330 0, sizeof( buf ), NULL,
331 buf ) );
332 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
333 mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
334 valid_mode,
335 0, sizeof( buf ), buf,
336 NULL ) );
337
338 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
339 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
340 NULL,
341 valid_mode,
342 0, sizeof( buf ), buf,
343 buf ) );
344 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
345 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
346 NULL,
347 invalid_mode,
348 0, sizeof( buf ), buf,
349 buf ) );
350 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
351 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
352 NULL,
353 valid_mode,
354 0, sizeof( buf ),
355 NULL, buf ) );
356 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
357 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
358 NULL,
359 valid_mode,
360 0, sizeof( buf ), buf,
361 NULL ) );
362
363 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
364 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
365 valid_mode,
366 0, sizeof( buf ),
367 buf, buf ) );
368 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
369 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
370 invalid_mode,
371 0, sizeof( buf ),
372 buf, buf ) );
373 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
374 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
375 valid_mode,
376 0, sizeof( buf ),
377 NULL, buf ) );
378 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
379 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
380 valid_mode,
381 0, sizeof( buf ),
382 buf, NULL ) );
383
384 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
385 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
386 valid_mode,
387 0, sizeof( buf ),
388 buf,
389 0, 0,
390 buf ) );
391 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
392 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
393 invalid_mode,
394 0, sizeof( buf ),
395 buf,
396 0, 0,
397 buf ) );
398 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
399 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
400 valid_mode,
401 0, sizeof( buf ),
402 NULL, 0, 0,
403 buf ) );
404 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
405 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
406 valid_mode,
407 0, sizeof( buf ),
408 buf, 0, 0,
409 NULL ) );
410
411 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
412 mbedtls_rsa_copy( NULL, &ctx ) );
413 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
414 mbedtls_rsa_copy( &ctx, NULL ) );
415
416exit:
417 return;
418}
419/* END_CASE */
420
Paul Bakker33b43f12013-08-20 11:48:36 +0200421/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100422void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100423 int digest, int mod, int radix_P, char * input_P,
424 int radix_Q, char * input_Q, int radix_N,
425 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100426 data_t * result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000427{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000428 unsigned char hash_result[1000];
429 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100431 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200432 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000433
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100434 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
435 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000437
Paul Bakker42a29bf2009-07-07 20:18:41 +0000438 memset( hash_result, 0x00, 1000 );
439 memset( output, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200440 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000441
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100442 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
443 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
444 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
445 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000446
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100447 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
448 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100449 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000451
Paul Bakker42a29bf2009-07-07 20:18:41 +0000452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100454 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 +0000455
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100456 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
457 MBEDTLS_RSA_PRIVATE, digest, 0,
458 hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200459 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000460 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000461
Azim Khand30ca132017-06-09 04:32:58 +0100462 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000463 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000464
Paul Bakkerbd51b262014-07-10 15:26:12 +0200465exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100466 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
467 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000469}
Paul Bakker33b43f12013-08-20 11:48:36 +0200470/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000471
Paul Bakker33b43f12013-08-20 11:48:36 +0200472/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100473void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100474 int digest, int mod, int radix_N,
475 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100476 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000477{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000478 unsigned char hash_result[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000480
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100481 mbedtls_mpi N, E;
482
483 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000485 memset( hash_result, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000486
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100487 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
488 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
489 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
490 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000492
Paul Bakker42a29bf2009-07-07 20:18:41 +0000493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100495 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 +0000496
Azim Khand30ca132017-06-09 04:32:58 +0100497 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 +0100498
Paul Bakkerbd51b262014-07-10 15:26:12 +0200499exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100500 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000502}
Paul Bakker33b43f12013-08-20 11:48:36 +0200503/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000504
Paul Bakker821fb082009-07-12 13:26:42 +0000505
Paul Bakker33b43f12013-08-20 11:48:36 +0200506/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100507void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100508 int padding_mode, int mod, int radix_P,
509 char * input_P, int radix_Q, char * input_Q,
510 int radix_N, char * input_N, int radix_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100511 char * input_E, data_t * result_hex_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000512{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000513 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100515 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200516 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100519 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
520 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000521
Paul Bakker42a29bf2009-07-07 20:18:41 +0000522 memset( output, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200523 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000524
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100525 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
526 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
527 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
528 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000529
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100530 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
531 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100532 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000534
Paul Bakker821fb082009-07-12 13:26:42 +0000535
Hanno Becker8fd55482017-08-23 14:07:48 +0100536 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
537 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
Azim Khand30ca132017-06-09 04:32:58 +0100538 hash_result->len, hash_result->x,
539 output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000540
Paul Bakker821fb082009-07-12 13:26:42 +0000541
Azim Khand30ca132017-06-09 04:32:58 +0100542 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000543
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200544#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100545 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100547 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100548 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100549 memset( output, 0x00, 1000 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100550
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100551 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Azim Khand30ca132017-06-09 04:32:58 +0100553 hash_result->len, hash_result->x, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100554
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100555#if !defined(MBEDTLS_RSA_ALT)
556 TEST_ASSERT( res == 0 );
557#else
558 TEST_ASSERT( ( res == 0 ) ||
559 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
560#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100561
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100562 if( res == 0 )
563 {
Azim Khand30ca132017-06-09 04:32:58 +0100564 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100565 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100566 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200567#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100568
Paul Bakkerbd51b262014-07-10 15:26:12 +0200569exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100570 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
571 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000574}
Paul Bakker33b43f12013-08-20 11:48:36 +0200575/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000576
Paul Bakker33b43f12013-08-20 11:48:36 +0200577/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100578void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200579 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100580 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100581 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000582{
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100583 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000585
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100586 mbedtls_mpi N, E;
587 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100590 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000591
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100592 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
593 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000594
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100595 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
596 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000598
Paul Bakker821fb082009-07-12 13:26:42 +0000599
Azim Khand30ca132017-06-09 04:32:58 +0100600 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 +0100601
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200602#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100603 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100605 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100606 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100607 int ok;
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200608 size_t olen;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100609
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100610 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Azim Khand30ca132017-06-09 04:32:58 +0100612 &olen, result_str->x, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100613
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100614#if !defined(MBEDTLS_RSA_ALT)
615 TEST_ASSERT( res == 0 );
616#else
617 TEST_ASSERT( ( res == 0 ) ||
618 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
619#endif
620
621 if( res == 0 )
622 {
Azim Khand30ca132017-06-09 04:32:58 +0100623 ok = olen == hash_result->len && memcmp( output, hash_result->x, olen ) == 0;
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100624 if( correct == 0 )
625 TEST_ASSERT( ok == 1 );
626 else
627 TEST_ASSERT( ok == 0 );
628 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100629 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200630#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100631
Paul Bakkerbd51b262014-07-10 15:26:12 +0200632exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100633 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000635}
Paul Bakker33b43f12013-08-20 11:48:36 +0200636/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000637
Paul Bakker33b43f12013-08-20 11:48:36 +0200638/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100639void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100640 int mod, int radix_N, char * input_N,
641 int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100642 data_t * result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000643{
Paul Bakker821fb082009-07-12 13:26:42 +0000644 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_rsa_context ctx;
Paul Bakker997bbd12011-03-13 15:45:42 +0000646 rnd_pseudo_info rnd_info;
647
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100648 mbedtls_mpi N, E;
649 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
650
Paul Bakker997bbd12011-03-13 15:45:42 +0000651 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000654 memset( output, 0x00, 1000 );
Paul Bakker821fb082009-07-12 13:26:42 +0000655
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100656 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
657 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000658
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100659 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
660 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000662
Paul Bakker42a29bf2009-07-07 20:18:41 +0000663
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100664 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100665 MBEDTLS_RSA_PUBLIC, message_str->len,
666 message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200667 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000668 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000669
Azim Khand30ca132017-06-09 04:32:58 +0100670 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000671 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100672
Paul Bakkerbd51b262014-07-10 15:26:12 +0200673exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100674 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000676}
Paul Bakker33b43f12013-08-20 11:48:36 +0200677/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000678
Paul Bakker33b43f12013-08-20 11:48:36 +0200679/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100680void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100681 int mod, int radix_N, char * input_N,
682 int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100683 data_t * result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000684{
Paul Bakkera6656852010-07-18 19:47:14 +0000685 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000687
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100688 mbedtls_mpi N, E;
689
690 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000692 memset( output, 0x00, 1000 );
Paul Bakkera6656852010-07-18 19:47:14 +0000693
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100694 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
695 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000696
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100697 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
698 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000700
Paul Bakkera6656852010-07-18 19:47:14 +0000701
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100702 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL,
Azim Khand30ca132017-06-09 04:32:58 +0100703 MBEDTLS_RSA_PUBLIC, message_str->len,
704 message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200705 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000706 {
Paul Bakkera6656852010-07-18 19:47:14 +0000707
Azim Khand30ca132017-06-09 04:32:58 +0100708 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000709 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100710
Paul Bakkerbd51b262014-07-10 15:26:12 +0200711exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100712 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000714}
Paul Bakker33b43f12013-08-20 11:48:36 +0200715/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000716
Paul Bakker33b43f12013-08-20 11:48:36 +0200717/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100718void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100719 int mod, int radix_P, char * input_P,
720 int radix_Q, char * input_Q, int radix_N,
721 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100722 int max_output, data_t * result_hex_str,
Azim Khand30ca132017-06-09 04:32:58 +0100723 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000724{
Paul Bakker42a29bf2009-07-07 20:18:41 +0000725 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000727 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200728 rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100729 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000730
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100731 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
732 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000735
Paul Bakker42a29bf2009-07-07 20:18:41 +0000736 memset( output, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200737 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000738
Paul Bakker42a29bf2009-07-07 20:18:41 +0000739
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100740 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
741 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
742 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
743 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000744
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100745 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
746 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100747 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000749
Paul Bakker69998dd2009-07-11 19:15:20 +0000750 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000751
Azim Khand30ca132017-06-09 04:32:58 +0100752 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 +0200753 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000754 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000755
Azim Khand30ca132017-06-09 04:32:58 +0100756 TEST_ASSERT( hexcmp( output, result_hex_str->x, output_len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000757 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000758
Paul Bakkerbd51b262014-07-10 15:26:12 +0200759exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100760 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
761 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000763}
Paul Bakker33b43f12013-08-20 11:48:36 +0200764/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000765
Paul Bakker33b43f12013-08-20 11:48:36 +0200766/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100767void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100768 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100769 data_t * result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000770{
Paul Bakker821fb082009-07-12 13:26:42 +0000771 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000773
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100774 mbedtls_mpi N, E;
775
776 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
778 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000779 memset( output, 0x00, 1000 );
Paul Bakker821fb082009-07-12 13:26:42 +0000780
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100781 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
782 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000783
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100784 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
785 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000787
Paul Bakker821fb082009-07-12 13:26:42 +0000788
Azim Khand30ca132017-06-09 04:32:58 +0100789 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200790 if( result == 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( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000794 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100795
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100796 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200798 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100802
803 memset( output, 0x00, 1000 );
Azim Khand30ca132017-06-09 04:32:58 +0100804 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100805 if( result == 0 )
806 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100807
Azim Khand30ca132017-06-09 04:32:58 +0100808 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100809 }
810
Paul Bakkerbd51b262014-07-10 15:26:12 +0200811exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100812 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_rsa_free( &ctx );
814 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000815}
Paul Bakker33b43f12013-08-20 11:48:36 +0200816/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000817
Paul Bakker33b43f12013-08-20 11:48:36 +0200818/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100819void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100820 char * input_P, int radix_Q, char * input_Q,
821 int radix_N, char * input_N, int radix_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100822 char * input_E, data_t * result_hex_str,
Azim Khand30ca132017-06-09 04:32:58 +0100823 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000824{
Paul Bakker821fb082009-07-12 13:26:42 +0000825 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100827 mbedtls_mpi N, P, Q, E;
Paul Bakker548957d2013-08-30 10:30:02 +0200828 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200829 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000830
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100831 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
832 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
834 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000835
Paul Bakker548957d2013-08-30 10:30:02 +0200836 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000837
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100838 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
839 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
840 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
841 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000842
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100843 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
844 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100845 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000847
Paul Bakker821fb082009-07-12 13:26:42 +0000848
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200849 /* repeat three times to test updating of blinding values */
850 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000851 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200852 memset( output, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100854 message_str->x, output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200855 if( result == 0 )
856 {
Paul Bakker821fb082009-07-12 13:26:42 +0000857
Azim Khand30ca132017-06-09 04:32:58 +0100858 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200859 }
Paul Bakker821fb082009-07-12 13:26:42 +0000860 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000861
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100862 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200864 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100868
869 memset( output, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
Azim Khand30ca132017-06-09 04:32:58 +0100871 message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100872 if( result == 0 )
873 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100874
Azim Khand30ca132017-06-09 04:32:58 +0100875 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx2.len, result_hex_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100876 }
877
Paul Bakkerbd51b262014-07-10 15:26:12 +0200878exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100879 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
880 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000883}
Paul Bakker33b43f12013-08-20 11:48:36 +0200884/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000885
Paul Bakker33b43f12013-08-20 11:48:36 +0200886/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100887void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_rsa_context ctx;
890 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000893}
Paul Bakker33b43f12013-08-20 11:48:36 +0200894/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000895
Paul Bakker33b43f12013-08-20 11:48:36 +0200896/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100897void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
898 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100901 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000902
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100903 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000905
Paul Bakker33b43f12013-08-20 11:48:36 +0200906 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000907 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100908 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000909 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200910 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000911 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100912 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000913 }
914
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100915 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100917
Paul Bakkerbd51b262014-07-10 15:26:12 +0200918exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100919 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000921}
Paul Bakker33b43f12013-08-20 11:48:36 +0200922/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000923
Paul Bakker33b43f12013-08-20 11:48:36 +0200924/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100925void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
926 int radix_Q, char * input_Q, int radix_N,
927 char * input_N, int radix_E, char * input_E,
928 int radix_D, char * input_D, int radix_DP,
929 char * input_DP, int radix_DQ,
930 char * input_DQ, int radix_QP,
931 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000936
Paul Bakker33b43f12013-08-20 11:48:36 +0200937 ctx.len = mod / 8;
938 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000941 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200942 if( strlen( input_Q ) )
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.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000945 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200946 if( strlen( input_N ) )
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.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000949 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200950 if( strlen( input_E ) )
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.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000953 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200954 if( strlen( input_D ) )
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.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000957 }
Hanno Becker131134f2017-08-23 08:31:07 +0100958#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200959 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000962 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200963 if( strlen( input_DQ ) )
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.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000966 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200967 if( strlen( input_QP ) )
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.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000970 }
Hanno Becker131134f2017-08-23 08:31:07 +0100971#else
972 ((void) radix_DP); ((void) input_DP);
973 ((void) radix_DQ); ((void) input_DQ);
974 ((void) radix_QP); ((void) input_QP);
975#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100978
Paul Bakkerbd51b262014-07-10 15:26:12 +0200979exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000981}
Paul Bakker33b43f12013-08-20 11:48:36 +0200982/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000983
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100984/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100985void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
986 int radix_Epub, char * input_Epub, int radix_P,
987 char * input_P, int radix_Q, char * input_Q,
988 int radix_N, char * input_N, int radix_E,
989 char * input_E, int radix_D, char * input_D,
990 int radix_DP, char * input_DP, int radix_DQ,
991 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100992 int result )
993{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
997 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100998
999 pub.len = mod / 8;
1000 prv.len = mod / 8;
1001
1002 if( strlen( input_Npub ) )
1003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001005 }
1006 if( strlen( input_Epub ) )
1007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001009 }
1010
1011 if( strlen( input_P ) )
1012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001014 }
1015 if( strlen( input_Q ) )
1016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001018 }
1019 if( strlen( input_N ) )
1020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001022 }
1023 if( strlen( input_E ) )
1024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001026 }
1027 if( strlen( input_D ) )
1028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001030 }
Hanno Becker131134f2017-08-23 08:31:07 +01001031#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001032 if( strlen( input_DP ) )
1033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001035 }
1036 if( strlen( input_DQ ) )
1037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001039 }
1040 if( strlen( input_QP ) )
1041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001043 }
Hanno Becker131134f2017-08-23 08:31:07 +01001044#else
1045 ((void) radix_DP); ((void) input_DP);
1046 ((void) radix_DQ); ((void) input_DQ);
1047 ((void) radix_QP); ((void) input_QP);
1048#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001051
1052exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_rsa_free( &pub );
1054 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001055}
1056/* END_CASE */
1057
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001058/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 mbedtls_rsa_context ctx;
1062 mbedtls_entropy_context entropy;
1063 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001064 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001065
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001066 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001068 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001069
Hanno Beckera47023e2017-12-22 17:08:03 +00001070 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1071 &entropy, (const unsigned char *) pers,
1072 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001075 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001078 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001079 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001080
Paul Bakkerbd51b262014-07-10 15:26:12 +02001081exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_rsa_free( &ctx );
1083 mbedtls_ctr_drbg_free( &ctr_drbg );
1084 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001085}
Paul Bakker33b43f12013-08-20 11:48:36 +02001086/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001087
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001088/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001089void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001090 int radix_D, char *input_D,
1091 int radix_E, char *input_E,
1092 int radix_P, char *output_P,
1093 int radix_Q, char *output_Q,
1094 int corrupt, int result )
1095{
1096 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1097
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001098 mbedtls_mpi_init( &N );
1099 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1100 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1101 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1102
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001103 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1104 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1105 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1106 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1107 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1108
1109 if( corrupt )
1110 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1111
1112 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001113 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001114
1115 if( !corrupt )
1116 {
1117 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1118 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1119 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1120 }
1121
1122exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001123 mbedtls_mpi_free( &N );
1124 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1125 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1126 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001127}
1128/* END_CASE */
1129
Hanno Becker6b4ce492017-08-23 11:00:21 +01001130/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001131void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1132 int radix_Q, char *input_Q,
1133 int radix_E, char *input_E,
1134 int radix_D, char *output_D,
1135 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001136{
1137 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1138
1139 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1140 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1141 mbedtls_mpi_init( &E );
1142 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1143
1144 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1145 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1146 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1147 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1148
1149 if( corrupt )
1150 {
1151 /* Make E even */
1152 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1153 }
1154
1155 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001156 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1157 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001158
1159 if( !corrupt )
1160 {
1161 /*
1162 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1163 */
1164
1165 /* Replace P,Q by P-1, Q-1 */
1166 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1167 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1168
1169 /* Check D == Dp modulo P-1 */
1170 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1171 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1172 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1173
1174 /* Check D == Dp modulo Q-1 */
1175 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1176 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1177 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1178 }
1179
1180exit:
1181
1182 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1183 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1184 mbedtls_mpi_free( &E );
1185 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1186}
1187/* END_CASE */
1188
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001189/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001190void mbedtls_rsa_import( int radix_N, char *input_N,
1191 int radix_P, char *input_P,
1192 int radix_Q, char *input_Q,
1193 int radix_D, char *input_D,
1194 int radix_E, char *input_E,
1195 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001196 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001197 int res_check,
1198 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001199{
1200 mbedtls_mpi N, P, Q, D, E;
1201 mbedtls_rsa_context ctx;
1202
Hanno Beckere1582a82017-09-29 11:51:05 +01001203 /* Buffers used for encryption-decryption test */
1204 unsigned char *buf_orig = NULL;
1205 unsigned char *buf_enc = NULL;
1206 unsigned char *buf_dec = NULL;
1207
Hanno Beckerc77ab892017-08-23 11:01:06 +01001208 mbedtls_entropy_context entropy;
1209 mbedtls_ctr_drbg_context ctr_drbg;
1210 const char *pers = "test_suite_rsa";
1211
Hanno Becker4d6e8342017-09-29 11:50:18 +01001212 const int have_N = ( strlen( input_N ) > 0 );
1213 const int have_P = ( strlen( input_P ) > 0 );
1214 const int have_Q = ( strlen( input_Q ) > 0 );
1215 const int have_D = ( strlen( input_D ) > 0 );
1216 const int have_E = ( strlen( input_E ) > 0 );
1217
Hanno Beckerc77ab892017-08-23 11:01:06 +01001218 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001219 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001220 mbedtls_rsa_init( &ctx, 0, 0 );
1221
1222 mbedtls_mpi_init( &N );
1223 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1224 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1225
Hanno Beckerd4d60572018-01-10 07:12:01 +00001226 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1227 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1228
Hanno Becker4d6e8342017-09-29 11:50:18 +01001229 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001230 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1231
Hanno Becker4d6e8342017-09-29 11:50:18 +01001232 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001233 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1234
Hanno Becker4d6e8342017-09-29 11:50:18 +01001235 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001236 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1237
Hanno Becker4d6e8342017-09-29 11:50:18 +01001238 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001239 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1240
Hanno Becker4d6e8342017-09-29 11:50:18 +01001241 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001242 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1243
1244 if( !successive )
1245 {
1246 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001247 have_N ? &N : NULL,
1248 have_P ? &P : NULL,
1249 have_Q ? &Q : NULL,
1250 have_D ? &D : NULL,
1251 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001252 }
1253 else
1254 {
1255 /* Import N, P, Q, D, E separately.
1256 * This should make no functional difference. */
1257
1258 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001259 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001260 NULL, NULL, NULL, NULL ) == 0 );
1261
1262 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1263 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001264 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001265 NULL, NULL, NULL ) == 0 );
1266
1267 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1268 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001269 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001270 NULL, NULL ) == 0 );
1271
1272 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1273 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001274 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001275 NULL ) == 0 );
1276
1277 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1278 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001279 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001280 }
1281
Hanno Becker04877a42017-10-11 10:01:33 +01001282 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001283
Hanno Beckere1582a82017-09-29 11:51:05 +01001284 /* On expected success, perform some public and private
1285 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001286 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001287 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001288 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001289 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1290 else
1291 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1292
1293 if( res_check != 0 )
1294 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001295
1296 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1297 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1298 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1299 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1300 goto exit;
1301
1302 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1303 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1304
1305 /* Make sure the number we're generating is smaller than the modulus */
1306 buf_orig[0] = 0x00;
1307
1308 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1309
1310 if( is_priv )
1311 {
1312 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1313 &ctr_drbg, buf_enc,
1314 buf_dec ) == 0 );
1315
1316 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1317 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1318 }
1319 }
1320
Hanno Beckerc77ab892017-08-23 11:01:06 +01001321exit:
1322
Hanno Beckere1582a82017-09-29 11:51:05 +01001323 mbedtls_free( buf_orig );
1324 mbedtls_free( buf_enc );
1325 mbedtls_free( buf_dec );
1326
Hanno Beckerc77ab892017-08-23 11:01:06 +01001327 mbedtls_rsa_free( &ctx );
1328
1329 mbedtls_ctr_drbg_free( &ctr_drbg );
1330 mbedtls_entropy_free( &entropy );
1331
1332 mbedtls_mpi_free( &N );
1333 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1334 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1335}
1336/* END_CASE */
1337
Hanno Becker417f2d62017-08-23 11:44:51 +01001338/* BEGIN_CASE */
1339void mbedtls_rsa_export( int radix_N, char *input_N,
1340 int radix_P, char *input_P,
1341 int radix_Q, char *input_Q,
1342 int radix_D, char *input_D,
1343 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001344 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001345 int successive )
1346{
1347 /* Original MPI's with which we set up the RSA context */
1348 mbedtls_mpi N, P, Q, D, E;
1349
1350 /* Exported MPI's */
1351 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1352
1353 const int have_N = ( strlen( input_N ) > 0 );
1354 const int have_P = ( strlen( input_P ) > 0 );
1355 const int have_Q = ( strlen( input_Q ) > 0 );
1356 const int have_D = ( strlen( input_D ) > 0 );
1357 const int have_E = ( strlen( input_E ) > 0 );
1358
Hanno Becker417f2d62017-08-23 11:44:51 +01001359 mbedtls_rsa_context ctx;
1360
1361 mbedtls_rsa_init( &ctx, 0, 0 );
1362
1363 mbedtls_mpi_init( &N );
1364 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1365 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1366
1367 mbedtls_mpi_init( &Ne );
1368 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1369 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1370
1371 /* Setup RSA context */
1372
1373 if( have_N )
1374 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1375
1376 if( have_P )
1377 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1378
1379 if( have_Q )
1380 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1381
1382 if( have_D )
1383 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1384
1385 if( have_E )
1386 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1387
1388 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1389 strlen( input_N ) ? &N : NULL,
1390 strlen( input_P ) ? &P : NULL,
1391 strlen( input_Q ) ? &Q : NULL,
1392 strlen( input_D ) ? &D : NULL,
1393 strlen( input_E ) ? &E : NULL ) == 0 );
1394
Hanno Becker7f25f852017-10-10 16:56:22 +01001395 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001396
1397 /*
1398 * Export parameters and compare to original ones.
1399 */
1400
1401 /* N and E must always be present. */
1402 if( !successive )
1403 {
1404 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1405 }
1406 else
1407 {
1408 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1409 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1410 }
1411 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1412 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1413
1414 /* If we were providing enough information to setup a complete private context,
1415 * we expect to be able to export all core parameters. */
1416
1417 if( is_priv )
1418 {
1419 if( !successive )
1420 {
1421 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1422 &De, NULL ) == 0 );
1423 }
1424 else
1425 {
1426 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1427 NULL, NULL ) == 0 );
1428 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1429 NULL, NULL ) == 0 );
1430 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1431 &De, NULL ) == 0 );
1432 }
1433
1434 if( have_P )
1435 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1436
1437 if( have_Q )
1438 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1439
1440 if( have_D )
1441 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1442
1443 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001444 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1445 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001446 }
1447
1448exit:
1449
1450 mbedtls_rsa_free( &ctx );
1451
1452 mbedtls_mpi_free( &N );
1453 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1454 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1455
1456 mbedtls_mpi_free( &Ne );
1457 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1458 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1459}
1460/* END_CASE */
1461
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001462/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Becker750e8b42017-08-25 07:54:27 +01001463void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1464 int radix_P, char *input_P,
1465 int radix_Q, char *input_Q,
1466 int radix_D, char *input_D,
1467 int radix_E, char *input_E,
1468 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001469{
1470 /* Original MPI's with which we set up the RSA context */
1471 mbedtls_mpi N, P, Q, D, E;
1472
1473 const int have_N = ( strlen( input_N ) > 0 );
1474 const int have_P = ( strlen( input_P ) > 0 );
1475 const int have_Q = ( strlen( input_Q ) > 0 );
1476 const int have_D = ( strlen( input_D ) > 0 );
1477 const int have_E = ( strlen( input_E ) > 0 );
1478
1479 mbedtls_entropy_context entropy;
1480 mbedtls_ctr_drbg_context ctr_drbg;
1481 const char *pers = "test_suite_rsa";
1482
1483 mbedtls_mpi_init( &N );
1484 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1485 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1486
1487 mbedtls_ctr_drbg_init( &ctr_drbg );
1488 mbedtls_entropy_init( &entropy );
1489 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1490 &entropy, (const unsigned char *) pers,
1491 strlen( pers ) ) == 0 );
1492
1493 if( have_N )
1494 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1495
1496 if( have_P )
1497 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1498
1499 if( have_Q )
1500 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1501
1502 if( have_D )
1503 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1504
1505 if( have_E )
1506 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1507
Hanno Becker750e8b42017-08-25 07:54:27 +01001508 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1509 have_P ? &P : NULL,
1510 have_Q ? &Q : NULL,
1511 have_D ? &D : NULL,
1512 have_E ? &E : NULL,
1513 prng ? mbedtls_ctr_drbg_random : NULL,
1514 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001515exit:
1516
1517 mbedtls_ctr_drbg_free( &ctr_drbg );
1518 mbedtls_entropy_free( &entropy );
1519
1520 mbedtls_mpi_free( &N );
1521 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1522 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1523}
1524/* END_CASE */
1525
Hanno Beckerc77ab892017-08-23 11:01:06 +01001526/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001527void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1528 data_t *input_Q, data_t *input_D,
1529 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001530 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001531{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001532 /* Exported buffers */
Azim Khand30ca132017-06-09 04:32:58 +01001533 unsigned char bufNe[1000];
1534 unsigned char bufPe[1000];
1535 unsigned char bufQe[1000];
1536 unsigned char bufDe[1000];
1537 unsigned char bufEe[1000];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001538
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001539 mbedtls_rsa_context ctx;
1540
1541 mbedtls_rsa_init( &ctx, 0, 0 );
1542
1543 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001544 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001545 input_N->len ? input_N->x : NULL, input_N->len,
1546 input_P->len ? input_P->x : NULL, input_P->len,
1547 input_Q->len ? input_Q->x : NULL, input_Q->len,
1548 input_D->len ? input_D->x : NULL, input_D->len,
1549 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001550
Hanno Becker7f25f852017-10-10 16:56:22 +01001551 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001552
1553 /*
1554 * Export parameters and compare to original ones.
1555 */
1556
1557 /* N and E must always be present. */
1558 if( !successive )
1559 {
Azim Khand30ca132017-06-09 04:32:58 +01001560 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001561 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001562 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001563 }
1564 else
1565 {
Azim Khand30ca132017-06-09 04:32:58 +01001566 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001567 NULL, 0, NULL, 0, NULL, 0,
1568 NULL, 0 ) == 0 );
1569 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1570 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001571 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001572 }
Azim Khand30ca132017-06-09 04:32:58 +01001573 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1574 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001575
1576 /* If we were providing enough information to setup a complete private context,
1577 * we expect to be able to export all core parameters. */
1578
1579 if( is_priv )
1580 {
1581 if( !successive )
1582 {
1583 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001584 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1585 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1586 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001587 NULL, 0 ) == 0 );
1588 }
1589 else
1590 {
1591 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001592 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001593 NULL, 0, NULL, 0,
1594 NULL, 0 ) == 0 );
1595
1596 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001597 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001598 NULL, 0, NULL, 0 ) == 0 );
1599
Azim Khand30ca132017-06-09 04:32:58 +01001600 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1601 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001602 NULL, 0 ) == 0 );
1603 }
1604
Azim Khand30ca132017-06-09 04:32:58 +01001605 if( input_P->len )
1606 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001607
Azim Khand30ca132017-06-09 04:32:58 +01001608 if( input_Q->len )
1609 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001610
Azim Khand30ca132017-06-09 04:32:58 +01001611 if( input_D->len )
1612 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001613
1614 }
1615
1616exit:
1617 mbedtls_rsa_free( &ctx );
1618}
1619/* END_CASE */
1620
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001621/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001622void mbedtls_rsa_import_raw( data_t *input_N,
1623 data_t *input_P, data_t *input_Q,
1624 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001625 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001626 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001627 int res_check,
1628 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001629{
Hanno Beckere1582a82017-09-29 11:51:05 +01001630 /* Buffers used for encryption-decryption test */
1631 unsigned char *buf_orig = NULL;
1632 unsigned char *buf_enc = NULL;
1633 unsigned char *buf_dec = NULL;
1634
Hanno Beckerc77ab892017-08-23 11:01:06 +01001635 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001636 mbedtls_entropy_context entropy;
1637 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001638
Hanno Beckerc77ab892017-08-23 11:01:06 +01001639 const char *pers = "test_suite_rsa";
1640
1641 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001642 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001643 mbedtls_rsa_init( &ctx, 0, 0 );
1644
Hanno Beckerc77ab892017-08-23 11:01:06 +01001645 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1646 &entropy, (const unsigned char *) pers,
1647 strlen( pers ) ) == 0 );
1648
Hanno Beckerc77ab892017-08-23 11:01:06 +01001649 if( !successive )
1650 {
1651 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001652 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1653 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1654 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1655 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1656 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001657 }
1658 else
1659 {
1660 /* Import N, P, Q, D, E separately.
1661 * This should make no functional difference. */
1662
1663 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001664 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001665 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1666
1667 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1668 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001669 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001670 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1671
1672 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1673 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001674 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001675 NULL, 0, NULL, 0 ) == 0 );
1676
1677 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1678 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001679 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001680 NULL, 0 ) == 0 );
1681
1682 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1683 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001684 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001685 }
1686
Hanno Becker04877a42017-10-11 10:01:33 +01001687 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001688
Hanno Beckere1582a82017-09-29 11:51:05 +01001689 /* On expected success, perform some public and private
1690 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001691 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001692 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001693 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001694 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1695 else
1696 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1697
1698 if( res_check != 0 )
1699 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001700
1701 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1702 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1703 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1704 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1705 goto exit;
1706
1707 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1708 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1709
1710 /* Make sure the number we're generating is smaller than the modulus */
1711 buf_orig[0] = 0x00;
1712
1713 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1714
1715 if( is_priv )
1716 {
1717 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1718 &ctr_drbg, buf_enc,
1719 buf_dec ) == 0 );
1720
1721 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1722 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1723 }
1724 }
1725
Hanno Beckerc77ab892017-08-23 11:01:06 +01001726exit:
1727
Hanno Becker3f3ae852017-10-02 10:08:39 +01001728 mbedtls_free( buf_orig );
1729 mbedtls_free( buf_enc );
1730 mbedtls_free( buf_dec );
1731
Hanno Beckerc77ab892017-08-23 11:01:06 +01001732 mbedtls_rsa_free( &ctx );
1733
1734 mbedtls_ctr_drbg_free( &ctr_drbg );
1735 mbedtls_entropy_free( &entropy );
1736
1737}
1738/* END_CASE */
1739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001741void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001742{
Andres AG93012e82016-09-09 09:10:28 +01001743 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001744}
Paul Bakker33b43f12013-08-20 11:48:36 +02001745/* END_CASE */