blob: 770a04a435dcddd5109ffb24d20fb4efe5ff8ff0 [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/md5.h"
5#include "mbedtls/sha1.h"
6#include "mbedtls/sha256.h"
7#include "mbedtls/sha512.h"
8#include "mbedtls/entropy.h"
9#include "mbedtls/ctr_drbg.h"
Hanno Becker47deec42017-07-24 12:27:09 +010010
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +020011#include "or_psa_helpers.h"
12
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
Paul Bakker33b43f12013-08-20 11:48:36 +020020/* BEGIN_CASE */
Ronald Cronea7631b2021-06-03 18:51:59 +020021void rsa_invalid_param( )
22{
23 mbedtls_rsa_context ctx;
24 const int invalid_padding = 42;
25 const int invalid_hash_id = 0xff;
26
Ronald Cronc1905a12021-06-05 11:11:14 +020027 mbedtls_rsa_init( &ctx );
Ronald Cronea7631b2021-06-03 18:51:59 +020028
29 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
30 invalid_padding,
31 MBEDTLS_MD_NONE ),
32 MBEDTLS_ERR_RSA_INVALID_PADDING );
33
34 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
35 MBEDTLS_RSA_PKCS_V21,
36 invalid_hash_id ),
37 MBEDTLS_ERR_RSA_INVALID_PADDING );
38
Ronald Cron3a0375f2021-06-08 10:22:28 +020039#if !defined(MBEDTLS_PKCS1_V15)
40 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
41 MBEDTLS_RSA_PKCS_V15,
42 MBEDTLS_MD_NONE ),
43 MBEDTLS_ERR_RSA_INVALID_PADDING );
44#endif
45
46#if !defined(MBEDTLS_PKCS1_V21)
47 TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
48 MBEDTLS_RSA_PKCS_V21,
49 MBEDTLS_MD_NONE ),
50 MBEDTLS_ERR_RSA_INVALID_PADDING );
51#endif
52
Ronald Cronea7631b2021-06-03 18:51:59 +020053exit:
54 mbedtls_rsa_free( &ctx );
55}
56/* END_CASE */
57
58/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +010059void rsa_init_free( int reinit )
60{
61 mbedtls_rsa_context ctx;
62
63 /* Double free is not explicitly documented to work, but we rely on it
64 * even inside the library so that you can call mbedtls_rsa_free()
65 * unconditionally on an error path without checking whether it has
66 * already been called in the success path. */
67
Ronald Cronc1905a12021-06-05 11:11:14 +020068 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +010069 mbedtls_rsa_free( &ctx );
70
71 if( reinit )
Ronald Cronc1905a12021-06-05 11:11:14 +020072 mbedtls_rsa_init( &ctx );
Gilles Peskine914afe12021-02-01 17:55:24 +010073 mbedtls_rsa_free( &ctx );
74
75 /* This test case always succeeds, functionally speaking. A plausible
76 * bug might trigger an invalid pointer dereference or a memory leak. */
77 goto exit;
78}
79/* END_CASE */
80
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +020081/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010082void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +010083 int digest, int mod, int radix_P, char * input_P,
84 int radix_Q, char * input_Q, int radix_N,
85 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +020086 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000087{
Ron Eldorfdc15bd2018-11-22 15:47:51 +020088 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010090 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +020091 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +000092
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010093 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
94 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +020095 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +020096 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,padding_mode,
97 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000098
Ron Eldorfdc15bd2018-11-22 15:47:51 +020099 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200100 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000101
Gilles Peskine20edee72021-06-10 23:18:39 +0200102 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
103 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
104 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
105 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000106
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100107 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
108 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100109 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000111
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200112 TEST_ASSERT( mbedtls_rsa_pkcs1_sign(
113 &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnarda4aa12f2022-07-16 08:20:26 +0200114 digest, message_str->len, message_str->x,
Gilles Peskine6e3187b2021-06-22 18:39:53 +0200115 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200116 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000117 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000118
Ronald Cronac6ae352020-06-26 14:33:03 +0200119 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
120 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000121 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000122
Paul Bakkerbd51b262014-07-10 15:26:12 +0200123exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100124 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
125 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000127}
Paul Bakker33b43f12013-08-20 11:48:36 +0200128/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000129
Manuel Pégourié-Gonnard236c4e22022-07-16 08:35:06 +0200130/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100131void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100132 int digest, int mod, int radix_N,
133 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100134 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100137 mbedtls_mpi N, E;
138
139 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200140 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200141 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
142 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000143
Gilles Peskine20edee72021-06-10 23:18:39 +0200144 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
145 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100146 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
147 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000149
Manuel Pégourié-Gonnarda4aa12f2022-07-16 08:20:26 +0200150 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, digest, message_str->len, message_str->x, result_str->x ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100151
Paul Bakkerbd51b262014-07-10 15:26:12 +0200152exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100153 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000155}
Paul Bakker33b43f12013-08-20 11:48:36 +0200156/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000157
Paul Bakker821fb082009-07-12 13:26:42 +0000158
Paul Bakker33b43f12013-08-20 11:48:36 +0200159/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100160void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100161 int padding_mode, int mod, int radix_P,
162 char * input_P, int radix_Q, char * input_Q,
163 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200164 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000165{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200166 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100168 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200169 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000170
Ronald Cronc1905a12021-06-05 11:11:14 +0200171 mbedtls_rsa_init( &ctx );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100172 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
173 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000174
Paul Elliotte57dd2d2021-06-25 11:13:24 +0100175 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
176 MBEDTLS_MD_NONE ) == 0 );
177
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200178 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200179 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000180
Gilles Peskine20edee72021-06-10 23:18:39 +0200181 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
182 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
183 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
184 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000185
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100186 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
187 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100188 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000190
Paul Bakker821fb082009-07-12 13:26:42 +0000191
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200192 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100193 &rnd_info, MBEDTLS_MD_NONE,
194 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200195 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000196
Paul Bakker821fb082009-07-12 13:26:42 +0000197
Ronald Cronac6ae352020-06-26 14:33:03 +0200198 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
199 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000200
Paul Bakkerbd51b262014-07-10 15:26:12 +0200201exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100202 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
203 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000206}
Paul Bakker33b43f12013-08-20 11:48:36 +0200207/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000208
Paul Bakker33b43f12013-08-20 11:48:36 +0200209/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100210void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200211 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100212 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100213 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000214{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200215 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000217
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100218 mbedtls_mpi N, E;
219 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
220
Ronald Cronc1905a12021-06-05 11:11:14 +0200221 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200222 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
223 MBEDTLS_MD_NONE ) == 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100224 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000225
Gilles Peskine20edee72021-06-10 23:18:39 +0200226 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
227 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000228
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100229 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
230 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000232
Paul Bakker821fb082009-07-12 13:26:42 +0000233
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100234 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 +0100235
Paul Bakkerbd51b262014-07-10 15:26:12 +0200236exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100237 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000239}
Paul Bakker33b43f12013-08-20 11:48:36 +0200240/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000241
Paul Bakker33b43f12013-08-20 11:48:36 +0200242/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100243void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100244 int mod, int radix_N, char * input_N,
245 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200246 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000247{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200248 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200250 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000251
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100252 mbedtls_mpi N, E;
253 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
254
Ronald Cron351f0ee2020-06-10 12:12:18 +0200255 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000256
Ronald Cronc1905a12021-06-05 11:11:14 +0200257 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200258 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
259 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200260 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000261
Gilles Peskine20edee72021-06-10 23:18:39 +0200262 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
263 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000264
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100265 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
266 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000268
Paul Bakker42a29bf2009-07-07 20:18:41 +0000269
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200270 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
271 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100272 &rnd_info, message_str->len,
273 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200274 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200275 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000276 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000277
Ronald Cronac6ae352020-06-26 14:33:03 +0200278 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
279 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000280 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100281
Paul Bakkerbd51b262014-07-10 15:26:12 +0200282exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100283 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000285}
Paul Bakker33b43f12013-08-20 11:48:36 +0200286/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000287
Paul Bakker33b43f12013-08-20 11:48:36 +0200288/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100289void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100290 int mod, int radix_N, char * input_N,
291 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200292 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000293{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200294 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000296
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100297 mbedtls_mpi N, E;
298
299 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200300 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200301 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
302 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200303 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000304
Gilles Peskine20edee72021-06-10 23:18:39 +0200305 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
306 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000307
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100308 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
309 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000311
Paul Bakkera6656852010-07-18 19:47:14 +0000312
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200313 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100314 NULL, message_str->len,
315 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200316 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200317 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000318 {
Paul Bakkera6656852010-07-18 19:47:14 +0000319
Ronald Cronac6ae352020-06-26 14:33:03 +0200320 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
321 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000322 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100323
Paul Bakkerbd51b262014-07-10 15:26:12 +0200324exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100325 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000327}
Paul Bakker33b43f12013-08-20 11:48:36 +0200328/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000329
Paul Bakker33b43f12013-08-20 11:48:36 +0200330/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100331void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100332 int mod, int radix_P, char * input_P,
333 int radix_Q, char * input_Q, int radix_N,
334 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200335 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100336 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000337{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200338 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000340 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200341 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100342 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000343
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100344 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
345 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
346
Ronald Cronc1905a12021-06-05 11:11:14 +0200347 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200348 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
349 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000350
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200351 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200352 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000353
Paul Bakker42a29bf2009-07-07 20:18:41 +0000354
Gilles Peskine20edee72021-06-10 23:18:39 +0200355 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
356 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
357 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
358 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000359
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100360 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
361 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100362 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000364
Paul Bakker69998dd2009-07-11 19:15:20 +0000365 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000366
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200367 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100368 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200369 &output_len, message_str->x, output,
370 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200371 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000372 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000373
Ronald Cronac6ae352020-06-26 14:33:03 +0200374 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200375 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200376 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000377 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000378
Paul Bakkerbd51b262014-07-10 15:26:12 +0200379exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100380 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
381 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000383}
Paul Bakker33b43f12013-08-20 11:48:36 +0200384/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000385
Paul Bakker33b43f12013-08-20 11:48:36 +0200386/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100387void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100388 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200389 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000390{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200391 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000393
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100394 mbedtls_mpi N, E;
395
396 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200397 mbedtls_rsa_init( &ctx );
398 mbedtls_rsa_init( &ctx2 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200399 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000400
Gilles Peskine20edee72021-06-10 23:18:39 +0200401 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
402 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000403
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100404 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Gilles Peskine058d0092021-06-09 16:24:35 +0200405
406 /* Check test data consistency */
407 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100408 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000410
Azim Khand30ca132017-06-09 04:32:58 +0100411 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200412 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000413 {
Paul Bakker821fb082009-07-12 13:26:42 +0000414
Ronald Cronac6ae352020-06-26 14:33:03 +0200415 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
416 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000417 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100418
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100419 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200421 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100425
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200426 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100427 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100428 if( result == 0 )
429 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100430
Ronald Cronac6ae352020-06-26 14:33:03 +0200431 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
432 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100433 }
434
Paul Bakkerbd51b262014-07-10 15:26:12 +0200435exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100436 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_rsa_free( &ctx );
438 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000439}
Paul Bakker33b43f12013-08-20 11:48:36 +0200440/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000441
Paul Bakker33b43f12013-08-20 11:48:36 +0200442/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100443void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100444 char * input_P, int radix_Q, char * input_Q,
445 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200446 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100447 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000448{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200449 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100451 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200452 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200453 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000454
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100455 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
456 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200457 mbedtls_rsa_init( &ctx );
458 mbedtls_rsa_init( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000459
Ronald Cron351f0ee2020-06-10 12:12:18 +0200460 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000461
Gilles Peskine20edee72021-06-10 23:18:39 +0200462 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
463 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
464 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
465 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000466
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100467 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
Gilles Peskine058d0092021-06-09 16:24:35 +0200468
469 /* Check test data consistency */
470 TEST_ASSERT( message_str->len == (size_t) ( mod / 8 ) );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100471 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100472 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000474
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200475 /* repeat three times to test updating of blinding values */
476 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000477 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200478 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200479 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
480 &rnd_info, message_str->x,
481 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200482 if( result == 0 )
483 {
Paul Bakker821fb082009-07-12 13:26:42 +0000484
Ronald Cronac6ae352020-06-26 14:33:03 +0200485 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200486 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200487 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200488 }
Paul Bakker821fb082009-07-12 13:26:42 +0000489 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000490
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100491 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200493 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100497
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200498 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200499 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
500 &rnd_info, message_str->x,
501 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100502 if( result == 0 )
503 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100504
Ronald Cronac6ae352020-06-26 14:33:03 +0200505 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200506 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200507 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100508 }
509
Paul Bakkerbd51b262014-07-10 15:26:12 +0200510exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100511 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
512 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000515}
Paul Bakker33b43f12013-08-20 11:48:36 +0200516/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000517
Paul Bakker33b43f12013-08-20 11:48:36 +0200518/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100519void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000520{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_rsa_context ctx;
522 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000525}
Paul Bakker33b43f12013-08-20 11:48:36 +0200526/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000527
Paul Bakker33b43f12013-08-20 11:48:36 +0200528/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100529void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
530 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100533 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000534
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100535 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200536 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000537
Paul Bakker33b43f12013-08-20 11:48:36 +0200538 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000539 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200540 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000541 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200542 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000543 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200544 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000545 }
546
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100547 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100549
Paul Bakkerbd51b262014-07-10 15:26:12 +0200550exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100551 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000553}
Paul Bakker33b43f12013-08-20 11:48:36 +0200554/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000555
Paul Bakker33b43f12013-08-20 11:48:36 +0200556/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100557void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
558 int radix_Q, char * input_Q, int radix_N,
559 char * input_N, int radix_E, char * input_E,
560 int radix_D, char * input_D, int radix_DP,
561 char * input_DP, int radix_DQ,
562 char * input_DQ, int radix_QP,
563 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000564{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000566
Ronald Cronc1905a12021-06-05 11:11:14 +0200567 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000568
Paul Bakker33b43f12013-08-20 11:48:36 +0200569 ctx.len = mod / 8;
570 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000571 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200572 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000573 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200574 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000575 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200576 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000577 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200578 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000579 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200580 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000581 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200582 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000583 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200584 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000585 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200586 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000587 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200588 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000589 }
Hanno Becker131134f2017-08-23 08:31:07 +0100590#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200591 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000592 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200593 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000594 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200595 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000596 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200597 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000598 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200599 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000600 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200601 TEST_ASSERT( mbedtls_test_read_mpi( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000602 }
Hanno Becker131134f2017-08-23 08:31:07 +0100603#else
604 ((void) radix_DP); ((void) input_DP);
605 ((void) radix_DQ); ((void) input_DQ);
606 ((void) radix_QP); ((void) input_QP);
607#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100610
Paul Bakkerbd51b262014-07-10 15:26:12 +0200611exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000613}
Paul Bakker33b43f12013-08-20 11:48:36 +0200614/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000615
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100616/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100617void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
618 int radix_Epub, char * input_Epub, int radix_P,
619 char * input_P, int radix_Q, char * input_Q,
620 int radix_N, char * input_N, int radix_E,
621 char * input_E, int radix_D, char * input_D,
622 int radix_DP, char * input_DP, int radix_DQ,
623 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100624 int result )
625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100627
Ronald Cronc1905a12021-06-05 11:11:14 +0200628 mbedtls_rsa_init( &pub );
629 mbedtls_rsa_init( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100630
631 pub.len = mod / 8;
632 prv.len = mod / 8;
633
634 if( strlen( input_Npub ) )
635 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200636 TEST_ASSERT( mbedtls_test_read_mpi( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100637 }
638 if( strlen( input_Epub ) )
639 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200640 TEST_ASSERT( mbedtls_test_read_mpi( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100641 }
642
643 if( strlen( input_P ) )
644 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200645 TEST_ASSERT( mbedtls_test_read_mpi( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100646 }
647 if( strlen( input_Q ) )
648 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200649 TEST_ASSERT( mbedtls_test_read_mpi( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100650 }
651 if( strlen( input_N ) )
652 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200653 TEST_ASSERT( mbedtls_test_read_mpi( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100654 }
655 if( strlen( input_E ) )
656 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200657 TEST_ASSERT( mbedtls_test_read_mpi( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100658 }
659 if( strlen( input_D ) )
660 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200661 TEST_ASSERT( mbedtls_test_read_mpi( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100662 }
Hanno Becker131134f2017-08-23 08:31:07 +0100663#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100664 if( strlen( input_DP ) )
665 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200666 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100667 }
668 if( strlen( input_DQ ) )
669 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200670 TEST_ASSERT( mbedtls_test_read_mpi( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100671 }
672 if( strlen( input_QP ) )
673 {
Gilles Peskine20edee72021-06-10 23:18:39 +0200674 TEST_ASSERT( mbedtls_test_read_mpi( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100675 }
Hanno Becker131134f2017-08-23 08:31:07 +0100676#else
677 ((void) radix_DP); ((void) input_DP);
678 ((void) radix_DQ); ((void) input_DQ);
679 ((void) radix_QP); ((void) input_QP);
680#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100683
684exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_rsa_free( &pub );
686 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100687}
688/* END_CASE */
689
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100690/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000692{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_rsa_context ctx;
694 mbedtls_entropy_context entropy;
695 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200696 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000697
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200698 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_entropy_init( &entropy );
Ronald Cronc1905a12021-06-05 11:11:14 +0200700 mbedtls_rsa_init ( &ctx );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000701
Hanno Beckera47023e2017-12-22 17:08:03 +0000702 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
703 &entropy, (const unsigned char *) pers,
704 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200707 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100710 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000711 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100712
Paul Bakkerbd51b262014-07-10 15:26:12 +0200713exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_rsa_free( &ctx );
715 mbedtls_ctr_drbg_free( &ctr_drbg );
716 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000717}
Paul Bakker33b43f12013-08-20 11:48:36 +0200718/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000719
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100720/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +0100721void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100722 int radix_D, char *input_D,
723 int radix_E, char *input_E,
724 int radix_P, char *output_P,
725 int radix_Q, char *output_Q,
726 int corrupt, int result )
727{
728 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
729
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100730 mbedtls_mpi_init( &N );
731 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
732 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
733 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
734
Gilles Peskine20edee72021-06-10 23:18:39 +0200735 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
736 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 );
737 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
738 TEST_ASSERT( mbedtls_test_read_mpi( &Qp, radix_P, output_P ) == 0 );
739 TEST_ASSERT( mbedtls_test_read_mpi( &Pp, radix_Q, output_Q ) == 0 );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100740
741 if( corrupt )
742 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
743
744 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100745 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100746
747 if( !corrupt )
748 {
749 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
750 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
751 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
752 }
753
754exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100755 mbedtls_mpi_free( &N );
756 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
757 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
758 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100759}
760/* END_CASE */
761
Hanno Becker6b4ce492017-08-23 11:00:21 +0100762/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100763void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
764 int radix_Q, char *input_Q,
765 int radix_E, char *input_E,
766 int radix_D, char *output_D,
767 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +0100768{
769 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
770
771 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
772 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
773 mbedtls_mpi_init( &E );
774 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
775
Gilles Peskine20edee72021-06-10 23:18:39 +0200776 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
777 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
778 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
779 TEST_ASSERT( mbedtls_test_read_mpi( &Dp, radix_D, output_D ) == 0 );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100780
781 if( corrupt )
782 {
783 /* Make E even */
784 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
785 }
786
787 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100788 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
789 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100790
791 if( !corrupt )
792 {
793 /*
794 * Check that D and Dp agree modulo LCM(P-1, Q-1).
795 */
796
797 /* Replace P,Q by P-1, Q-1 */
798 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
799 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
800
801 /* Check D == Dp modulo P-1 */
802 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
803 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
804 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
805
806 /* Check D == Dp modulo Q-1 */
807 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
808 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
809 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
810 }
811
812exit:
813
814 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
815 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
816 mbedtls_mpi_free( &E );
817 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
818}
819/* END_CASE */
820
Hanno Beckerf40cdf92017-12-22 11:03:27 +0000821/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +0100822void mbedtls_rsa_import( int radix_N, char *input_N,
823 int radix_P, char *input_P,
824 int radix_Q, char *input_Q,
825 int radix_D, char *input_D,
826 int radix_E, char *input_E,
827 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +0100828 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +0100829 int res_check,
830 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100831{
832 mbedtls_mpi N, P, Q, D, E;
833 mbedtls_rsa_context ctx;
834
Hanno Beckere1582a82017-09-29 11:51:05 +0100835 /* Buffers used for encryption-decryption test */
836 unsigned char *buf_orig = NULL;
837 unsigned char *buf_enc = NULL;
838 unsigned char *buf_dec = NULL;
839
Hanno Beckerc77ab892017-08-23 11:01:06 +0100840 mbedtls_entropy_context entropy;
841 mbedtls_ctr_drbg_context ctr_drbg;
842 const char *pers = "test_suite_rsa";
843
Hanno Becker4d6e8342017-09-29 11:50:18 +0100844 const int have_N = ( strlen( input_N ) > 0 );
845 const int have_P = ( strlen( input_P ) > 0 );
846 const int have_Q = ( strlen( input_Q ) > 0 );
847 const int have_D = ( strlen( input_D ) > 0 );
848 const int have_E = ( strlen( input_E ) > 0 );
849
Hanno Beckerc77ab892017-08-23 11:01:06 +0100850 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100851 mbedtls_entropy_init( &entropy );
Ronald Cronc1905a12021-06-05 11:11:14 +0200852 mbedtls_rsa_init( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100853
854 mbedtls_mpi_init( &N );
855 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
856 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
857
Hanno Beckerd4d60572018-01-10 07:12:01 +0000858 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
859 (const unsigned char *) pers, strlen( pers ) ) == 0 );
860
Hanno Becker4d6e8342017-09-29 11:50:18 +0100861 if( have_N )
Gilles Peskine20edee72021-06-10 23:18:39 +0200862 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100863
Hanno Becker4d6e8342017-09-29 11:50:18 +0100864 if( have_P )
Gilles Peskine20edee72021-06-10 23:18:39 +0200865 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100866
Hanno Becker4d6e8342017-09-29 11:50:18 +0100867 if( have_Q )
Gilles Peskine20edee72021-06-10 23:18:39 +0200868 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100869
Hanno Becker4d6e8342017-09-29 11:50:18 +0100870 if( have_D )
Gilles Peskine20edee72021-06-10 23:18:39 +0200871 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100872
Hanno Becker4d6e8342017-09-29 11:50:18 +0100873 if( have_E )
Gilles Peskine20edee72021-06-10 23:18:39 +0200874 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100875
876 if( !successive )
877 {
878 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100879 have_N ? &N : NULL,
880 have_P ? &P : NULL,
881 have_Q ? &Q : NULL,
882 have_D ? &D : NULL,
883 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100884 }
885 else
886 {
887 /* Import N, P, Q, D, E separately.
888 * This should make no functional difference. */
889
890 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100891 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100892 NULL, NULL, NULL, NULL ) == 0 );
893
894 TEST_ASSERT( mbedtls_rsa_import( &ctx,
895 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100896 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100897 NULL, NULL, NULL ) == 0 );
898
899 TEST_ASSERT( mbedtls_rsa_import( &ctx,
900 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100901 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100902 NULL, NULL ) == 0 );
903
904 TEST_ASSERT( mbedtls_rsa_import( &ctx,
905 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100906 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100907 NULL ) == 0 );
908
909 TEST_ASSERT( mbedtls_rsa_import( &ctx,
910 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100911 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100912 }
913
Hanno Becker04877a42017-10-11 10:01:33 +0100914 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100915
Hanno Beckere1582a82017-09-29 11:51:05 +0100916 /* On expected success, perform some public and private
917 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +0100918 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +0100919 {
Hanno Beckere1582a82017-09-29 11:51:05 +0100920 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +0100921 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
922 else
923 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
924
925 if( res_check != 0 )
926 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +0100927
928 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
929 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
930 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
931 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
932 goto exit;
933
934 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
935 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
936
937 /* Make sure the number we're generating is smaller than the modulus */
938 buf_orig[0] = 0x00;
939
940 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
941
942 if( is_priv )
943 {
944 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
945 &ctr_drbg, buf_enc,
946 buf_dec ) == 0 );
947
948 TEST_ASSERT( memcmp( buf_orig, buf_dec,
949 mbedtls_rsa_get_len( &ctx ) ) == 0 );
950 }
951 }
952
Hanno Beckerc77ab892017-08-23 11:01:06 +0100953exit:
954
Hanno Beckere1582a82017-09-29 11:51:05 +0100955 mbedtls_free( buf_orig );
956 mbedtls_free( buf_enc );
957 mbedtls_free( buf_dec );
958
Hanno Beckerc77ab892017-08-23 11:01:06 +0100959 mbedtls_rsa_free( &ctx );
960
961 mbedtls_ctr_drbg_free( &ctr_drbg );
962 mbedtls_entropy_free( &entropy );
963
964 mbedtls_mpi_free( &N );
965 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
966 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
967}
968/* END_CASE */
969
Hanno Becker417f2d62017-08-23 11:44:51 +0100970/* BEGIN_CASE */
971void mbedtls_rsa_export( int radix_N, char *input_N,
972 int radix_P, char *input_P,
973 int radix_Q, char *input_Q,
974 int radix_D, char *input_D,
975 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +0100976 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +0100977 int successive )
978{
979 /* Original MPI's with which we set up the RSA context */
980 mbedtls_mpi N, P, Q, D, E;
981
982 /* Exported MPI's */
983 mbedtls_mpi Ne, Pe, Qe, De, Ee;
984
985 const int have_N = ( strlen( input_N ) > 0 );
986 const int have_P = ( strlen( input_P ) > 0 );
987 const int have_Q = ( strlen( input_Q ) > 0 );
988 const int have_D = ( strlen( input_D ) > 0 );
989 const int have_E = ( strlen( input_E ) > 0 );
990
Hanno Becker417f2d62017-08-23 11:44:51 +0100991 mbedtls_rsa_context ctx;
992
Ronald Cronc1905a12021-06-05 11:11:14 +0200993 mbedtls_rsa_init( &ctx );
Hanno Becker417f2d62017-08-23 11:44:51 +0100994
995 mbedtls_mpi_init( &N );
996 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
997 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
998
999 mbedtls_mpi_init( &Ne );
1000 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1001 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1002
1003 /* Setup RSA context */
1004
1005 if( have_N )
Gilles Peskine20edee72021-06-10 23:18:39 +02001006 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001007
1008 if( have_P )
Gilles Peskine20edee72021-06-10 23:18:39 +02001009 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001010
1011 if( have_Q )
Gilles Peskine20edee72021-06-10 23:18:39 +02001012 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001013
1014 if( have_D )
Gilles Peskine20edee72021-06-10 23:18:39 +02001015 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001016
1017 if( have_E )
Gilles Peskine20edee72021-06-10 23:18:39 +02001018 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001019
1020 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1021 strlen( input_N ) ? &N : NULL,
1022 strlen( input_P ) ? &P : NULL,
1023 strlen( input_Q ) ? &Q : NULL,
1024 strlen( input_D ) ? &D : NULL,
1025 strlen( input_E ) ? &E : NULL ) == 0 );
1026
Hanno Becker7f25f852017-10-10 16:56:22 +01001027 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001028
1029 /*
1030 * Export parameters and compare to original ones.
1031 */
1032
1033 /* N and E must always be present. */
1034 if( !successive )
1035 {
1036 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1037 }
1038 else
1039 {
1040 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1041 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1042 }
1043 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1044 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1045
1046 /* If we were providing enough information to setup a complete private context,
1047 * we expect to be able to export all core parameters. */
1048
1049 if( is_priv )
1050 {
1051 if( !successive )
1052 {
1053 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1054 &De, NULL ) == 0 );
1055 }
1056 else
1057 {
1058 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1059 NULL, NULL ) == 0 );
1060 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1061 NULL, NULL ) == 0 );
1062 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1063 &De, NULL ) == 0 );
1064 }
1065
1066 if( have_P )
1067 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1068
1069 if( have_Q )
1070 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1071
1072 if( have_D )
1073 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1074
1075 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001076 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1077 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001078 }
1079
1080exit:
1081
1082 mbedtls_rsa_free( &ctx );
1083
1084 mbedtls_mpi_free( &N );
1085 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1086 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1087
1088 mbedtls_mpi_free( &Ne );
1089 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1090 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1091}
1092/* END_CASE */
1093
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001094/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001095void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1096 int radix_P, char *input_P,
1097 int radix_Q, char *input_Q,
1098 int radix_D, char *input_D,
1099 int radix_E, char *input_E,
1100 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001101{
1102 /* Original MPI's with which we set up the RSA context */
1103 mbedtls_mpi N, P, Q, D, E;
1104
1105 const int have_N = ( strlen( input_N ) > 0 );
1106 const int have_P = ( strlen( input_P ) > 0 );
1107 const int have_Q = ( strlen( input_Q ) > 0 );
1108 const int have_D = ( strlen( input_D ) > 0 );
1109 const int have_E = ( strlen( input_E ) > 0 );
1110
1111 mbedtls_entropy_context entropy;
1112 mbedtls_ctr_drbg_context ctr_drbg;
1113 const char *pers = "test_suite_rsa";
1114
1115 mbedtls_mpi_init( &N );
1116 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1117 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1118
1119 mbedtls_ctr_drbg_init( &ctr_drbg );
1120 mbedtls_entropy_init( &entropy );
1121 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1122 &entropy, (const unsigned char *) pers,
1123 strlen( pers ) ) == 0 );
1124
1125 if( have_N )
Gilles Peskine20edee72021-06-10 23:18:39 +02001126 TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001127
1128 if( have_P )
Gilles Peskine20edee72021-06-10 23:18:39 +02001129 TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001130
1131 if( have_Q )
Gilles Peskine20edee72021-06-10 23:18:39 +02001132 TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001133
1134 if( have_D )
Gilles Peskine20edee72021-06-10 23:18:39 +02001135 TEST_ASSERT( mbedtls_test_read_mpi( &D, radix_D, input_D ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001136
1137 if( have_E )
Gilles Peskine20edee72021-06-10 23:18:39 +02001138 TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
Hanno Beckerce002632017-08-23 13:22:36 +01001139
Hanno Becker750e8b42017-08-25 07:54:27 +01001140 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1141 have_P ? &P : NULL,
1142 have_Q ? &Q : NULL,
1143 have_D ? &D : NULL,
1144 have_E ? &E : NULL,
1145 prng ? mbedtls_ctr_drbg_random : NULL,
1146 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001147exit:
1148
1149 mbedtls_ctr_drbg_free( &ctr_drbg );
1150 mbedtls_entropy_free( &entropy );
1151
1152 mbedtls_mpi_free( &N );
1153 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1154 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1155}
1156/* END_CASE */
1157
Hanno Beckerc77ab892017-08-23 11:01:06 +01001158/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001159void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1160 data_t *input_Q, data_t *input_D,
1161 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001162 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001163{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001164 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001165 unsigned char bufNe[256];
1166 unsigned char bufPe[128];
1167 unsigned char bufQe[128];
1168 unsigned char bufDe[256];
1169 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001170
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001171 mbedtls_rsa_context ctx;
1172
Ronald Cronc1905a12021-06-05 11:11:14 +02001173 mbedtls_rsa_init( &ctx );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001174
1175 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001176 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001177 input_N->len ? input_N->x : NULL, input_N->len,
1178 input_P->len ? input_P->x : NULL, input_P->len,
1179 input_Q->len ? input_Q->x : NULL, input_Q->len,
1180 input_D->len ? input_D->x : NULL, input_D->len,
1181 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001182
Hanno Becker7f25f852017-10-10 16:56:22 +01001183 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001184
1185 /*
1186 * Export parameters and compare to original ones.
1187 */
1188
1189 /* N and E must always be present. */
1190 if( !successive )
1191 {
Azim Khand30ca132017-06-09 04:32:58 +01001192 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001193 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001194 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001195 }
1196 else
1197 {
Azim Khand30ca132017-06-09 04:32:58 +01001198 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001199 NULL, 0, NULL, 0, NULL, 0,
1200 NULL, 0 ) == 0 );
1201 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1202 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001203 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001204 }
Azim Khand30ca132017-06-09 04:32:58 +01001205 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1206 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001207
1208 /* If we were providing enough information to setup a complete private context,
1209 * we expect to be able to export all core parameters. */
1210
1211 if( is_priv )
1212 {
1213 if( !successive )
1214 {
1215 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001216 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1217 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1218 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001219 NULL, 0 ) == 0 );
1220 }
1221 else
1222 {
1223 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001224 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001225 NULL, 0, NULL, 0,
1226 NULL, 0 ) == 0 );
1227
1228 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001229 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001230 NULL, 0, NULL, 0 ) == 0 );
1231
Azim Khand30ca132017-06-09 04:32:58 +01001232 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1233 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001234 NULL, 0 ) == 0 );
1235 }
1236
Azim Khand30ca132017-06-09 04:32:58 +01001237 if( input_P->len )
1238 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001239
Azim Khand30ca132017-06-09 04:32:58 +01001240 if( input_Q->len )
1241 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001242
Azim Khand30ca132017-06-09 04:32:58 +01001243 if( input_D->len )
1244 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001245
1246 }
1247
1248exit:
1249 mbedtls_rsa_free( &ctx );
1250}
1251/* END_CASE */
1252
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001253/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001254void mbedtls_rsa_import_raw( data_t *input_N,
1255 data_t *input_P, data_t *input_Q,
1256 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001257 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001258 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001259 int res_check,
1260 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001261{
Hanno Beckere1582a82017-09-29 11:51:05 +01001262 /* Buffers used for encryption-decryption test */
1263 unsigned char *buf_orig = NULL;
1264 unsigned char *buf_enc = NULL;
1265 unsigned char *buf_dec = NULL;
1266
Hanno Beckerc77ab892017-08-23 11:01:06 +01001267 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001268 mbedtls_entropy_context entropy;
1269 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001270
Hanno Beckerc77ab892017-08-23 11:01:06 +01001271 const char *pers = "test_suite_rsa";
1272
1273 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001274 mbedtls_entropy_init( &entropy );
Ronald Cronc1905a12021-06-05 11:11:14 +02001275 mbedtls_rsa_init( &ctx );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001276
Hanno Beckerc77ab892017-08-23 11:01:06 +01001277 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1278 &entropy, (const unsigned char *) pers,
1279 strlen( pers ) ) == 0 );
1280
Hanno Beckerc77ab892017-08-23 11:01:06 +01001281 if( !successive )
1282 {
1283 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001284 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1285 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1286 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1287 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1288 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001289 }
1290 else
1291 {
1292 /* Import N, P, Q, D, E separately.
1293 * This should make no functional difference. */
1294
1295 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001296 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001297 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1298
1299 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1300 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001301 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001302 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1303
1304 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1305 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001306 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001307 NULL, 0, NULL, 0 ) == 0 );
1308
1309 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1310 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001311 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001312 NULL, 0 ) == 0 );
1313
1314 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1315 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001316 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001317 }
1318
Hanno Becker04877a42017-10-11 10:01:33 +01001319 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001320
Hanno Beckere1582a82017-09-29 11:51:05 +01001321 /* On expected success, perform some public and private
1322 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001323 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001324 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001325 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001326 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1327 else
1328 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1329
1330 if( res_check != 0 )
1331 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001332
1333 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1334 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1335 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1336 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1337 goto exit;
1338
1339 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1340 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1341
1342 /* Make sure the number we're generating is smaller than the modulus */
1343 buf_orig[0] = 0x00;
1344
1345 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1346
1347 if( is_priv )
1348 {
1349 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1350 &ctr_drbg, buf_enc,
1351 buf_dec ) == 0 );
1352
1353 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1354 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1355 }
1356 }
1357
Hanno Beckerc77ab892017-08-23 11:01:06 +01001358exit:
1359
Hanno Becker3f3ae852017-10-02 10:08:39 +01001360 mbedtls_free( buf_orig );
1361 mbedtls_free( buf_enc );
1362 mbedtls_free( buf_dec );
1363
Hanno Beckerc77ab892017-08-23 11:01:06 +01001364 mbedtls_rsa_free( &ctx );
1365
1366 mbedtls_ctr_drbg_free( &ctr_drbg );
1367 mbedtls_entropy_free( &entropy );
1368
1369}
1370/* END_CASE */
1371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001373void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001374{
Andres AG93012e82016-09-09 09:10:28 +01001375 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001376}
Paul Bakker33b43f12013-08-20 11:48:36 +02001377/* END_CASE */