blob: 112c4fc7b6e58093085b327b63a136d0d294568f [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/rsa.h"
Chris Jones66a4cd42021-03-09 16:04:12 +00003#include "rsa_alt_helpers.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00004#include "mbedtls/md2.h"
5#include "mbedtls/md4.h"
6#include "mbedtls/md5.h"
7#include "mbedtls/sha1.h"
8#include "mbedtls/sha256.h"
9#include "mbedtls/sha512.h"
10#include "mbedtls/entropy.h"
11#include "mbedtls/ctr_drbg.h"
Hanno Becker47deec42017-07-24 12:27:09 +010012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +000014
Paul Bakker33b43f12013-08-20 11:48:36 +020015/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +020017 * END_DEPENDENCIES
18 */
Paul Bakker5690efc2011-05-26 13:16:06 +000019
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050020/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
21void rsa_invalid_param( )
22{
23 mbedtls_rsa_context ctx;
24 const int valid_padding = MBEDTLS_RSA_PKCS_V21;
25 const int invalid_padding = 42;
26 const int valid_mode = MBEDTLS_RSA_PRIVATE;
27 const int invalid_mode = 42;
28 unsigned char buf[42] = { 0 };
29 size_t olen;
30
31 TEST_INVALID_PARAM( mbedtls_rsa_init( NULL, valid_padding, 0 ) );
32 TEST_INVALID_PARAM( mbedtls_rsa_init( &ctx, invalid_padding, 0 ) );
33 TEST_VALID_PARAM( mbedtls_rsa_free( NULL ) );
34
35 /* No more variants because only the first argument must be non-NULL. */
36 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
37 mbedtls_rsa_import( NULL, NULL, NULL,
38 NULL, NULL, NULL ) );
39 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
40 mbedtls_rsa_import_raw( NULL,
41 NULL, 0,
42 NULL, 0,
43 NULL, 0,
44 NULL, 0,
45 NULL, 0 ) );
46
47 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
48 mbedtls_rsa_complete( NULL ) );
49
50 /* No more variants because only the first argument must be non-NULL. */
51 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
52 mbedtls_rsa_export( NULL, NULL, NULL,
53 NULL, NULL, NULL ) );
54 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
55 mbedtls_rsa_export_raw( NULL,
56 NULL, 0,
57 NULL, 0,
58 NULL, 0,
59 NULL, 0,
60 NULL, 0 ) );
61 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
62 mbedtls_rsa_export_crt( NULL, NULL, NULL, NULL ) );
63
64 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( NULL,
65 valid_padding, 0 ) );
66 TEST_INVALID_PARAM( mbedtls_rsa_set_padding( &ctx,
67 invalid_padding, 0 ) );
68
69 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +020070 mbedtls_rsa_gen_key( NULL,
71 mbedtls_test_rnd_std_rand,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072 NULL, 0, 0 ) );
73 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
74 mbedtls_rsa_gen_key( &ctx, NULL,
75 NULL, 0, 0 ) );
76
77 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
78 mbedtls_rsa_check_pubkey( NULL ) );
79 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
80 mbedtls_rsa_check_privkey( NULL ) );
81
82 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
83 mbedtls_rsa_check_pub_priv( NULL, &ctx ) );
84 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
85 mbedtls_rsa_check_pub_priv( &ctx, NULL ) );
86
87 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
88 mbedtls_rsa_public( NULL, buf, buf ) );
89 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
90 mbedtls_rsa_public( &ctx, NULL, buf ) );
91 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
92 mbedtls_rsa_public( &ctx, buf, NULL ) );
93
94 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
95 mbedtls_rsa_private( NULL, NULL, NULL,
96 buf, buf ) );
97 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
98 mbedtls_rsa_private( &ctx, NULL, NULL,
99 NULL, buf ) );
100 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
101 mbedtls_rsa_private( &ctx, NULL, NULL,
102 buf, NULL ) );
103
104 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
105 mbedtls_rsa_pkcs1_encrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500106 sizeof( buf ), buf,
107 buf ) );
108 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
109 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 sizeof( buf ), NULL,
111 buf ) );
112 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
113 mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500114 sizeof( buf ), buf,
115 NULL ) );
116
117 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
118 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100119 NULL, sizeof( buf ),
120 buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
122 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100123 NULL, sizeof( buf ),
124 NULL, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500125 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
126 mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
Thomas Daubney53e4ac62021-05-13 18:26:49 +0100127 NULL, sizeof( buf ),
128 buf, NULL ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500129
130 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
131 mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132 buf, sizeof( buf ),
133 sizeof( buf ), buf,
134 buf ) );
135 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
136 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 NULL, sizeof( buf ),
138 sizeof( buf ), buf,
139 buf ) );
140 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
141 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 buf, sizeof( buf ),
143 sizeof( buf ), NULL,
144 buf ) );
145 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
146 mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500147 buf, sizeof( buf ),
148 sizeof( buf ), buf,
149 NULL ) );
150
151 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
152 mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100153 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500154 buf, buf, 42 ) );
155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
156 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100157 NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500158 buf, buf, 42 ) );
159 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
160 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100161 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500162 NULL, buf, 42 ) );
163 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
164 mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100165 &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500166 buf, NULL, 42 ) );
167
168 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
169 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100170 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500171 buf, buf, 42 ) );
172 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
173 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100174 NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500175 buf, buf, 42 ) );
176 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
177 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100178 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500179 NULL, buf, 42 ) );
180 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
181 mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
Thomas Daubney34733082021-05-12 09:24:29 +0100182 NULL, &olen,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 buf, NULL, 42 ) );
184
185 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
186 mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187 buf, sizeof( buf ),
188 &olen,
189 buf, buf, 42 ) );
190 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
191 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500192 NULL, sizeof( buf ),
193 NULL,
194 buf, buf, 42 ) );
195 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
196 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 buf, sizeof( buf ),
198 &olen,
199 NULL, buf, 42 ) );
200 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
201 mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 buf, sizeof( buf ),
203 &olen,
204 buf, NULL, 42 ) );
205
206 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
207 mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500208 0, sizeof( buf ), buf,
209 buf ) );
210 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
211 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 0, sizeof( buf ), NULL,
213 buf ) );
214 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
215 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216 0, sizeof( buf ), buf,
217 NULL ) );
218 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
219 mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500220 MBEDTLS_MD_SHA1,
221 0, NULL,
222 buf ) );
223
224 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
225 mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500226 0, sizeof( buf ), buf,
227 buf ) );
228 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
229 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500230 0, sizeof( buf ), NULL,
231 buf ) );
232 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
233 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500234 0, sizeof( buf ), buf,
235 NULL ) );
236 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
237 mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500238 MBEDTLS_MD_SHA1,
239 0, NULL,
240 buf ) );
241
242 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
243 mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500244 0, sizeof( buf ), buf,
245 buf ) );
246 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
247 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500248 0, sizeof( buf ), NULL,
249 buf ) );
250 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
251 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500252 0, sizeof( buf ), buf,
253 NULL ) );
254 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
255 mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500256 MBEDTLS_MD_SHA1,
257 0, NULL,
258 buf ) );
259
260 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Cédric Meutera05cbec2020-04-25 15:02:34 +0200261 mbedtls_rsa_rsassa_pss_sign_ext( NULL, NULL, NULL,
262 0, sizeof( buf ), buf,
263 MBEDTLS_RSA_SALT_LEN_ANY,
264 buf ) );
265 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
266 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
267 0, sizeof( buf ), NULL,
268 MBEDTLS_RSA_SALT_LEN_ANY,
269 buf ) );
270 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
271 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
272 0, sizeof( buf ), buf,
273 MBEDTLS_RSA_SALT_LEN_ANY,
274 NULL ) );
275 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
276 mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL,
277 MBEDTLS_MD_SHA1,
278 0, NULL,
279 MBEDTLS_RSA_SALT_LEN_ANY,
280 buf ) );
281
282 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100283 mbedtls_rsa_pkcs1_verify( NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500284 0, sizeof( buf ), buf,
285 buf ) );
286 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100287 mbedtls_rsa_pkcs1_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288 0, sizeof( buf ), NULL,
289 buf ) );
290 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100291 mbedtls_rsa_pkcs1_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500292 0, sizeof( buf ), buf,
293 NULL ) );
294 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100295 mbedtls_rsa_pkcs1_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500296 MBEDTLS_MD_SHA1, 0, NULL,
297 buf ) );
298
299 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
300 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
301 NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100302 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500303 0, sizeof( buf ), buf,
304 buf ) );
305 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
306 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
307 NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100308 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500309 0, sizeof( buf ),
310 NULL, buf ) );
311 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
312 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
313 NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100314 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500315 0, sizeof( buf ), buf,
316 NULL ) );
317 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
318 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
319 NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100320 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500321 MBEDTLS_MD_SHA1,
322 0, NULL,
323 buf ) );
324
325 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
326 mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100327 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500328 0, sizeof( buf ),
329 buf, buf ) );
330 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
331 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100332 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500333 0, sizeof( buf ),
334 NULL, buf ) );
335 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
336 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100337 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500338 0, sizeof( buf ),
339 buf, NULL ) );
340 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
341 mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100342 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500343 MBEDTLS_MD_SHA1,
344 0, NULL,
345 buf ) );
346
347 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
348 mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100349 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500350 0, sizeof( buf ),
351 buf,
352 0, 0,
353 buf ) );
354 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
355 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100356 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500357 0, sizeof( buf ),
358 NULL, 0, 0,
359 buf ) );
360 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
361 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100362 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500363 0, sizeof( buf ),
364 buf, 0, 0,
365 NULL ) );
366 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
367 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100368 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500369 MBEDTLS_MD_SHA1,
370 0, NULL,
371 0, 0,
372 buf ) );
373
374 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
375 mbedtls_rsa_copy( NULL, &ctx ) );
376 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
377 mbedtls_rsa_copy( &ctx, NULL ) );
378
379exit:
380 return;
381}
382/* END_CASE */
383
Paul Bakker33b43f12013-08-20 11:48:36 +0200384/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100385void rsa_init_free( int reinit )
386{
387 mbedtls_rsa_context ctx;
388
389 /* Double free is not explicitly documented to work, but we rely on it
390 * even inside the library so that you can call mbedtls_rsa_free()
391 * unconditionally on an error path without checking whether it has
392 * already been called in the success path. */
393
394 mbedtls_rsa_init( &ctx, 0, 0 );
395 mbedtls_rsa_free( &ctx );
396
397 if( reinit )
398 mbedtls_rsa_init( &ctx, 0, 0 );
399 mbedtls_rsa_free( &ctx );
400
401 /* This test case always succeeds, functionally speaking. A plausible
402 * bug might trigger an invalid pointer dereference or a memory leak. */
403 goto exit;
404}
405/* END_CASE */
406
407/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100408void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100409 int digest, int mod, int radix_P, char * input_P,
410 int radix_Q, char * input_Q, int radix_N,
411 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200412 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000413{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200414 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
415 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100417 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200418 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000419
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100420 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
421 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000423
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200424 memset( hash_result, 0x00, sizeof( hash_result ) );
425 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200426 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000427
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100428 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
429 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
430 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
431 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000432
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100433 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
434 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100435 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000437
Paul Bakker42a29bf2009-07-07 20:18:41 +0000438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100440 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 +0000441
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200442 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100443 &rnd_info, digest, 0, hash_result,
444 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200445 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000446 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000447
Ronald Cronac6ae352020-06-26 14:33:03 +0200448 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
449 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000450 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000451
Paul Bakkerbd51b262014-07-10 15:26:12 +0200452exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100453 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
454 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000456}
Paul Bakker33b43f12013-08-20 11:48:36 +0200457/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000458
Paul Bakker33b43f12013-08-20 11:48:36 +0200459/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100460void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100461 int digest, int mod, int radix_N,
462 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100463 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000464{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200465 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000467
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100468 mbedtls_mpi N, E;
469
470 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200472 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000473
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100474 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
475 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
476 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
477 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000479
Paul Bakker42a29bf2009-07-07 20:18:41 +0000480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100482 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 +0000483
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100484 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, digest, 0, hash_result, result_str->x ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100485
Paul Bakkerbd51b262014-07-10 15:26:12 +0200486exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100487 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000489}
Paul Bakker33b43f12013-08-20 11:48:36 +0200490/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000491
Paul Bakker821fb082009-07-12 13:26:42 +0000492
Paul Bakker33b43f12013-08-20 11:48:36 +0200493/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100494void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100495 int padding_mode, int mod, int radix_P,
496 char * input_P, int radix_Q, char * input_Q,
497 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200498 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000499{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200500 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100502 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200503 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100506 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
507 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000508
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200509 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200510 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000511
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100512 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
513 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
514 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
515 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000516
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100517 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
518 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100519 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000521
Paul Bakker821fb082009-07-12 13:26:42 +0000522
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200523 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100524 &rnd_info, MBEDTLS_MD_NONE,
525 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200526 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000527
Paul Bakker821fb082009-07-12 13:26:42 +0000528
Ronald Cronac6ae352020-06-26 14:33:03 +0200529 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
530 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000531
Paul Bakkerbd51b262014-07-10 15:26:12 +0200532exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100533 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
534 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000537}
Paul Bakker33b43f12013-08-20 11:48:36 +0200538/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000539
Paul Bakker33b43f12013-08-20 11:48:36 +0200540/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100541void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200542 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100543 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100544 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000545{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200546 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000548
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100549 mbedtls_mpi N, E;
550 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100553 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000554
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100555 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
556 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000557
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100558 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
559 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000561
Paul Bakker821fb082009-07-12 13:26:42 +0000562
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100563 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, MBEDTLS_MD_NONE, hash_result->len, hash_result->x, result_str->x ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100564
Paul Bakkerbd51b262014-07-10 15:26:12 +0200565exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100566 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000568}
Paul Bakker33b43f12013-08-20 11:48:36 +0200569/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000570
Paul Bakker33b43f12013-08-20 11:48:36 +0200571/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100572void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100573 int mod, int radix_N, char * input_N,
574 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200575 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000576{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200577 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200579 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000580
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100581 mbedtls_mpi N, E;
582 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
583
Ronald Cron351f0ee2020-06-10 12:12:18 +0200584 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200587 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000588
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100589 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
590 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000591
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100592 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
593 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000595
Paul Bakker42a29bf2009-07-07 20:18:41 +0000596
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200597 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
598 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100599 &rnd_info, message_str->len,
600 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200601 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200602 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000603 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000604
Ronald Cronac6ae352020-06-26 14:33:03 +0200605 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
606 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000607 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100608
Paul Bakkerbd51b262014-07-10 15:26:12 +0200609exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100610 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000612}
Paul Bakker33b43f12013-08-20 11:48:36 +0200613/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000614
Paul Bakker33b43f12013-08-20 11:48:36 +0200615/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100616void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100617 int mod, int radix_N, char * input_N,
618 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200619 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000620{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200621 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000623
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100624 mbedtls_mpi N, E;
625
626 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200628 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000629
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100630 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
631 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000632
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100633 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
634 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000636
Paul Bakkera6656852010-07-18 19:47:14 +0000637
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200638 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100639 NULL, message_str->len,
640 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200641 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200642 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000643 {
Paul Bakkera6656852010-07-18 19:47:14 +0000644
Ronald Cronac6ae352020-06-26 14:33:03 +0200645 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
646 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000647 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100648
Paul Bakkerbd51b262014-07-10 15:26:12 +0200649exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100650 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000652}
Paul Bakker33b43f12013-08-20 11:48:36 +0200653/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000654
Paul Bakker33b43f12013-08-20 11:48:36 +0200655/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100656void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100657 int mod, int radix_P, char * input_P,
658 int radix_Q, char * input_Q, int radix_N,
659 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200660 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100661 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000662{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200663 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000665 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200666 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100667 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000668
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100669 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
670 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000673
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200674 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200675 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000676
Paul Bakker42a29bf2009-07-07 20:18:41 +0000677
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100678 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
679 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
680 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
681 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000682
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100683 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
684 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100685 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000687
Paul Bakker69998dd2009-07-11 19:15:20 +0000688 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000689
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200690 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100691 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200692 &output_len, message_str->x, output,
693 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200694 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000695 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000696
Ronald Cronac6ae352020-06-26 14:33:03 +0200697 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200698 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200699 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000700 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000701
Paul Bakkerbd51b262014-07-10 15:26:12 +0200702exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100703 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
704 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000706}
Paul Bakker33b43f12013-08-20 11:48:36 +0200707/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000708
Paul Bakker33b43f12013-08-20 11:48:36 +0200709/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100710void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100711 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200712 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000713{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200714 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000716
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100717 mbedtls_mpi N, E;
718
719 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
721 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200722 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000723
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100724 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
725 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000726
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100727 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
728 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000730
Paul Bakker821fb082009-07-12 13:26:42 +0000731
Azim Khand30ca132017-06-09 04:32:58 +0100732 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200733 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000734 {
Paul Bakker821fb082009-07-12 13:26:42 +0000735
Ronald Cronac6ae352020-06-26 14:33:03 +0200736 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
737 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000738 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100739
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100740 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200742 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100746
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200747 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100748 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100749 if( result == 0 )
750 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100751
Ronald Cronac6ae352020-06-26 14:33:03 +0200752 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
753 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100754 }
755
Paul Bakkerbd51b262014-07-10 15:26:12 +0200756exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100757 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_rsa_free( &ctx );
759 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000760}
Paul Bakker33b43f12013-08-20 11:48:36 +0200761/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000762
Paul Bakker33b43f12013-08-20 11:48:36 +0200763/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100764void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100765 char * input_P, int radix_Q, char * input_Q,
766 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200767 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100768 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000769{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200770 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100772 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200773 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200774 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000775
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100776 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
777 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
779 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000780
Ronald Cron351f0ee2020-06-10 12:12:18 +0200781 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000782
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100783 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
784 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
785 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
786 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000787
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100788 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
789 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100790 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000792
Paul Bakker821fb082009-07-12 13:26:42 +0000793
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200794 /* repeat three times to test updating of blinding values */
795 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000796 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200797 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200798 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
799 &rnd_info, message_str->x,
800 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200801 if( result == 0 )
802 {
Paul Bakker821fb082009-07-12 13:26:42 +0000803
Ronald Cronac6ae352020-06-26 14:33:03 +0200804 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200805 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200806 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200807 }
Paul Bakker821fb082009-07-12 13:26:42 +0000808 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000809
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100810 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200812 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100816
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200817 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200818 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
819 &rnd_info, message_str->x,
820 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100821 if( result == 0 )
822 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100823
Ronald Cronac6ae352020-06-26 14:33:03 +0200824 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200825 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200826 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100827 }
828
Paul Bakkerbd51b262014-07-10 15:26:12 +0200829exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100830 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
831 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000834}
Paul Bakker33b43f12013-08-20 11:48:36 +0200835/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000836
Paul Bakker33b43f12013-08-20 11:48:36 +0200837/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100838void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000839{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_rsa_context ctx;
841 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000844}
Paul Bakker33b43f12013-08-20 11:48:36 +0200845/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000846
Paul Bakker33b43f12013-08-20 11:48:36 +0200847/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100848void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
849 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000850{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100852 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000853
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100854 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000856
Paul Bakker33b43f12013-08-20 11:48:36 +0200857 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000858 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100859 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000860 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200861 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000862 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100863 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000864 }
865
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100866 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100868
Paul Bakkerbd51b262014-07-10 15:26:12 +0200869exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100870 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000872}
Paul Bakker33b43f12013-08-20 11:48:36 +0200873/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000874
Paul Bakker33b43f12013-08-20 11:48:36 +0200875/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100876void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
877 int radix_Q, char * input_Q, int radix_N,
878 char * input_N, int radix_E, char * input_E,
879 int radix_D, char * input_D, int radix_DP,
880 char * input_DP, int radix_DQ,
881 char * input_DQ, int radix_QP,
882 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000883{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000887
Paul Bakker33b43f12013-08-20 11:48:36 +0200888 ctx.len = mod / 8;
889 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000892 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200893 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000896 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200897 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000900 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200901 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000904 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200905 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000908 }
Hanno Becker131134f2017-08-23 08:31:07 +0100909#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200910 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000913 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200914 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000917 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200918 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000921 }
Hanno Becker131134f2017-08-23 08:31:07 +0100922#else
923 ((void) radix_DP); ((void) input_DP);
924 ((void) radix_DQ); ((void) input_DQ);
925 ((void) radix_QP); ((void) input_QP);
926#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100929
Paul Bakkerbd51b262014-07-10 15:26:12 +0200930exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000932}
Paul Bakker33b43f12013-08-20 11:48:36 +0200933/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000934
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100935/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100936void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
937 int radix_Epub, char * input_Epub, int radix_P,
938 char * input_P, int radix_Q, char * input_Q,
939 int radix_N, char * input_N, int radix_E,
940 char * input_E, int radix_D, char * input_D,
941 int radix_DP, char * input_DP, int radix_DQ,
942 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100943 int result )
944{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
948 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100949
950 pub.len = mod / 8;
951 prv.len = mod / 8;
952
953 if( strlen( input_Npub ) )
954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100956 }
957 if( strlen( input_Epub ) )
958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100960 }
961
962 if( strlen( input_P ) )
963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100965 }
966 if( strlen( input_Q ) )
967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100969 }
970 if( strlen( input_N ) )
971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100973 }
974 if( strlen( input_E ) )
975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100977 }
978 if( strlen( input_D ) )
979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100981 }
Hanno Becker131134f2017-08-23 08:31:07 +0100982#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100983 if( strlen( input_DP ) )
984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100986 }
987 if( strlen( input_DQ ) )
988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100990 }
991 if( strlen( input_QP ) )
992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100994 }
Hanno Becker131134f2017-08-23 08:31:07 +0100995#else
996 ((void) radix_DP); ((void) input_DP);
997 ((void) radix_DQ); ((void) input_DQ);
998 ((void) radix_QP); ((void) input_QP);
999#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001002
1003exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_rsa_free( &pub );
1005 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +01001006}
1007/* END_CASE */
1008
Hanno Beckerd4a872e2017-09-07 08:09:33 +01001009/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +00001011{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_rsa_context ctx;
1013 mbedtls_entropy_context entropy;
1014 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001015 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001016
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001017 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001019 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001020
Hanno Beckera47023e2017-12-22 17:08:03 +00001021 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1022 &entropy, (const unsigned char *) pers,
1023 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001026 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001029 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001030 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001031
Paul Bakkerbd51b262014-07-10 15:26:12 +02001032exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 mbedtls_rsa_free( &ctx );
1034 mbedtls_ctr_drbg_free( &ctr_drbg );
1035 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001036}
Paul Bakker33b43f12013-08-20 11:48:36 +02001037/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001038
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001039/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001040void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001041 int radix_D, char *input_D,
1042 int radix_E, char *input_E,
1043 int radix_P, char *output_P,
1044 int radix_Q, char *output_Q,
1045 int corrupt, int result )
1046{
1047 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1048
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001049 mbedtls_mpi_init( &N );
1050 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1051 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1052 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1053
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001054 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1055 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1056 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1057 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1058 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1059
1060 if( corrupt )
1061 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1062
1063 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001064 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001065
1066 if( !corrupt )
1067 {
1068 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1069 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1070 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1071 }
1072
1073exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001074 mbedtls_mpi_free( &N );
1075 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1076 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1077 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001078}
1079/* END_CASE */
1080
Hanno Becker6b4ce492017-08-23 11:00:21 +01001081/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001082void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1083 int radix_Q, char *input_Q,
1084 int radix_E, char *input_E,
1085 int radix_D, char *output_D,
1086 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001087{
1088 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1089
1090 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1091 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1092 mbedtls_mpi_init( &E );
1093 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1094
1095 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1096 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1097 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1098 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1099
1100 if( corrupt )
1101 {
1102 /* Make E even */
1103 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1104 }
1105
1106 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001107 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1108 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001109
1110 if( !corrupt )
1111 {
1112 /*
1113 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1114 */
1115
1116 /* Replace P,Q by P-1, Q-1 */
1117 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1118 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1119
1120 /* Check D == Dp modulo P-1 */
1121 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1122 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1123 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1124
1125 /* Check D == Dp modulo Q-1 */
1126 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1127 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1128 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1129 }
1130
1131exit:
1132
1133 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1134 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1135 mbedtls_mpi_free( &E );
1136 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1137}
1138/* END_CASE */
1139
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001140/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001141void mbedtls_rsa_import( int radix_N, char *input_N,
1142 int radix_P, char *input_P,
1143 int radix_Q, char *input_Q,
1144 int radix_D, char *input_D,
1145 int radix_E, char *input_E,
1146 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001147 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001148 int res_check,
1149 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001150{
1151 mbedtls_mpi N, P, Q, D, E;
1152 mbedtls_rsa_context ctx;
1153
Hanno Beckere1582a82017-09-29 11:51:05 +01001154 /* Buffers used for encryption-decryption test */
1155 unsigned char *buf_orig = NULL;
1156 unsigned char *buf_enc = NULL;
1157 unsigned char *buf_dec = NULL;
1158
Hanno Beckerc77ab892017-08-23 11:01:06 +01001159 mbedtls_entropy_context entropy;
1160 mbedtls_ctr_drbg_context ctr_drbg;
1161 const char *pers = "test_suite_rsa";
1162
Hanno Becker4d6e8342017-09-29 11:50:18 +01001163 const int have_N = ( strlen( input_N ) > 0 );
1164 const int have_P = ( strlen( input_P ) > 0 );
1165 const int have_Q = ( strlen( input_Q ) > 0 );
1166 const int have_D = ( strlen( input_D ) > 0 );
1167 const int have_E = ( strlen( input_E ) > 0 );
1168
Hanno Beckerc77ab892017-08-23 11:01:06 +01001169 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001170 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001171 mbedtls_rsa_init( &ctx, 0, 0 );
1172
1173 mbedtls_mpi_init( &N );
1174 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1175 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1176
Hanno Beckerd4d60572018-01-10 07:12:01 +00001177 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1178 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1179
Hanno Becker4d6e8342017-09-29 11:50:18 +01001180 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001181 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1182
Hanno Becker4d6e8342017-09-29 11:50:18 +01001183 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001184 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1185
Hanno Becker4d6e8342017-09-29 11:50:18 +01001186 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001187 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1188
Hanno Becker4d6e8342017-09-29 11:50:18 +01001189 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001190 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1191
Hanno Becker4d6e8342017-09-29 11:50:18 +01001192 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001193 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1194
1195 if( !successive )
1196 {
1197 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001198 have_N ? &N : NULL,
1199 have_P ? &P : NULL,
1200 have_Q ? &Q : NULL,
1201 have_D ? &D : NULL,
1202 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001203 }
1204 else
1205 {
1206 /* Import N, P, Q, D, E separately.
1207 * This should make no functional difference. */
1208
1209 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001210 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001211 NULL, NULL, NULL, NULL ) == 0 );
1212
1213 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1214 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001215 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001216 NULL, NULL, NULL ) == 0 );
1217
1218 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1219 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001220 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001221 NULL, NULL ) == 0 );
1222
1223 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1224 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001225 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001226 NULL ) == 0 );
1227
1228 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1229 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001230 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001231 }
1232
Hanno Becker04877a42017-10-11 10:01:33 +01001233 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001234
Hanno Beckere1582a82017-09-29 11:51:05 +01001235 /* On expected success, perform some public and private
1236 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001237 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001238 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001239 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001240 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1241 else
1242 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1243
1244 if( res_check != 0 )
1245 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001246
1247 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1248 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1249 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1250 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1251 goto exit;
1252
1253 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1254 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1255
1256 /* Make sure the number we're generating is smaller than the modulus */
1257 buf_orig[0] = 0x00;
1258
1259 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1260
1261 if( is_priv )
1262 {
1263 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1264 &ctr_drbg, buf_enc,
1265 buf_dec ) == 0 );
1266
1267 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1268 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1269 }
1270 }
1271
Hanno Beckerc77ab892017-08-23 11:01:06 +01001272exit:
1273
Hanno Beckere1582a82017-09-29 11:51:05 +01001274 mbedtls_free( buf_orig );
1275 mbedtls_free( buf_enc );
1276 mbedtls_free( buf_dec );
1277
Hanno Beckerc77ab892017-08-23 11:01:06 +01001278 mbedtls_rsa_free( &ctx );
1279
1280 mbedtls_ctr_drbg_free( &ctr_drbg );
1281 mbedtls_entropy_free( &entropy );
1282
1283 mbedtls_mpi_free( &N );
1284 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1285 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1286}
1287/* END_CASE */
1288
Hanno Becker417f2d62017-08-23 11:44:51 +01001289/* BEGIN_CASE */
1290void mbedtls_rsa_export( int radix_N, char *input_N,
1291 int radix_P, char *input_P,
1292 int radix_Q, char *input_Q,
1293 int radix_D, char *input_D,
1294 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001295 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001296 int successive )
1297{
1298 /* Original MPI's with which we set up the RSA context */
1299 mbedtls_mpi N, P, Q, D, E;
1300
1301 /* Exported MPI's */
1302 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1303
1304 const int have_N = ( strlen( input_N ) > 0 );
1305 const int have_P = ( strlen( input_P ) > 0 );
1306 const int have_Q = ( strlen( input_Q ) > 0 );
1307 const int have_D = ( strlen( input_D ) > 0 );
1308 const int have_E = ( strlen( input_E ) > 0 );
1309
Hanno Becker417f2d62017-08-23 11:44:51 +01001310 mbedtls_rsa_context ctx;
1311
1312 mbedtls_rsa_init( &ctx, 0, 0 );
1313
1314 mbedtls_mpi_init( &N );
1315 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1316 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1317
1318 mbedtls_mpi_init( &Ne );
1319 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1320 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1321
1322 /* Setup RSA context */
1323
1324 if( have_N )
1325 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1326
1327 if( have_P )
1328 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1329
1330 if( have_Q )
1331 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1332
1333 if( have_D )
1334 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1335
1336 if( have_E )
1337 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1338
1339 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1340 strlen( input_N ) ? &N : NULL,
1341 strlen( input_P ) ? &P : NULL,
1342 strlen( input_Q ) ? &Q : NULL,
1343 strlen( input_D ) ? &D : NULL,
1344 strlen( input_E ) ? &E : NULL ) == 0 );
1345
Hanno Becker7f25f852017-10-10 16:56:22 +01001346 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001347
1348 /*
1349 * Export parameters and compare to original ones.
1350 */
1351
1352 /* N and E must always be present. */
1353 if( !successive )
1354 {
1355 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1356 }
1357 else
1358 {
1359 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1360 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1361 }
1362 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1363 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1364
1365 /* If we were providing enough information to setup a complete private context,
1366 * we expect to be able to export all core parameters. */
1367
1368 if( is_priv )
1369 {
1370 if( !successive )
1371 {
1372 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1373 &De, NULL ) == 0 );
1374 }
1375 else
1376 {
1377 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1378 NULL, NULL ) == 0 );
1379 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1380 NULL, NULL ) == 0 );
1381 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1382 &De, NULL ) == 0 );
1383 }
1384
1385 if( have_P )
1386 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1387
1388 if( have_Q )
1389 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1390
1391 if( have_D )
1392 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1393
1394 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001395 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1396 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001397 }
1398
1399exit:
1400
1401 mbedtls_rsa_free( &ctx );
1402
1403 mbedtls_mpi_free( &N );
1404 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1405 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1406
1407 mbedtls_mpi_free( &Ne );
1408 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1409 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1410}
1411/* END_CASE */
1412
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001413/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001414void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1415 int radix_P, char *input_P,
1416 int radix_Q, char *input_Q,
1417 int radix_D, char *input_D,
1418 int radix_E, char *input_E,
1419 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001420{
1421 /* Original MPI's with which we set up the RSA context */
1422 mbedtls_mpi N, P, Q, D, E;
1423
1424 const int have_N = ( strlen( input_N ) > 0 );
1425 const int have_P = ( strlen( input_P ) > 0 );
1426 const int have_Q = ( strlen( input_Q ) > 0 );
1427 const int have_D = ( strlen( input_D ) > 0 );
1428 const int have_E = ( strlen( input_E ) > 0 );
1429
1430 mbedtls_entropy_context entropy;
1431 mbedtls_ctr_drbg_context ctr_drbg;
1432 const char *pers = "test_suite_rsa";
1433
1434 mbedtls_mpi_init( &N );
1435 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1436 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1437
1438 mbedtls_ctr_drbg_init( &ctr_drbg );
1439 mbedtls_entropy_init( &entropy );
1440 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1441 &entropy, (const unsigned char *) pers,
1442 strlen( pers ) ) == 0 );
1443
1444 if( have_N )
1445 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1446
1447 if( have_P )
1448 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1449
1450 if( have_Q )
1451 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1452
1453 if( have_D )
1454 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1455
1456 if( have_E )
1457 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1458
Hanno Becker750e8b42017-08-25 07:54:27 +01001459 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1460 have_P ? &P : NULL,
1461 have_Q ? &Q : NULL,
1462 have_D ? &D : NULL,
1463 have_E ? &E : NULL,
1464 prng ? mbedtls_ctr_drbg_random : NULL,
1465 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001466exit:
1467
1468 mbedtls_ctr_drbg_free( &ctr_drbg );
1469 mbedtls_entropy_free( &entropy );
1470
1471 mbedtls_mpi_free( &N );
1472 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1473 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1474}
1475/* END_CASE */
1476
Hanno Beckerc77ab892017-08-23 11:01:06 +01001477/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001478void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1479 data_t *input_Q, data_t *input_D,
1480 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001481 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001482{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001483 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001484 unsigned char bufNe[256];
1485 unsigned char bufPe[128];
1486 unsigned char bufQe[128];
1487 unsigned char bufDe[256];
1488 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001489
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001490 mbedtls_rsa_context ctx;
1491
1492 mbedtls_rsa_init( &ctx, 0, 0 );
1493
1494 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001495 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001496 input_N->len ? input_N->x : NULL, input_N->len,
1497 input_P->len ? input_P->x : NULL, input_P->len,
1498 input_Q->len ? input_Q->x : NULL, input_Q->len,
1499 input_D->len ? input_D->x : NULL, input_D->len,
1500 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001501
Hanno Becker7f25f852017-10-10 16:56:22 +01001502 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001503
1504 /*
1505 * Export parameters and compare to original ones.
1506 */
1507
1508 /* N and E must always be present. */
1509 if( !successive )
1510 {
Azim Khand30ca132017-06-09 04:32:58 +01001511 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001512 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001513 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001514 }
1515 else
1516 {
Azim Khand30ca132017-06-09 04:32:58 +01001517 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001518 NULL, 0, NULL, 0, NULL, 0,
1519 NULL, 0 ) == 0 );
1520 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1521 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001522 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001523 }
Azim Khand30ca132017-06-09 04:32:58 +01001524 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1525 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001526
1527 /* If we were providing enough information to setup a complete private context,
1528 * we expect to be able to export all core parameters. */
1529
1530 if( is_priv )
1531 {
1532 if( !successive )
1533 {
1534 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001535 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1536 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1537 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001538 NULL, 0 ) == 0 );
1539 }
1540 else
1541 {
1542 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001543 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001544 NULL, 0, NULL, 0,
1545 NULL, 0 ) == 0 );
1546
1547 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001548 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001549 NULL, 0, NULL, 0 ) == 0 );
1550
Azim Khand30ca132017-06-09 04:32:58 +01001551 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1552 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001553 NULL, 0 ) == 0 );
1554 }
1555
Azim Khand30ca132017-06-09 04:32:58 +01001556 if( input_P->len )
1557 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001558
Azim Khand30ca132017-06-09 04:32:58 +01001559 if( input_Q->len )
1560 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001561
Azim Khand30ca132017-06-09 04:32:58 +01001562 if( input_D->len )
1563 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001564
1565 }
1566
1567exit:
1568 mbedtls_rsa_free( &ctx );
1569}
1570/* END_CASE */
1571
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001572/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001573void mbedtls_rsa_import_raw( data_t *input_N,
1574 data_t *input_P, data_t *input_Q,
1575 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001576 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001577 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001578 int res_check,
1579 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001580{
Hanno Beckere1582a82017-09-29 11:51:05 +01001581 /* Buffers used for encryption-decryption test */
1582 unsigned char *buf_orig = NULL;
1583 unsigned char *buf_enc = NULL;
1584 unsigned char *buf_dec = NULL;
1585
Hanno Beckerc77ab892017-08-23 11:01:06 +01001586 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001587 mbedtls_entropy_context entropy;
1588 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001589
Hanno Beckerc77ab892017-08-23 11:01:06 +01001590 const char *pers = "test_suite_rsa";
1591
1592 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001593 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001594 mbedtls_rsa_init( &ctx, 0, 0 );
1595
Hanno Beckerc77ab892017-08-23 11:01:06 +01001596 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1597 &entropy, (const unsigned char *) pers,
1598 strlen( pers ) ) == 0 );
1599
Hanno Beckerc77ab892017-08-23 11:01:06 +01001600 if( !successive )
1601 {
1602 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001603 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1604 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1605 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1606 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1607 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001608 }
1609 else
1610 {
1611 /* Import N, P, Q, D, E separately.
1612 * This should make no functional difference. */
1613
1614 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001615 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001616 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1617
1618 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1619 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001620 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001621 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1622
1623 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1624 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001625 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001626 NULL, 0, NULL, 0 ) == 0 );
1627
1628 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1629 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001630 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001631 NULL, 0 ) == 0 );
1632
1633 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1634 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001635 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001636 }
1637
Hanno Becker04877a42017-10-11 10:01:33 +01001638 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001639
Hanno Beckere1582a82017-09-29 11:51:05 +01001640 /* On expected success, perform some public and private
1641 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001642 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001643 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001644 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001645 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1646 else
1647 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1648
1649 if( res_check != 0 )
1650 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001651
1652 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1653 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1654 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1655 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1656 goto exit;
1657
1658 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1659 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1660
1661 /* Make sure the number we're generating is smaller than the modulus */
1662 buf_orig[0] = 0x00;
1663
1664 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1665
1666 if( is_priv )
1667 {
1668 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1669 &ctr_drbg, buf_enc,
1670 buf_dec ) == 0 );
1671
1672 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1673 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1674 }
1675 }
1676
Hanno Beckerc77ab892017-08-23 11:01:06 +01001677exit:
1678
Hanno Becker3f3ae852017-10-02 10:08:39 +01001679 mbedtls_free( buf_orig );
1680 mbedtls_free( buf_enc );
1681 mbedtls_free( buf_dec );
1682
Hanno Beckerc77ab892017-08-23 11:01:06 +01001683 mbedtls_rsa_free( &ctx );
1684
1685 mbedtls_ctr_drbg_free( &ctr_drbg );
1686 mbedtls_entropy_free( &entropy );
1687
1688}
1689/* END_CASE */
1690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001692void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001693{
Andres AG93012e82016-09-09 09:10:28 +01001694 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001695}
Paul Bakker33b43f12013-08-20 11:48:36 +02001696/* END_CASE */