blob: bf2398313ce5683e85e6a182e7ba054a35c82f45 [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
Paul Bakker33b43f12013-08-20 11:48:36 +020020/* BEGIN_CASE */
Gilles Peskine914afe12021-02-01 17:55:24 +010021void rsa_init_free( int reinit )
22{
23 mbedtls_rsa_context ctx;
24
25 /* Double free is not explicitly documented to work, but we rely on it
26 * even inside the library so that you can call mbedtls_rsa_free()
27 * unconditionally on an error path without checking whether it has
28 * already been called in the success path. */
29
30 mbedtls_rsa_init( &ctx, 0, 0 );
31 mbedtls_rsa_free( &ctx );
32
33 if( reinit )
34 mbedtls_rsa_init( &ctx, 0, 0 );
35 mbedtls_rsa_free( &ctx );
36
37 /* This test case always succeeds, functionally speaking. A plausible
38 * bug might trigger an invalid pointer dereference or a memory leak. */
39 goto exit;
40}
41/* END_CASE */
42
43/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010044void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +010045 int digest, int mod, int radix_P, char * input_P,
46 int radix_Q, char * input_Q, int radix_N,
47 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +020048 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000049{
Ron Eldorfdc15bd2018-11-22 15:47:51 +020050 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
51 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010053 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +020054 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +000055
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010056 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
57 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000059
Ron Eldorfdc15bd2018-11-22 15:47:51 +020060 memset( hash_result, 0x00, sizeof( hash_result ) );
61 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +020062 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +000063
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010064 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
65 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
66 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
67 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000068
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010069 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
70 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +010071 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000073
Paul Bakker42a29bf2009-07-07 20:18:41 +000074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +010076 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 +000077
Ronald Cron6c5bd7f2020-06-10 14:08:26 +020078 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
79 &rnd_info, MBEDTLS_RSA_PRIVATE, digest,
80 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020081 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +000082 {
Paul Bakker42a29bf2009-07-07 20:18:41 +000083
Ronald Cronac6ae352020-06-26 14:33:03 +020084 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
85 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +000086 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000087
Paul Bakkerbd51b262014-07-10 15:26:12 +020088exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010089 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
90 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +000092}
Paul Bakker33b43f12013-08-20 11:48:36 +020093/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +000094
Paul Bakker33b43f12013-08-20 11:48:36 +020095/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010096void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +010097 int digest, int mod, int radix_N,
98 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +010099 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000100{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200101 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000103
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100104 mbedtls_mpi N, E;
105
106 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200108 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000109
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100110 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
111 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
112 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
113 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000115
Paul Bakker42a29bf2009-07-07 20:18:41 +0000116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100118 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 +0000119
Azim Khand30ca132017-06-09 04:32:58 +0100120 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str->x ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100121
Paul Bakkerbd51b262014-07-10 15:26:12 +0200122exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100123 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000125}
Paul Bakker33b43f12013-08-20 11:48:36 +0200126/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000127
Paul Bakker821fb082009-07-12 13:26:42 +0000128
Paul Bakker33b43f12013-08-20 11:48:36 +0200129/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100130void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100131 int padding_mode, int mod, int radix_P,
132 char * input_P, int radix_Q, char * input_Q,
133 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200134 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000135{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200136 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100138 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200139 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100142 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
143 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000144
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200145 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200146 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000147
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100148 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
149 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
150 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
151 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000152
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100153 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
154 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100155 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000157
Paul Bakker821fb082009-07-12 13:26:42 +0000158
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200159 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
160 &rnd_info, MBEDTLS_RSA_PRIVATE,
161 MBEDTLS_MD_NONE, hash_result->len,
162 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000163
Paul Bakker821fb082009-07-12 13:26:42 +0000164
Ronald Cronac6ae352020-06-26 14:33:03 +0200165 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
166 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000167
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200168#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100169 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100171 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100172 int res;
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200173 memset( output, 0x00, sizeof( output) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100174
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100175 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200176 &mbedtls_test_rnd_pseudo_rand, &rnd_info,
177 MBEDTLS_RSA_PRIVATE, hash_result->len,
178 hash_result->x, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100179
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100180#if !defined(MBEDTLS_RSA_ALT)
181 TEST_ASSERT( res == 0 );
182#else
183 TEST_ASSERT( ( res == 0 ) ||
TRodziewiczb579ccd2021-04-13 14:28:28 +0200184 ( res == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100185#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100186
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100187 if( res == 0 )
188 {
Ronald Cronac6ae352020-06-26 14:33:03 +0200189 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200190 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200191 result_str->len ) == 0 );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100192 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100193 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200194#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100195
Paul Bakkerbd51b262014-07-10 15:26:12 +0200196exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100197 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
198 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000201}
Paul Bakker33b43f12013-08-20 11:48:36 +0200202/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000203
Paul Bakker33b43f12013-08-20 11:48:36 +0200204/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100205void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200206 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100207 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100208 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000209{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200210 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000212
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100213 mbedtls_mpi N, E;
214 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100217 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000218
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100219 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
220 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000221
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100222 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
223 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000225
Paul Bakker821fb082009-07-12 13:26:42 +0000226
Azim Khand30ca132017-06-09 04:32:58 +0100227 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE, hash_result->len, hash_result->x, result_str->x ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100228
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200229#if defined(MBEDTLS_PKCS1_V15)
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100230 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100232 {
Manuel Pégourié-Gonnard1ba8a3f2018-03-13 13:27:14 +0100233 int res;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100234 int ok;
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200235 size_t olen;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100236
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100237 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Azim Khand30ca132017-06-09 04:32:58 +0100239 &olen, result_str->x, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100240
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100241#if !defined(MBEDTLS_RSA_ALT)
242 TEST_ASSERT( res == 0 );
243#else
244 TEST_ASSERT( ( res == 0 ) ||
TRodziewiczb579ccd2021-04-13 14:28:28 +0200245 ( res == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) );
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100246#endif
247
248 if( res == 0 )
249 {
Azim Khand30ca132017-06-09 04:32:58 +0100250 ok = olen == hash_result->len && memcmp( output, hash_result->x, olen ) == 0;
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100251 if( correct == 0 )
252 TEST_ASSERT( ok == 1 );
253 else
254 TEST_ASSERT( ok == 0 );
255 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100256 }
Manuel Pégourié-Gonnard43be6cd2017-06-20 09:53:42 +0200257#endif /* MBEDTLS_PKCS1_V15 */
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100258
Paul Bakkerbd51b262014-07-10 15:26:12 +0200259exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100260 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000262}
Paul Bakker33b43f12013-08-20 11:48:36 +0200263/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000264
Paul Bakker33b43f12013-08-20 11:48:36 +0200265/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100266void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100267 int mod, int radix_N, char * input_N,
268 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200269 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000270{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200271 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200273 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000274
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100275 mbedtls_mpi N, E;
276 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
277
Ronald Cron351f0ee2020-06-10 12:12:18 +0200278 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200281 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000282
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100283 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
284 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000285
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100286 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
287 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000289
Paul Bakker42a29bf2009-07-07 20:18:41 +0000290
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200291 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
292 &mbedtls_test_rnd_pseudo_rand,
293 &rnd_info, MBEDTLS_RSA_PUBLIC,
294 message_str->len, message_str->x,
295 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200296 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000297 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000298
Ronald Cronac6ae352020-06-26 14:33:03 +0200299 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
300 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000301 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100302
Paul Bakkerbd51b262014-07-10 15:26:12 +0200303exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100304 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000306}
Paul Bakker33b43f12013-08-20 11:48:36 +0200307/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000308
Paul Bakker33b43f12013-08-20 11:48:36 +0200309/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100310void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100311 int mod, int radix_N, char * input_N,
312 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200313 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000314{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200315 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000317
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100318 mbedtls_mpi N, E;
319
320 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200322 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000323
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100324 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
325 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000326
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100327 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
328 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000330
Paul Bakkera6656852010-07-18 19:47:14 +0000331
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200332 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
333 NULL, MBEDTLS_RSA_PUBLIC,
334 message_str->len, message_str->x,
335 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200336 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000337 {
Paul Bakkera6656852010-07-18 19:47:14 +0000338
Ronald Cronac6ae352020-06-26 14:33:03 +0200339 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
340 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000341 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100342
Paul Bakkerbd51b262014-07-10 15:26:12 +0200343exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100344 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000346}
Paul Bakker33b43f12013-08-20 11:48:36 +0200347/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000348
Paul Bakker33b43f12013-08-20 11:48:36 +0200349/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100350void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100351 int mod, int radix_P, char * input_P,
352 int radix_Q, char * input_Q, int radix_N,
353 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200354 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100355 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000356{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200357 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000359 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200360 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100361 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000362
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100363 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
364 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000367
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200368 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200369 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000370
Paul Bakker42a29bf2009-07-07 20:18:41 +0000371
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100372 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
373 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
374 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
375 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000376
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100377 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
378 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100379 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000381
Paul Bakker69998dd2009-07-11 19:15:20 +0000382 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000383
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200384 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
385 &rnd_info, MBEDTLS_RSA_PRIVATE,
386 &output_len, message_str->x, output,
387 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200388 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000389 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000390
Ronald Cronac6ae352020-06-26 14:33:03 +0200391 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200392 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200393 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000394 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000395
Paul Bakkerbd51b262014-07-10 15:26:12 +0200396exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100397 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
398 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000400}
Paul Bakker33b43f12013-08-20 11:48:36 +0200401/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000402
Paul Bakker33b43f12013-08-20 11:48:36 +0200403/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100404void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100405 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200406 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000407{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200408 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000410
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100411 mbedtls_mpi N, E;
412
413 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
415 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200416 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000417
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100418 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 Bakker821fb082009-07-12 13:26:42 +0000420
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100421 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
422 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000424
Paul Bakker821fb082009-07-12 13:26:42 +0000425
Azim Khand30ca132017-06-09 04:32:58 +0100426 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200427 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000428 {
Paul Bakker821fb082009-07-12 13:26:42 +0000429
Ronald Cronac6ae352020-06-26 14:33:03 +0200430 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
431 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000432 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100433
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100434 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200436 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100440
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200441 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100442 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100443 if( result == 0 )
444 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100445
Ronald Cronac6ae352020-06-26 14:33:03 +0200446 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
447 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100448 }
449
Paul Bakkerbd51b262014-07-10 15:26:12 +0200450exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100451 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_rsa_free( &ctx );
453 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000454}
Paul Bakker33b43f12013-08-20 11:48:36 +0200455/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000456
Paul Bakker33b43f12013-08-20 11:48:36 +0200457/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100458void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100459 char * input_P, int radix_Q, char * input_Q,
460 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200461 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100462 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000463{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200464 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100466 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200467 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200468 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000469
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100470 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
471 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
473 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000474
Ronald Cron351f0ee2020-06-10 12:12:18 +0200475 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000476
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100477 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
478 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
479 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
480 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000481
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100482 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
483 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100484 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000486
Paul Bakker821fb082009-07-12 13:26:42 +0000487
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200488 /* repeat three times to test updating of blinding values */
489 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000490 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200491 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200492 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
493 &rnd_info, message_str->x,
494 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200495 if( result == 0 )
496 {
Paul Bakker821fb082009-07-12 13:26:42 +0000497
Ronald Cronac6ae352020-06-26 14:33:03 +0200498 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200499 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200500 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200501 }
Paul Bakker821fb082009-07-12 13:26:42 +0000502 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000503
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100504 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200506 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100510
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200511 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200512 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
513 &rnd_info, message_str->x,
514 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100515 if( result == 0 )
516 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100517
Ronald Cronac6ae352020-06-26 14:33:03 +0200518 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200519 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200520 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100521 }
522
Paul Bakkerbd51b262014-07-10 15:26:12 +0200523exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100524 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
525 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000528}
Paul Bakker33b43f12013-08-20 11:48:36 +0200529/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000530
Paul Bakker33b43f12013-08-20 11:48:36 +0200531/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100532void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_rsa_context ctx;
535 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000538}
Paul Bakker33b43f12013-08-20 11:48:36 +0200539/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000540
Paul Bakker33b43f12013-08-20 11:48:36 +0200541/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100542void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
543 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100546 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000547
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100548 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000550
Paul Bakker33b43f12013-08-20 11:48:36 +0200551 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000552 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100553 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000554 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200555 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000556 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100557 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000558 }
559
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100560 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100562
Paul Bakkerbd51b262014-07-10 15:26:12 +0200563exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100564 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000566}
Paul Bakker33b43f12013-08-20 11:48:36 +0200567/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000568
Paul Bakker33b43f12013-08-20 11:48:36 +0200569/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100570void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
571 int radix_Q, char * input_Q, int radix_N,
572 char * input_N, int radix_E, char * input_E,
573 int radix_D, char * input_D, int radix_DP,
574 char * input_DP, int radix_DQ,
575 char * input_DQ, int radix_QP,
576 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000577{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000581
Paul Bakker33b43f12013-08-20 11:48:36 +0200582 ctx.len = mod / 8;
583 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000586 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200587 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000590 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200591 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000594 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200595 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000598 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200599 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000602 }
Hanno Becker131134f2017-08-23 08:31:07 +0100603#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200604 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000607 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200608 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000611 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200612 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000615 }
Hanno Becker131134f2017-08-23 08:31:07 +0100616#else
617 ((void) radix_DP); ((void) input_DP);
618 ((void) radix_DQ); ((void) input_DQ);
619 ((void) radix_QP); ((void) input_QP);
620#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100623
Paul Bakkerbd51b262014-07-10 15:26:12 +0200624exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000626}
Paul Bakker33b43f12013-08-20 11:48:36 +0200627/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000628
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100629/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100630void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
631 int radix_Epub, char * input_Epub, int radix_P,
632 char * input_P, int radix_Q, char * input_Q,
633 int radix_N, char * input_N, int radix_E,
634 char * input_E, int radix_D, char * input_D,
635 int radix_DP, char * input_DP, int radix_DQ,
636 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100637 int result )
638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
642 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100643
644 pub.len = mod / 8;
645 prv.len = mod / 8;
646
647 if( strlen( input_Npub ) )
648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100650 }
651 if( strlen( input_Epub ) )
652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100654 }
655
656 if( strlen( input_P ) )
657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100659 }
660 if( strlen( input_Q ) )
661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100663 }
664 if( strlen( input_N ) )
665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100667 }
668 if( strlen( input_E ) )
669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100671 }
672 if( strlen( input_D ) )
673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100675 }
Hanno Becker131134f2017-08-23 08:31:07 +0100676#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100677 if( strlen( input_DP ) )
678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100680 }
681 if( strlen( input_DQ ) )
682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100684 }
685 if( strlen( input_QP ) )
686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100688 }
Hanno Becker131134f2017-08-23 08:31:07 +0100689#else
690 ((void) radix_DP); ((void) input_DP);
691 ((void) radix_DQ); ((void) input_DQ);
692 ((void) radix_QP); ((void) input_QP);
693#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100696
697exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 mbedtls_rsa_free( &pub );
699 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100700}
701/* END_CASE */
702
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100703/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_rsa_context ctx;
707 mbedtls_entropy_context entropy;
708 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200709 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000710
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200711 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +0100713 mbedtls_rsa_init ( &ctx, 0, 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000714
Hanno Beckera47023e2017-12-22 17:08:03 +0000715 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
716 &entropy, (const unsigned char *) pers,
717 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200720 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100723 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000724 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100725
Paul Bakkerbd51b262014-07-10 15:26:12 +0200726exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_rsa_free( &ctx );
728 mbedtls_ctr_drbg_free( &ctr_drbg );
729 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000730}
Paul Bakker33b43f12013-08-20 11:48:36 +0200731/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000732
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100733/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +0100734void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100735 int radix_D, char *input_D,
736 int radix_E, char *input_E,
737 int radix_P, char *output_P,
738 int radix_Q, char *output_Q,
739 int corrupt, int result )
740{
741 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
742
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100743 mbedtls_mpi_init( &N );
744 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
745 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
746 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
747
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100748 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
749 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
750 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
751 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
752 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
753
754 if( corrupt )
755 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
756
757 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100758 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100759
760 if( !corrupt )
761 {
762 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
763 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
764 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
765 }
766
767exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100768 mbedtls_mpi_free( &N );
769 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
770 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
771 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100772}
773/* END_CASE */
774
Hanno Becker6b4ce492017-08-23 11:00:21 +0100775/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100776void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
777 int radix_Q, char *input_Q,
778 int radix_E, char *input_E,
779 int radix_D, char *output_D,
780 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +0100781{
782 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
783
784 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
785 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
786 mbedtls_mpi_init( &E );
787 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
788
789 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
790 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
791 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
792 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
793
794 if( corrupt )
795 {
796 /* Make E even */
797 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
798 }
799
800 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100801 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
802 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100803
804 if( !corrupt )
805 {
806 /*
807 * Check that D and Dp agree modulo LCM(P-1, Q-1).
808 */
809
810 /* Replace P,Q by P-1, Q-1 */
811 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
812 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
813
814 /* Check D == Dp modulo P-1 */
815 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
816 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
817 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
818
819 /* Check D == Dp modulo Q-1 */
820 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
821 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
822 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
823 }
824
825exit:
826
827 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
828 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
829 mbedtls_mpi_free( &E );
830 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
831}
832/* END_CASE */
833
Hanno Beckerf40cdf92017-12-22 11:03:27 +0000834/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +0100835void mbedtls_rsa_import( int radix_N, char *input_N,
836 int radix_P, char *input_P,
837 int radix_Q, char *input_Q,
838 int radix_D, char *input_D,
839 int radix_E, char *input_E,
840 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +0100841 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +0100842 int res_check,
843 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100844{
845 mbedtls_mpi N, P, Q, D, E;
846 mbedtls_rsa_context ctx;
847
Hanno Beckere1582a82017-09-29 11:51:05 +0100848 /* Buffers used for encryption-decryption test */
849 unsigned char *buf_orig = NULL;
850 unsigned char *buf_enc = NULL;
851 unsigned char *buf_dec = NULL;
852
Hanno Beckerc77ab892017-08-23 11:01:06 +0100853 mbedtls_entropy_context entropy;
854 mbedtls_ctr_drbg_context ctr_drbg;
855 const char *pers = "test_suite_rsa";
856
Hanno Becker4d6e8342017-09-29 11:50:18 +0100857 const int have_N = ( strlen( input_N ) > 0 );
858 const int have_P = ( strlen( input_P ) > 0 );
859 const int have_Q = ( strlen( input_Q ) > 0 );
860 const int have_D = ( strlen( input_D ) > 0 );
861 const int have_E = ( strlen( input_E ) > 0 );
862
Hanno Beckerc77ab892017-08-23 11:01:06 +0100863 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100864 mbedtls_entropy_init( &entropy );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100865 mbedtls_rsa_init( &ctx, 0, 0 );
866
867 mbedtls_mpi_init( &N );
868 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
869 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
870
Hanno Beckerd4d60572018-01-10 07:12:01 +0000871 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
872 (const unsigned char *) pers, strlen( pers ) ) == 0 );
873
Hanno Becker4d6e8342017-09-29 11:50:18 +0100874 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100875 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
876
Hanno Becker4d6e8342017-09-29 11:50:18 +0100877 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100878 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
879
Hanno Becker4d6e8342017-09-29 11:50:18 +0100880 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100881 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
882
Hanno Becker4d6e8342017-09-29 11:50:18 +0100883 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100884 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
885
Hanno Becker4d6e8342017-09-29 11:50:18 +0100886 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100887 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
888
889 if( !successive )
890 {
891 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100892 have_N ? &N : NULL,
893 have_P ? &P : NULL,
894 have_Q ? &Q : NULL,
895 have_D ? &D : NULL,
896 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100897 }
898 else
899 {
900 /* Import N, P, Q, D, E separately.
901 * This should make no functional difference. */
902
903 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100904 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100905 NULL, NULL, NULL, NULL ) == 0 );
906
907 TEST_ASSERT( mbedtls_rsa_import( &ctx,
908 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100909 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100910 NULL, NULL, NULL ) == 0 );
911
912 TEST_ASSERT( mbedtls_rsa_import( &ctx,
913 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100914 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100915 NULL, NULL ) == 0 );
916
917 TEST_ASSERT( mbedtls_rsa_import( &ctx,
918 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100919 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100920 NULL ) == 0 );
921
922 TEST_ASSERT( mbedtls_rsa_import( &ctx,
923 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100924 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100925 }
926
Hanno Becker04877a42017-10-11 10:01:33 +0100927 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100928
Hanno Beckere1582a82017-09-29 11:51:05 +0100929 /* On expected success, perform some public and private
930 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +0100931 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +0100932 {
Hanno Beckere1582a82017-09-29 11:51:05 +0100933 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +0100934 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
935 else
936 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
937
938 if( res_check != 0 )
939 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +0100940
941 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
942 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
943 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
944 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
945 goto exit;
946
947 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
948 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
949
950 /* Make sure the number we're generating is smaller than the modulus */
951 buf_orig[0] = 0x00;
952
953 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
954
955 if( is_priv )
956 {
957 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
958 &ctr_drbg, buf_enc,
959 buf_dec ) == 0 );
960
961 TEST_ASSERT( memcmp( buf_orig, buf_dec,
962 mbedtls_rsa_get_len( &ctx ) ) == 0 );
963 }
964 }
965
Hanno Beckerc77ab892017-08-23 11:01:06 +0100966exit:
967
Hanno Beckere1582a82017-09-29 11:51:05 +0100968 mbedtls_free( buf_orig );
969 mbedtls_free( buf_enc );
970 mbedtls_free( buf_dec );
971
Hanno Beckerc77ab892017-08-23 11:01:06 +0100972 mbedtls_rsa_free( &ctx );
973
974 mbedtls_ctr_drbg_free( &ctr_drbg );
975 mbedtls_entropy_free( &entropy );
976
977 mbedtls_mpi_free( &N );
978 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
979 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
980}
981/* END_CASE */
982
Hanno Becker417f2d62017-08-23 11:44:51 +0100983/* BEGIN_CASE */
984void mbedtls_rsa_export( int radix_N, char *input_N,
985 int radix_P, char *input_P,
986 int radix_Q, char *input_Q,
987 int radix_D, char *input_D,
988 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +0100989 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +0100990 int successive )
991{
992 /* Original MPI's with which we set up the RSA context */
993 mbedtls_mpi N, P, Q, D, E;
994
995 /* Exported MPI's */
996 mbedtls_mpi Ne, Pe, Qe, De, Ee;
997
998 const int have_N = ( strlen( input_N ) > 0 );
999 const int have_P = ( strlen( input_P ) > 0 );
1000 const int have_Q = ( strlen( input_Q ) > 0 );
1001 const int have_D = ( strlen( input_D ) > 0 );
1002 const int have_E = ( strlen( input_E ) > 0 );
1003
Hanno Becker417f2d62017-08-23 11:44:51 +01001004 mbedtls_rsa_context ctx;
1005
1006 mbedtls_rsa_init( &ctx, 0, 0 );
1007
1008 mbedtls_mpi_init( &N );
1009 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1010 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1011
1012 mbedtls_mpi_init( &Ne );
1013 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1014 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1015
1016 /* Setup RSA context */
1017
1018 if( have_N )
1019 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1020
1021 if( have_P )
1022 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1023
1024 if( have_Q )
1025 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1026
1027 if( have_D )
1028 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1029
1030 if( have_E )
1031 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1032
1033 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1034 strlen( input_N ) ? &N : NULL,
1035 strlen( input_P ) ? &P : NULL,
1036 strlen( input_Q ) ? &Q : NULL,
1037 strlen( input_D ) ? &D : NULL,
1038 strlen( input_E ) ? &E : NULL ) == 0 );
1039
Hanno Becker7f25f852017-10-10 16:56:22 +01001040 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001041
1042 /*
1043 * Export parameters and compare to original ones.
1044 */
1045
1046 /* N and E must always be present. */
1047 if( !successive )
1048 {
1049 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1050 }
1051 else
1052 {
1053 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1054 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1055 }
1056 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1057 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1058
1059 /* If we were providing enough information to setup a complete private context,
1060 * we expect to be able to export all core parameters. */
1061
1062 if( is_priv )
1063 {
1064 if( !successive )
1065 {
1066 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1067 &De, NULL ) == 0 );
1068 }
1069 else
1070 {
1071 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1072 NULL, NULL ) == 0 );
1073 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1074 NULL, NULL ) == 0 );
1075 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1076 &De, NULL ) == 0 );
1077 }
1078
1079 if( have_P )
1080 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1081
1082 if( have_Q )
1083 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1084
1085 if( have_D )
1086 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1087
1088 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001089 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1090 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001091 }
1092
1093exit:
1094
1095 mbedtls_rsa_free( &ctx );
1096
1097 mbedtls_mpi_free( &N );
1098 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1099 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1100
1101 mbedtls_mpi_free( &Ne );
1102 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1103 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1104}
1105/* END_CASE */
1106
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001107/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001108void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1109 int radix_P, char *input_P,
1110 int radix_Q, char *input_Q,
1111 int radix_D, char *input_D,
1112 int radix_E, char *input_E,
1113 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001114{
1115 /* Original MPI's with which we set up the RSA context */
1116 mbedtls_mpi N, P, Q, D, E;
1117
1118 const int have_N = ( strlen( input_N ) > 0 );
1119 const int have_P = ( strlen( input_P ) > 0 );
1120 const int have_Q = ( strlen( input_Q ) > 0 );
1121 const int have_D = ( strlen( input_D ) > 0 );
1122 const int have_E = ( strlen( input_E ) > 0 );
1123
1124 mbedtls_entropy_context entropy;
1125 mbedtls_ctr_drbg_context ctr_drbg;
1126 const char *pers = "test_suite_rsa";
1127
1128 mbedtls_mpi_init( &N );
1129 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1130 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1131
1132 mbedtls_ctr_drbg_init( &ctr_drbg );
1133 mbedtls_entropy_init( &entropy );
1134 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1135 &entropy, (const unsigned char *) pers,
1136 strlen( pers ) ) == 0 );
1137
1138 if( have_N )
1139 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1140
1141 if( have_P )
1142 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1143
1144 if( have_Q )
1145 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1146
1147 if( have_D )
1148 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1149
1150 if( have_E )
1151 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1152
Hanno Becker750e8b42017-08-25 07:54:27 +01001153 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1154 have_P ? &P : NULL,
1155 have_Q ? &Q : NULL,
1156 have_D ? &D : NULL,
1157 have_E ? &E : NULL,
1158 prng ? mbedtls_ctr_drbg_random : NULL,
1159 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001160exit:
1161
1162 mbedtls_ctr_drbg_free( &ctr_drbg );
1163 mbedtls_entropy_free( &entropy );
1164
1165 mbedtls_mpi_free( &N );
1166 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1167 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1168}
1169/* END_CASE */
1170
Hanno Beckerc77ab892017-08-23 11:01:06 +01001171/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001172void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1173 data_t *input_Q, data_t *input_D,
1174 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001175 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001176{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001177 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001178 unsigned char bufNe[256];
1179 unsigned char bufPe[128];
1180 unsigned char bufQe[128];
1181 unsigned char bufDe[256];
1182 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001183
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001184 mbedtls_rsa_context ctx;
1185
1186 mbedtls_rsa_init( &ctx, 0, 0 );
1187
1188 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001189 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001190 input_N->len ? input_N->x : NULL, input_N->len,
1191 input_P->len ? input_P->x : NULL, input_P->len,
1192 input_Q->len ? input_Q->x : NULL, input_Q->len,
1193 input_D->len ? input_D->x : NULL, input_D->len,
1194 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001195
Hanno Becker7f25f852017-10-10 16:56:22 +01001196 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001197
1198 /*
1199 * Export parameters and compare to original ones.
1200 */
1201
1202 /* N and E must always be present. */
1203 if( !successive )
1204 {
Azim Khand30ca132017-06-09 04:32:58 +01001205 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001206 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001207 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001208 }
1209 else
1210 {
Azim Khand30ca132017-06-09 04:32:58 +01001211 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001212 NULL, 0, NULL, 0, NULL, 0,
1213 NULL, 0 ) == 0 );
1214 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1215 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001216 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001217 }
Azim Khand30ca132017-06-09 04:32:58 +01001218 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1219 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001220
1221 /* If we were providing enough information to setup a complete private context,
1222 * we expect to be able to export all core parameters. */
1223
1224 if( is_priv )
1225 {
1226 if( !successive )
1227 {
1228 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001229 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1230 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1231 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001232 NULL, 0 ) == 0 );
1233 }
1234 else
1235 {
1236 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001237 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001238 NULL, 0, NULL, 0,
1239 NULL, 0 ) == 0 );
1240
1241 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001242 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001243 NULL, 0, NULL, 0 ) == 0 );
1244
Azim Khand30ca132017-06-09 04:32:58 +01001245 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1246 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001247 NULL, 0 ) == 0 );
1248 }
1249
Azim Khand30ca132017-06-09 04:32:58 +01001250 if( input_P->len )
1251 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001252
Azim Khand30ca132017-06-09 04:32:58 +01001253 if( input_Q->len )
1254 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001255
Azim Khand30ca132017-06-09 04:32:58 +01001256 if( input_D->len )
1257 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001258
1259 }
1260
1261exit:
1262 mbedtls_rsa_free( &ctx );
1263}
1264/* END_CASE */
1265
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001266/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001267void mbedtls_rsa_import_raw( data_t *input_N,
1268 data_t *input_P, data_t *input_Q,
1269 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001270 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001271 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001272 int res_check,
1273 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001274{
Hanno Beckere1582a82017-09-29 11:51:05 +01001275 /* Buffers used for encryption-decryption test */
1276 unsigned char *buf_orig = NULL;
1277 unsigned char *buf_enc = NULL;
1278 unsigned char *buf_dec = NULL;
1279
Hanno Beckerc77ab892017-08-23 11:01:06 +01001280 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001281 mbedtls_entropy_context entropy;
1282 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001283
Hanno Beckerc77ab892017-08-23 11:01:06 +01001284 const char *pers = "test_suite_rsa";
1285
1286 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001287 mbedtls_entropy_init( &entropy );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001288 mbedtls_rsa_init( &ctx, 0, 0 );
1289
Hanno Beckerc77ab892017-08-23 11:01:06 +01001290 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1291 &entropy, (const unsigned char *) pers,
1292 strlen( pers ) ) == 0 );
1293
Hanno Beckerc77ab892017-08-23 11:01:06 +01001294 if( !successive )
1295 {
1296 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001297 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1298 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1299 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1300 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1301 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001302 }
1303 else
1304 {
1305 /* Import N, P, Q, D, E separately.
1306 * This should make no functional difference. */
1307
1308 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001309 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001310 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1311
1312 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1313 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001314 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001315 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1316
1317 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1318 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001319 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001320 NULL, 0, NULL, 0 ) == 0 );
1321
1322 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1323 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001324 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001325 NULL, 0 ) == 0 );
1326
1327 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1328 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001329 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001330 }
1331
Hanno Becker04877a42017-10-11 10:01:33 +01001332 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001333
Hanno Beckere1582a82017-09-29 11:51:05 +01001334 /* On expected success, perform some public and private
1335 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001336 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001337 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001338 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001339 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1340 else
1341 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1342
1343 if( res_check != 0 )
1344 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001345
1346 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1347 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1348 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1349 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1350 goto exit;
1351
1352 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1353 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1354
1355 /* Make sure the number we're generating is smaller than the modulus */
1356 buf_orig[0] = 0x00;
1357
1358 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1359
1360 if( is_priv )
1361 {
1362 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1363 &ctr_drbg, buf_enc,
1364 buf_dec ) == 0 );
1365
1366 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1367 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1368 }
1369 }
1370
Hanno Beckerc77ab892017-08-23 11:01:06 +01001371exit:
1372
Hanno Becker3f3ae852017-10-02 10:08:39 +01001373 mbedtls_free( buf_orig );
1374 mbedtls_free( buf_enc );
1375 mbedtls_free( buf_dec );
1376
Hanno Beckerc77ab892017-08-23 11:01:06 +01001377 mbedtls_rsa_free( &ctx );
1378
1379 mbedtls_ctr_drbg_free( &ctr_drbg );
1380 mbedtls_entropy_free( &entropy );
1381
1382}
1383/* END_CASE */
1384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001386void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001387{
Andres AG93012e82016-09-09 09:10:28 +01001388 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001389}
Paul Bakker33b43f12013-08-20 11:48:36 +02001390/* END_CASE */