blob: 05886ffba82d3dc0f59a563d79fb0bec293f5243 [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,
Thomas Daubney475053d2021-05-19 11:44:27 +0100300 mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500301 0, sizeof( buf ), buf,
302 buf ) );
303 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney475053d2021-05-19 11:44:27 +0100304 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500305 0, sizeof( buf ),
306 NULL, buf ) );
307 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney475053d2021-05-19 11:44:27 +0100308 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500309 0, sizeof( buf ), buf,
310 NULL ) );
311 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney475053d2021-05-19 11:44:27 +0100312 mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500313 MBEDTLS_MD_SHA1,
314 0, NULL,
315 buf ) );
316
317 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney718a53d2021-05-19 12:01:35 +0100318 mbedtls_rsa_rsassa_pss_verify( NULL,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500319 0, sizeof( buf ),
320 buf, buf ) );
321 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney718a53d2021-05-19 12:01:35 +0100322 mbedtls_rsa_rsassa_pss_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500323 0, sizeof( buf ),
324 NULL, buf ) );
325 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney718a53d2021-05-19 12:01:35 +0100326 mbedtls_rsa_rsassa_pss_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500327 0, sizeof( buf ),
328 buf, NULL ) );
329 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney718a53d2021-05-19 12:01:35 +0100330 mbedtls_rsa_rsassa_pss_verify( &ctx,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500331 MBEDTLS_MD_SHA1,
332 0, NULL,
333 buf ) );
334
335 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney578e9ab2021-05-19 12:14:57 +0100336 mbedtls_rsa_rsassa_pss_verify_ext( 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,
340 0, 0,
341 buf ) );
342 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney578e9ab2021-05-19 12:14:57 +0100343 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100344 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500345 0, sizeof( buf ),
346 NULL, 0, 0,
347 buf ) );
348 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney578e9ab2021-05-19 12:14:57 +0100349 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100350 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500351 0, sizeof( buf ),
352 buf, 0, 0,
353 NULL ) );
354 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
Thomas Daubney578e9ab2021-05-19 12:14:57 +0100355 mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL,
Thomas Daubney28b55852021-05-18 18:30:00 +0100356 MBEDTLS_RSA_PUBLIC,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500357 MBEDTLS_MD_SHA1,
358 0, NULL,
359 0, 0,
360 buf ) );
361
362 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
363 mbedtls_rsa_copy( NULL, &ctx ) );
364 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
365 mbedtls_rsa_copy( &ctx, NULL ) );
366
367exit:
368 return;
369}
370/* END_CASE */
371
Paul Bakker33b43f12013-08-20 11:48:36 +0200372/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +0100373void rsa_init_free( int reinit )
374{
375 mbedtls_rsa_context ctx;
376
377 /* Double free is not explicitly documented to work, but we rely on it
378 * even inside the library so that you can call mbedtls_rsa_free()
379 * unconditionally on an error path without checking whether it has
380 * already been called in the success path. */
381
382 mbedtls_rsa_init( &ctx, 0, 0 );
383 mbedtls_rsa_free( &ctx );
384
385 if( reinit )
386 mbedtls_rsa_init( &ctx, 0, 0 );
387 mbedtls_rsa_free( &ctx );
388
389 /* This test case always succeeds, functionally speaking. A plausible
390 * bug might trigger an invalid pointer dereference or a memory leak. */
391 goto exit;
392}
393/* END_CASE */
394
395/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100396void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100397 int digest, int mod, int radix_P, char * input_P,
398 int radix_Q, char * input_Q, int radix_N,
399 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200400 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000401{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200402 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
403 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100405 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200406 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000407
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100408 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
409 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000411
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200412 memset( hash_result, 0x00, sizeof( hash_result ) );
413 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200414 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000415
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100416 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
417 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
418 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
419 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000420
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100421 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
422 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100423 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000425
Paul Bakker42a29bf2009-07-07 20:18:41 +0000426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100428 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 +0000429
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200430 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100431 &rnd_info, digest, 0, hash_result,
432 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200433 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000434 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000435
Ronald Cronac6ae352020-06-26 14:33:03 +0200436 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
437 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000438 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000439
Paul Bakkerbd51b262014-07-10 15:26:12 +0200440exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100441 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
442 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000444}
Paul Bakker33b43f12013-08-20 11:48:36 +0200445/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000446
Paul Bakker33b43f12013-08-20 11:48:36 +0200447/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100448void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100449 int digest, int mod, int radix_N,
450 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100451 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000452{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200453 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000455
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100456 mbedtls_mpi N, E;
457
458 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200460 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000461
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100462 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
463 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
464 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
465 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000467
Paul Bakker42a29bf2009-07-07 20:18:41 +0000468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100470 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 +0000471
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100472 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, digest, 0, hash_result, result_str->x ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100473
Paul Bakkerbd51b262014-07-10 15:26:12 +0200474exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100475 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000477}
Paul Bakker33b43f12013-08-20 11:48:36 +0200478/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000479
Paul Bakker821fb082009-07-12 13:26:42 +0000480
Paul Bakker33b43f12013-08-20 11:48:36 +0200481/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100482void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100483 int padding_mode, int mod, int radix_P,
484 char * input_P, int radix_Q, char * input_Q,
485 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200486 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000487{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200488 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100490 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200491 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100494 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
495 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000496
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200497 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200498 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000499
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100500 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
501 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
502 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
503 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000504
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100505 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
506 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100507 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000509
Paul Bakker821fb082009-07-12 13:26:42 +0000510
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200511 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100512 &rnd_info, MBEDTLS_MD_NONE,
513 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200514 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000515
Paul Bakker821fb082009-07-12 13:26:42 +0000516
Ronald Cronac6ae352020-06-26 14:33:03 +0200517 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
518 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000519
Paul Bakkerbd51b262014-07-10 15:26:12 +0200520exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100521 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
522 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000525}
Paul Bakker33b43f12013-08-20 11:48:36 +0200526/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000527
Paul Bakker33b43f12013-08-20 11:48:36 +0200528/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100529void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200530 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100531 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100532 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000533{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200534 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000536
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100537 mbedtls_mpi N, E;
538 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100541 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000542
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100543 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
544 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000545
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100546 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
547 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000549
Paul Bakker821fb082009-07-12 13:26:42 +0000550
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100551 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 +0100552
Paul Bakkerbd51b262014-07-10 15:26:12 +0200553exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100554 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000556}
Paul Bakker33b43f12013-08-20 11:48:36 +0200557/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000558
Paul Bakker33b43f12013-08-20 11:48:36 +0200559/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100560void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100561 int mod, int radix_N, char * input_N,
562 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200563 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000564{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200565 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200567 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000568
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100569 mbedtls_mpi N, E;
570 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
571
Ronald Cron351f0ee2020-06-10 12:12:18 +0200572 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200575 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000576
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100577 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
578 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000579
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100580 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
581 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000583
Paul Bakker42a29bf2009-07-07 20:18:41 +0000584
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200585 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
586 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100587 &rnd_info, message_str->len,
588 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200589 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200590 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000591 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000592
Ronald Cronac6ae352020-06-26 14:33:03 +0200593 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
594 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000595 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100596
Paul Bakkerbd51b262014-07-10 15:26:12 +0200597exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100598 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000600}
Paul Bakker33b43f12013-08-20 11:48:36 +0200601/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000602
Paul Bakker33b43f12013-08-20 11:48:36 +0200603/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100604void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100605 int mod, int radix_N, char * input_N,
606 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200607 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000608{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200609 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000611
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100612 mbedtls_mpi N, E;
613
614 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200616 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000617
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100618 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
619 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000620
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100621 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
622 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000624
Paul Bakkera6656852010-07-18 19:47:14 +0000625
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200626 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100627 NULL, message_str->len,
628 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200629 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200630 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000631 {
Paul Bakkera6656852010-07-18 19:47:14 +0000632
Ronald Cronac6ae352020-06-26 14:33:03 +0200633 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
634 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000635 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100636
Paul Bakkerbd51b262014-07-10 15:26:12 +0200637exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100638 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000640}
Paul Bakker33b43f12013-08-20 11:48:36 +0200641/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000642
Paul Bakker33b43f12013-08-20 11:48:36 +0200643/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100644void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100645 int mod, int radix_P, char * input_P,
646 int radix_Q, char * input_Q, int radix_N,
647 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200648 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100649 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000650{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200651 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000653 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200654 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100655 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000656
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100657 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
658 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000661
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200662 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200663 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000664
Paul Bakker42a29bf2009-07-07 20:18:41 +0000665
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100666 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
667 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
668 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
669 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000670
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100671 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
672 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100673 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000675
Paul Bakker69998dd2009-07-11 19:15:20 +0000676 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000677
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200678 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100679 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200680 &output_len, message_str->x, output,
681 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200682 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000683 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000684
Ronald Cronac6ae352020-06-26 14:33:03 +0200685 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200686 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200687 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000688 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000689
Paul Bakkerbd51b262014-07-10 15:26:12 +0200690exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100691 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
692 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000694}
Paul Bakker33b43f12013-08-20 11:48:36 +0200695/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000696
Paul Bakker33b43f12013-08-20 11:48:36 +0200697/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100698void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100699 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200700 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000701{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200702 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000704
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100705 mbedtls_mpi N, E;
706
707 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
709 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200710 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000711
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100712 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
713 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000714
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100715 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
716 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000718
Paul Bakker821fb082009-07-12 13:26:42 +0000719
Azim Khand30ca132017-06-09 04:32:58 +0100720 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200721 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000722 {
Paul Bakker821fb082009-07-12 13:26:42 +0000723
Ronald Cronac6ae352020-06-26 14:33:03 +0200724 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
725 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000726 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100727
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100728 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200730 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100734
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200735 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100736 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100737 if( result == 0 )
738 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100739
Ronald Cronac6ae352020-06-26 14:33:03 +0200740 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
741 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100742 }
743
Paul Bakkerbd51b262014-07-10 15:26:12 +0200744exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100745 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_rsa_free( &ctx );
747 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000748}
Paul Bakker33b43f12013-08-20 11:48:36 +0200749/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000750
Paul Bakker33b43f12013-08-20 11:48:36 +0200751/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100752void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100753 char * input_P, int radix_Q, char * input_Q,
754 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200755 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100756 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000757{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200758 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100760 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200761 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200762 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000763
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100764 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
765 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
767 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000768
Ronald Cron351f0ee2020-06-10 12:12:18 +0200769 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000770
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100771 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
772 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
773 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
774 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000775
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100776 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
777 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100778 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000780
Paul Bakker821fb082009-07-12 13:26:42 +0000781
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200782 /* repeat three times to test updating of blinding values */
783 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000784 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200785 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200786 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
787 &rnd_info, message_str->x,
788 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200789 if( result == 0 )
790 {
Paul Bakker821fb082009-07-12 13:26:42 +0000791
Ronald Cronac6ae352020-06-26 14:33:03 +0200792 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200793 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200794 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200795 }
Paul Bakker821fb082009-07-12 13:26:42 +0000796 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000797
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100798 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200800 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100804
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200805 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200806 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
807 &rnd_info, message_str->x,
808 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100809 if( result == 0 )
810 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100811
Ronald Cronac6ae352020-06-26 14:33:03 +0200812 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200813 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200814 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100815 }
816
Paul Bakkerbd51b262014-07-10 15:26:12 +0200817exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100818 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
819 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000822}
Paul Bakker33b43f12013-08-20 11:48:36 +0200823/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000824
Paul Bakker33b43f12013-08-20 11:48:36 +0200825/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100826void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000827{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 mbedtls_rsa_context ctx;
829 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000832}
Paul Bakker33b43f12013-08-20 11:48:36 +0200833/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000834
Paul Bakker33b43f12013-08-20 11:48:36 +0200835/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100836void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
837 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100840 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000841
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100842 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000844
Paul Bakker33b43f12013-08-20 11:48:36 +0200845 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000846 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100847 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000848 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200849 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000850 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100851 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000852 }
853
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100854 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100856
Paul Bakkerbd51b262014-07-10 15:26:12 +0200857exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100858 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000860}
Paul Bakker33b43f12013-08-20 11:48:36 +0200861/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000862
Paul Bakker33b43f12013-08-20 11:48:36 +0200863/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100864void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
865 int radix_Q, char * input_Q, int radix_N,
866 char * input_N, int radix_E, char * input_E,
867 int radix_D, char * input_D, int radix_DP,
868 char * input_DP, int radix_DQ,
869 char * input_DQ, int radix_QP,
870 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000871{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000875
Paul Bakker33b43f12013-08-20 11:48:36 +0200876 ctx.len = mod / 8;
877 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000880 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200881 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000884 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200885 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000888 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200889 if( strlen( input_E ) )
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.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000892 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200893 if( strlen( input_D ) )
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.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000896 }
Hanno Becker131134f2017-08-23 08:31:07 +0100897#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200898 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000901 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200902 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000905 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200906 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000909 }
Hanno Becker131134f2017-08-23 08:31:07 +0100910#else
911 ((void) radix_DP); ((void) input_DP);
912 ((void) radix_DQ); ((void) input_DQ);
913 ((void) radix_QP); ((void) input_QP);
914#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100917
Paul Bakkerbd51b262014-07-10 15:26:12 +0200918exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000920}
Paul Bakker33b43f12013-08-20 11:48:36 +0200921/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000922
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100923/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100924void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
925 int radix_Epub, char * input_Epub, int radix_P,
926 char * input_P, int radix_Q, char * input_Q,
927 int radix_N, char * input_N, int radix_E,
928 char * input_E, int radix_D, char * input_D,
929 int radix_DP, char * input_DP, int radix_DQ,
930 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100931 int result )
932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
936 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100937
938 pub.len = mod / 8;
939 prv.len = mod / 8;
940
941 if( strlen( input_Npub ) )
942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100944 }
945 if( strlen( input_Epub ) )
946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100948 }
949
950 if( strlen( input_P ) )
951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100953 }
954 if( strlen( input_Q ) )
955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100957 }
958 if( strlen( input_N ) )
959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100961 }
962 if( strlen( input_E ) )
963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100965 }
966 if( strlen( input_D ) )
967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100969 }
Hanno Becker131134f2017-08-23 08:31:07 +0100970#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100971 if( strlen( input_DP ) )
972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100974 }
975 if( strlen( input_DQ ) )
976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100978 }
979 if( strlen( input_QP ) )
980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100982 }
Hanno Becker131134f2017-08-23 08:31:07 +0100983#else
984 ((void) radix_DP); ((void) input_DP);
985 ((void) radix_DQ); ((void) input_DQ);
986 ((void) radix_QP); ((void) input_QP);
987#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100990
991exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 mbedtls_rsa_free( &pub );
993 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100994}
995/* END_CASE */
996
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100997/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000999{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_rsa_context ctx;
1001 mbedtls_entropy_context entropy;
1002 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +02001003 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +00001004
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +02001005 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +01001007 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +00001008
Hanno Beckera47023e2017-12-22 17:08:03 +00001009 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1010 &entropy, (const unsigned char *) pers,
1011 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +02001014 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +00001015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +01001017 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +00001018 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +01001019
Paul Bakkerbd51b262014-07-10 15:26:12 +02001020exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_rsa_free( &ctx );
1022 mbedtls_ctr_drbg_free( &ctr_drbg );
1023 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +00001024}
Paul Bakker33b43f12013-08-20 11:48:36 +02001025/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +00001026
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001027/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +01001028void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001029 int radix_D, char *input_D,
1030 int radix_E, char *input_E,
1031 int radix_P, char *output_P,
1032 int radix_Q, char *output_Q,
1033 int corrupt, int result )
1034{
1035 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
1036
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001037 mbedtls_mpi_init( &N );
1038 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1039 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
1040 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1041
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001042 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1043 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1044 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1045 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
1046 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
1047
1048 if( corrupt )
1049 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
1050
1051 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +01001052 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001053
1054 if( !corrupt )
1055 {
1056 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
1057 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
1058 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
1059 }
1060
1061exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001062 mbedtls_mpi_free( &N );
1063 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1064 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
1065 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +01001066}
1067/* END_CASE */
1068
Hanno Becker6b4ce492017-08-23 11:00:21 +01001069/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001070void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
1071 int radix_Q, char *input_Q,
1072 int radix_E, char *input_E,
1073 int radix_D, char *output_D,
1074 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +01001075{
1076 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
1077
1078 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1079 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
1080 mbedtls_mpi_init( &E );
1081 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
1082
1083 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1084 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1085 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1086 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
1087
1088 if( corrupt )
1089 {
1090 /* Make E even */
1091 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
1092 }
1093
1094 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +01001095 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
1096 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +01001097
1098 if( !corrupt )
1099 {
1100 /*
1101 * Check that D and Dp agree modulo LCM(P-1, Q-1).
1102 */
1103
1104 /* Replace P,Q by P-1, Q-1 */
1105 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
1106 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
1107
1108 /* Check D == Dp modulo P-1 */
1109 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
1110 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
1111 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1112
1113 /* Check D == Dp modulo Q-1 */
1114 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
1115 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
1116 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
1117 }
1118
1119exit:
1120
1121 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1122 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
1123 mbedtls_mpi_free( &E );
1124 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
1125}
1126/* END_CASE */
1127
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001128/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001129void mbedtls_rsa_import( int radix_N, char *input_N,
1130 int radix_P, char *input_P,
1131 int radix_Q, char *input_Q,
1132 int radix_D, char *input_D,
1133 int radix_E, char *input_E,
1134 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001135 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001136 int res_check,
1137 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001138{
1139 mbedtls_mpi N, P, Q, D, E;
1140 mbedtls_rsa_context ctx;
1141
Hanno Beckere1582a82017-09-29 11:51:05 +01001142 /* Buffers used for encryption-decryption test */
1143 unsigned char *buf_orig = NULL;
1144 unsigned char *buf_enc = NULL;
1145 unsigned char *buf_dec = NULL;
1146
Hanno Beckerc77ab892017-08-23 11:01:06 +01001147 mbedtls_entropy_context entropy;
1148 mbedtls_ctr_drbg_context ctr_drbg;
1149 const char *pers = "test_suite_rsa";
1150
Hanno Becker4d6e8342017-09-29 11:50:18 +01001151 const int have_N = ( strlen( input_N ) > 0 );
1152 const int have_P = ( strlen( input_P ) > 0 );
1153 const int have_Q = ( strlen( input_Q ) > 0 );
1154 const int have_D = ( strlen( input_D ) > 0 );
1155 const int have_E = ( strlen( input_E ) > 0 );
1156
Hanno Beckerc77ab892017-08-23 11:01:06 +01001157 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001158 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001159 mbedtls_rsa_init( &ctx, 0, 0 );
1160
1161 mbedtls_mpi_init( &N );
1162 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1163 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1164
Hanno Beckerd4d60572018-01-10 07:12:01 +00001165 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
1166 (const unsigned char *) pers, strlen( pers ) ) == 0 );
1167
Hanno Becker4d6e8342017-09-29 11:50:18 +01001168 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001169 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1170
Hanno Becker4d6e8342017-09-29 11:50:18 +01001171 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001172 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1173
Hanno Becker4d6e8342017-09-29 11:50:18 +01001174 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001175 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1176
Hanno Becker4d6e8342017-09-29 11:50:18 +01001177 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001178 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1179
Hanno Becker4d6e8342017-09-29 11:50:18 +01001180 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001181 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1182
1183 if( !successive )
1184 {
1185 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001186 have_N ? &N : NULL,
1187 have_P ? &P : NULL,
1188 have_Q ? &Q : NULL,
1189 have_D ? &D : NULL,
1190 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001191 }
1192 else
1193 {
1194 /* Import N, P, Q, D, E separately.
1195 * This should make no functional difference. */
1196
1197 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001198 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001199 NULL, NULL, NULL, NULL ) == 0 );
1200
1201 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1202 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001203 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001204 NULL, NULL, NULL ) == 0 );
1205
1206 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1207 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001208 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001209 NULL, NULL ) == 0 );
1210
1211 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1212 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001213 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001214 NULL ) == 0 );
1215
1216 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1217 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +01001218 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001219 }
1220
Hanno Becker04877a42017-10-11 10:01:33 +01001221 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001222
Hanno Beckere1582a82017-09-29 11:51:05 +01001223 /* On expected success, perform some public and private
1224 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001225 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001226 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001227 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001228 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1229 else
1230 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1231
1232 if( res_check != 0 )
1233 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001234
1235 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1236 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1237 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1238 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1239 goto exit;
1240
1241 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1242 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1243
1244 /* Make sure the number we're generating is smaller than the modulus */
1245 buf_orig[0] = 0x00;
1246
1247 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1248
1249 if( is_priv )
1250 {
1251 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1252 &ctr_drbg, buf_enc,
1253 buf_dec ) == 0 );
1254
1255 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1256 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1257 }
1258 }
1259
Hanno Beckerc77ab892017-08-23 11:01:06 +01001260exit:
1261
Hanno Beckere1582a82017-09-29 11:51:05 +01001262 mbedtls_free( buf_orig );
1263 mbedtls_free( buf_enc );
1264 mbedtls_free( buf_dec );
1265
Hanno Beckerc77ab892017-08-23 11:01:06 +01001266 mbedtls_rsa_free( &ctx );
1267
1268 mbedtls_ctr_drbg_free( &ctr_drbg );
1269 mbedtls_entropy_free( &entropy );
1270
1271 mbedtls_mpi_free( &N );
1272 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1273 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1274}
1275/* END_CASE */
1276
Hanno Becker417f2d62017-08-23 11:44:51 +01001277/* BEGIN_CASE */
1278void mbedtls_rsa_export( int radix_N, char *input_N,
1279 int radix_P, char *input_P,
1280 int radix_Q, char *input_Q,
1281 int radix_D, char *input_D,
1282 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +01001283 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +01001284 int successive )
1285{
1286 /* Original MPI's with which we set up the RSA context */
1287 mbedtls_mpi N, P, Q, D, E;
1288
1289 /* Exported MPI's */
1290 mbedtls_mpi Ne, Pe, Qe, De, Ee;
1291
1292 const int have_N = ( strlen( input_N ) > 0 );
1293 const int have_P = ( strlen( input_P ) > 0 );
1294 const int have_Q = ( strlen( input_Q ) > 0 );
1295 const int have_D = ( strlen( input_D ) > 0 );
1296 const int have_E = ( strlen( input_E ) > 0 );
1297
Hanno Becker417f2d62017-08-23 11:44:51 +01001298 mbedtls_rsa_context ctx;
1299
1300 mbedtls_rsa_init( &ctx, 0, 0 );
1301
1302 mbedtls_mpi_init( &N );
1303 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1304 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1305
1306 mbedtls_mpi_init( &Ne );
1307 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1308 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1309
1310 /* Setup RSA context */
1311
1312 if( have_N )
1313 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1314
1315 if( have_P )
1316 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1317
1318 if( have_Q )
1319 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1320
1321 if( have_D )
1322 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1323
1324 if( have_E )
1325 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1326
1327 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1328 strlen( input_N ) ? &N : NULL,
1329 strlen( input_P ) ? &P : NULL,
1330 strlen( input_Q ) ? &Q : NULL,
1331 strlen( input_D ) ? &D : NULL,
1332 strlen( input_E ) ? &E : NULL ) == 0 );
1333
Hanno Becker7f25f852017-10-10 16:56:22 +01001334 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001335
1336 /*
1337 * Export parameters and compare to original ones.
1338 */
1339
1340 /* N and E must always be present. */
1341 if( !successive )
1342 {
1343 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1344 }
1345 else
1346 {
1347 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1348 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1349 }
1350 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1351 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1352
1353 /* If we were providing enough information to setup a complete private context,
1354 * we expect to be able to export all core parameters. */
1355
1356 if( is_priv )
1357 {
1358 if( !successive )
1359 {
1360 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1361 &De, NULL ) == 0 );
1362 }
1363 else
1364 {
1365 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1366 NULL, NULL ) == 0 );
1367 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1368 NULL, NULL ) == 0 );
1369 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1370 &De, NULL ) == 0 );
1371 }
1372
1373 if( have_P )
1374 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1375
1376 if( have_Q )
1377 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1378
1379 if( have_D )
1380 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1381
1382 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001383 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1384 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001385 }
1386
1387exit:
1388
1389 mbedtls_rsa_free( &ctx );
1390
1391 mbedtls_mpi_free( &N );
1392 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1393 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1394
1395 mbedtls_mpi_free( &Ne );
1396 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1397 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1398}
1399/* END_CASE */
1400
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001401/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001402void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1403 int radix_P, char *input_P,
1404 int radix_Q, char *input_Q,
1405 int radix_D, char *input_D,
1406 int radix_E, char *input_E,
1407 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001408{
1409 /* Original MPI's with which we set up the RSA context */
1410 mbedtls_mpi N, P, Q, D, E;
1411
1412 const int have_N = ( strlen( input_N ) > 0 );
1413 const int have_P = ( strlen( input_P ) > 0 );
1414 const int have_Q = ( strlen( input_Q ) > 0 );
1415 const int have_D = ( strlen( input_D ) > 0 );
1416 const int have_E = ( strlen( input_E ) > 0 );
1417
1418 mbedtls_entropy_context entropy;
1419 mbedtls_ctr_drbg_context ctr_drbg;
1420 const char *pers = "test_suite_rsa";
1421
1422 mbedtls_mpi_init( &N );
1423 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1424 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1425
1426 mbedtls_ctr_drbg_init( &ctr_drbg );
1427 mbedtls_entropy_init( &entropy );
1428 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1429 &entropy, (const unsigned char *) pers,
1430 strlen( pers ) ) == 0 );
1431
1432 if( have_N )
1433 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1434
1435 if( have_P )
1436 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1437
1438 if( have_Q )
1439 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1440
1441 if( have_D )
1442 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1443
1444 if( have_E )
1445 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1446
Hanno Becker750e8b42017-08-25 07:54:27 +01001447 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1448 have_P ? &P : NULL,
1449 have_Q ? &Q : NULL,
1450 have_D ? &D : NULL,
1451 have_E ? &E : NULL,
1452 prng ? mbedtls_ctr_drbg_random : NULL,
1453 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001454exit:
1455
1456 mbedtls_ctr_drbg_free( &ctr_drbg );
1457 mbedtls_entropy_free( &entropy );
1458
1459 mbedtls_mpi_free( &N );
1460 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1461 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1462}
1463/* END_CASE */
1464
Hanno Beckerc77ab892017-08-23 11:01:06 +01001465/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001466void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1467 data_t *input_Q, data_t *input_D,
1468 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001469 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001470{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001471 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001472 unsigned char bufNe[256];
1473 unsigned char bufPe[128];
1474 unsigned char bufQe[128];
1475 unsigned char bufDe[256];
1476 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001477
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001478 mbedtls_rsa_context ctx;
1479
1480 mbedtls_rsa_init( &ctx, 0, 0 );
1481
1482 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001483 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001484 input_N->len ? input_N->x : NULL, input_N->len,
1485 input_P->len ? input_P->x : NULL, input_P->len,
1486 input_Q->len ? input_Q->x : NULL, input_Q->len,
1487 input_D->len ? input_D->x : NULL, input_D->len,
1488 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001489
Hanno Becker7f25f852017-10-10 16:56:22 +01001490 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001491
1492 /*
1493 * Export parameters and compare to original ones.
1494 */
1495
1496 /* N and E must always be present. */
1497 if( !successive )
1498 {
Azim Khand30ca132017-06-09 04:32:58 +01001499 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001500 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001501 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001502 }
1503 else
1504 {
Azim Khand30ca132017-06-09 04:32:58 +01001505 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001506 NULL, 0, NULL, 0, NULL, 0,
1507 NULL, 0 ) == 0 );
1508 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1509 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001510 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001511 }
Azim Khand30ca132017-06-09 04:32:58 +01001512 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1513 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001514
1515 /* If we were providing enough information to setup a complete private context,
1516 * we expect to be able to export all core parameters. */
1517
1518 if( is_priv )
1519 {
1520 if( !successive )
1521 {
1522 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001523 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1524 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1525 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001526 NULL, 0 ) == 0 );
1527 }
1528 else
1529 {
1530 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001531 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001532 NULL, 0, NULL, 0,
1533 NULL, 0 ) == 0 );
1534
1535 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001536 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001537 NULL, 0, NULL, 0 ) == 0 );
1538
Azim Khand30ca132017-06-09 04:32:58 +01001539 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1540 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001541 NULL, 0 ) == 0 );
1542 }
1543
Azim Khand30ca132017-06-09 04:32:58 +01001544 if( input_P->len )
1545 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001546
Azim Khand30ca132017-06-09 04:32:58 +01001547 if( input_Q->len )
1548 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001549
Azim Khand30ca132017-06-09 04:32:58 +01001550 if( input_D->len )
1551 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001552
1553 }
1554
1555exit:
1556 mbedtls_rsa_free( &ctx );
1557}
1558/* END_CASE */
1559
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001560/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001561void mbedtls_rsa_import_raw( data_t *input_N,
1562 data_t *input_P, data_t *input_Q,
1563 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001564 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001565 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001566 int res_check,
1567 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001568{
Hanno Beckere1582a82017-09-29 11:51:05 +01001569 /* Buffers used for encryption-decryption test */
1570 unsigned char *buf_orig = NULL;
1571 unsigned char *buf_enc = NULL;
1572 unsigned char *buf_dec = NULL;
1573
Hanno Beckerc77ab892017-08-23 11:01:06 +01001574 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001575 mbedtls_entropy_context entropy;
1576 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001577
Hanno Beckerc77ab892017-08-23 11:01:06 +01001578 const char *pers = "test_suite_rsa";
1579
1580 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001581 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001582 mbedtls_rsa_init( &ctx, 0, 0 );
1583
Hanno Beckerc77ab892017-08-23 11:01:06 +01001584 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1585 &entropy, (const unsigned char *) pers,
1586 strlen( pers ) ) == 0 );
1587
Hanno Beckerc77ab892017-08-23 11:01:06 +01001588 if( !successive )
1589 {
1590 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001591 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1592 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1593 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1594 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1595 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001596 }
1597 else
1598 {
1599 /* Import N, P, Q, D, E separately.
1600 * This should make no functional difference. */
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,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001604 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1605
1606 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1607 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001608 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001609 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1610
1611 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1612 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001613 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001614 NULL, 0, NULL, 0 ) == 0 );
1615
1616 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1617 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001618 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001619 NULL, 0 ) == 0 );
1620
1621 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1622 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001623 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001624 }
1625
Hanno Becker04877a42017-10-11 10:01:33 +01001626 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001627
Hanno Beckere1582a82017-09-29 11:51:05 +01001628 /* On expected success, perform some public and private
1629 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001630 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001631 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001632 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001633 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1634 else
1635 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1636
1637 if( res_check != 0 )
1638 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001639
1640 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1641 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1642 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1643 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1644 goto exit;
1645
1646 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1647 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1648
1649 /* Make sure the number we're generating is smaller than the modulus */
1650 buf_orig[0] = 0x00;
1651
1652 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1653
1654 if( is_priv )
1655 {
1656 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1657 &ctr_drbg, buf_enc,
1658 buf_dec ) == 0 );
1659
1660 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1661 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1662 }
1663 }
1664
Hanno Beckerc77ab892017-08-23 11:01:06 +01001665exit:
1666
Hanno Becker3f3ae852017-10-02 10:08:39 +01001667 mbedtls_free( buf_orig );
1668 mbedtls_free( buf_enc );
1669 mbedtls_free( buf_dec );
1670
Hanno Beckerc77ab892017-08-23 11:01:06 +01001671 mbedtls_rsa_free( &ctx );
1672
1673 mbedtls_ctr_drbg_free( &ctr_drbg );
1674 mbedtls_entropy_free( &entropy );
1675
1676}
1677/* END_CASE */
1678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001680void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001681{
Andres AG93012e82016-09-09 09:10:28 +01001682 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001683}
Paul Bakker33b43f12013-08-20 11:48:36 +02001684/* END_CASE */