blob: 14b4afc3a34cf473b307a950a50ad94ee2ecc722 [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 */
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
81/* 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 hash_result[MBEDTLS_MD_MAX_SIZE];
89 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010091 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +020092 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +000093
Hanno Beckerceb7a9d2017-08-23 08:33:08 +010094 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
95 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +020096 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +020097 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,padding_mode,
98 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000099
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200100 memset( hash_result, 0x00, sizeof( hash_result ) );
101 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200102 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000103
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100104 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
105 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
106 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
107 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000108
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100109 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
110 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100111 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000113
Paul Bakker42a29bf2009-07-07 20:18:41 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100116 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 +0000117
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200118 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100119 &rnd_info, digest, 0, hash_result,
120 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200121 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000122 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000123
Ronald Cronac6ae352020-06-26 14:33:03 +0200124 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
125 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000126 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000127
Paul Bakkerbd51b262014-07-10 15:26:12 +0200128exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100129 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
130 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000132}
Paul Bakker33b43f12013-08-20 11:48:36 +0200133/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000134
Paul Bakker33b43f12013-08-20 11:48:36 +0200135/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100136void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100137 int digest, int mod, int radix_N,
138 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100139 data_t * result_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000140{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200141 unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_rsa_context ctx;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000143
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100144 mbedtls_mpi N, E;
145
146 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200147 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200148 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
149 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200150 memset( hash_result, 0x00, sizeof( hash_result ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000151
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100152 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
153 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
154 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
155 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000157
Paul Bakker42a29bf2009-07-07 20:18:41 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100160 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 +0000161
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100162 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, digest, 0, hash_result, result_str->x ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100163
Paul Bakkerbd51b262014-07-10 15:26:12 +0200164exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100165 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000167}
Paul Bakker33b43f12013-08-20 11:48:36 +0200168/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000169
Paul Bakker821fb082009-07-12 13:26:42 +0000170
Paul Bakker33b43f12013-08-20 11:48:36 +0200171/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100172void rsa_pkcs1_sign_raw( data_t * hash_result,
Azim Khanf1aaec92017-05-30 14:23:15 +0100173 int padding_mode, int mod, int radix_P,
174 char * input_P, int radix_Q, char * input_Q,
175 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200176 char * input_E, data_t * result_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000177{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200178 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100180 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200181 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000182
Ronald Cronc1905a12021-06-05 11:11:14 +0200183 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200184 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
185 MBEDTLS_MD_NONE ) == 0 );
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100186 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
187 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Paul Bakker821fb082009-07-12 13:26:42 +0000188
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200189 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200190 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000191
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100192 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
193 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
194 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
195 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000196
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100197 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
198 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100199 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000201
Paul Bakker821fb082009-07-12 13:26:42 +0000202
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200203 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney140184d2021-05-18 16:04:07 +0100204 &rnd_info, MBEDTLS_MD_NONE,
205 hash_result->len,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200206 hash_result->x, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000207
Paul Bakker821fb082009-07-12 13:26:42 +0000208
Ronald Cronac6ae352020-06-26 14:33:03 +0200209 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
210 ctx.len, result_str->len ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000211
Paul Bakkerbd51b262014-07-10 15:26:12 +0200212exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100213 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
214 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000217}
Paul Bakker33b43f12013-08-20 11:48:36 +0200218/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000219
Paul Bakker33b43f12013-08-20 11:48:36 +0200220/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100221void rsa_pkcs1_verify_raw( data_t * hash_result,
Paul Bakker33b43f12013-08-20 11:48:36 +0200222 int padding_mode, int mod, int radix_N,
Azim Khanf1aaec92017-05-30 14:23:15 +0100223 char * input_N, int radix_E, char * input_E,
Azim Khan5fcca462018-06-29 11:05:32 +0100224 data_t * result_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000225{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200226 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000228
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100229 mbedtls_mpi N, E;
230 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
231
Ronald Cronc1905a12021-06-05 11:11:14 +0200232 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200233 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
234 MBEDTLS_MD_NONE ) == 0 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100235 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000236
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100237 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
238 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000239
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100240 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
241 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000243
Paul Bakker821fb082009-07-12 13:26:42 +0000244
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100245 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 +0100246
Paul Bakkerbd51b262014-07-10 15:26:12 +0200247exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100248 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000250}
Paul Bakker33b43f12013-08-20 11:48:36 +0200251/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000252
Paul Bakker33b43f12013-08-20 11:48:36 +0200253/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100254void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100255 int mod, int radix_N, char * input_N,
256 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200257 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000258{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200259 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_rsa_context ctx;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200261 mbedtls_test_rnd_pseudo_info rnd_info;
Paul Bakker997bbd12011-03-13 15:45:42 +0000262
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100263 mbedtls_mpi N, E;
264 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
265
Ronald Cron351f0ee2020-06-10 12:12:18 +0200266 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000267
Ronald Cronc1905a12021-06-05 11:11:14 +0200268 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200269 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
270 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200271 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000272
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100273 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
274 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000275
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100276 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
277 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000279
Paul Bakker42a29bf2009-07-07 20:18:41 +0000280
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200281 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
282 &mbedtls_test_rnd_pseudo_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100283 &rnd_info, message_str->len,
284 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200285 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200286 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000287 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000288
Ronald Cronac6ae352020-06-26 14:33:03 +0200289 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
290 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000291 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100292
Paul Bakkerbd51b262014-07-10 15:26:12 +0200293exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100294 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000296}
Paul Bakker33b43f12013-08-20 11:48:36 +0200297/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000298
Paul Bakker33b43f12013-08-20 11:48:36 +0200299/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100300void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
Azim Khand30ca132017-06-09 04:32:58 +0100301 int mod, int radix_N, char * input_N,
302 int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200303 data_t * result_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000304{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200305 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_rsa_context ctx;
Paul Bakkera6656852010-07-18 19:47:14 +0000307
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100308 mbedtls_mpi N, E;
309
310 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200311 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200312 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
313 MBEDTLS_MD_NONE ) == 0 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200314 memset( output, 0x00, sizeof( output ) );
Paul Bakkera6656852010-07-18 19:47:14 +0000315
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100316 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
317 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000318
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100319 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
320 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000322
Paul Bakkera6656852010-07-18 19:47:14 +0000323
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200324 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand,
Thomas Daubney21772772021-05-13 17:30:32 +0100325 NULL, message_str->len,
326 message_str->x,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200327 output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200328 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000329 {
Paul Bakkera6656852010-07-18 19:47:14 +0000330
Ronald Cronac6ae352020-06-26 14:33:03 +0200331 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
332 ctx.len, result_str->len ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000333 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100334
Paul Bakkerbd51b262014-07-10 15:26:12 +0200335exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100336 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000338}
Paul Bakker33b43f12013-08-20 11:48:36 +0200339/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000340
Paul Bakker33b43f12013-08-20 11:48:36 +0200341/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100342void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
Azim Khanf1aaec92017-05-30 14:23:15 +0100343 int mod, int radix_P, char * input_P,
344 int radix_Q, char * input_Q, int radix_N,
345 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200346 int max_output, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100347 int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000348{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200349 unsigned char output[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000351 size_t output_len;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200352 mbedtls_test_rnd_pseudo_info rnd_info;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100353 mbedtls_mpi N, P, Q, E;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000354
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100355 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
356 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
357
Ronald Cronc1905a12021-06-05 11:11:14 +0200358 mbedtls_rsa_init( &ctx );
Ronald Cron266b6d22021-06-08 10:03:49 +0200359 TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
360 MBEDTLS_MD_NONE ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000361
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200362 memset( output, 0x00, sizeof( output ) );
Ronald Cron351f0ee2020-06-10 12:12:18 +0200363 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000364
Paul Bakker42a29bf2009-07-07 20:18:41 +0000365
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100366 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
367 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
368 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
369 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000370
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100371 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
372 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100373 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000375
Paul Bakker69998dd2009-07-11 19:15:20 +0000376 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000377
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200378 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100379 &rnd_info,
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200380 &output_len, message_str->x, output,
381 max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200382 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000383 {
Paul Bakker42a29bf2009-07-07 20:18:41 +0000384
Ronald Cronac6ae352020-06-26 14:33:03 +0200385 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200386 output_len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200387 result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000388 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000389
Paul Bakkerbd51b262014-07-10 15:26:12 +0200390exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100391 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
392 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000394}
Paul Bakker33b43f12013-08-20 11:48:36 +0200395/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000396
Paul Bakker33b43f12013-08-20 11:48:36 +0200397/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100398void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
Azim Khand30ca132017-06-09 04:32:58 +0100399 char * input_N, int radix_E, char * input_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200400 data_t * result_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000401{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200402 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000404
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100405 mbedtls_mpi N, E;
406
407 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200408 mbedtls_rsa_init( &ctx );
409 mbedtls_rsa_init( &ctx2 );
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200410 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000411
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100412 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
413 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000414
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100415 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
416 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000418
Paul Bakker821fb082009-07-12 13:26:42 +0000419
Azim Khand30ca132017-06-09 04:32:58 +0100420 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200421 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000422 {
Paul Bakker821fb082009-07-12 13:26:42 +0000423
Ronald Cronac6ae352020-06-26 14:33:03 +0200424 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
425 ctx.len, result_str->len ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000426 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100427
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100428 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200430 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100434
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200435 memset( output, 0x00, sizeof( output ) );
Azim Khand30ca132017-06-09 04:32:58 +0100436 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100437 if( result == 0 )
438 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100439
Ronald Cronac6ae352020-06-26 14:33:03 +0200440 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
441 ctx.len, result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100442 }
443
Paul Bakkerbd51b262014-07-10 15:26:12 +0200444exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100445 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_rsa_free( &ctx );
447 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000448}
Paul Bakker33b43f12013-08-20 11:48:36 +0200449/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000450
Paul Bakker33b43f12013-08-20 11:48:36 +0200451/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100452void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
Azim Khand30ca132017-06-09 04:32:58 +0100453 char * input_P, int radix_Q, char * input_Q,
454 int radix_N, char * input_N, int radix_E,
Ronald Cronac6ae352020-06-26 14:33:03 +0200455 char * input_E, data_t * result_str,
Azim Khand30ca132017-06-09 04:32:58 +0100456 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000457{
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200458 unsigned char output[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100460 mbedtls_mpi N, P, Q, E;
Ronald Cron351f0ee2020-06-10 12:12:18 +0200461 mbedtls_test_rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200462 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000463
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100464 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
465 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200466 mbedtls_rsa_init( &ctx );
467 mbedtls_rsa_init( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000468
Ronald Cron351f0ee2020-06-10 12:12:18 +0200469 memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000470
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100471 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
472 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
473 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
474 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000475
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100476 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
477 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100478 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000480
Paul Bakker821fb082009-07-12 13:26:42 +0000481
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200482 /* repeat three times to test updating of blinding values */
483 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000484 {
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200485 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200486 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand,
487 &rnd_info, message_str->x,
488 output ) == result );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200489 if( result == 0 )
490 {
Paul Bakker821fb082009-07-12 13:26:42 +0000491
Ronald Cronac6ae352020-06-26 14:33:03 +0200492 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200493 ctx.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200494 result_str->len ) == 0 );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200495 }
Paul Bakker821fb082009-07-12 13:26:42 +0000496 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000497
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100498 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200500 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100504
Ron Eldorfdc15bd2018-11-22 15:47:51 +0200505 memset( output, 0x00, sizeof( output ) );
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200506 TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand,
507 &rnd_info, message_str->x,
508 output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100509 if( result == 0 )
510 {
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100511
Ronald Cronac6ae352020-06-26 14:33:03 +0200512 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
Ronald Cron2dbba992020-06-10 11:42:32 +0200513 ctx2.len,
Ronald Cronac6ae352020-06-26 14:33:03 +0200514 result_str->len ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100515 }
516
Paul Bakkerbd51b262014-07-10 15:26:12 +0200517exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100518 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
519 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000522}
Paul Bakker33b43f12013-08-20 11:48:36 +0200523/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000524
Paul Bakker33b43f12013-08-20 11:48:36 +0200525/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100526void rsa_check_privkey_null( )
Paul Bakker37940d9f2009-07-10 22:38:58 +0000527{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_rsa_context ctx;
529 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000532}
Paul Bakker33b43f12013-08-20 11:48:36 +0200533/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000534
Paul Bakker33b43f12013-08-20 11:48:36 +0200535/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100536void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
537 char * input_E, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_rsa_context ctx;
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100540 mbedtls_mpi N, E;
Paul Bakker821fb082009-07-12 13:26:42 +0000541
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100542 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Ronald Cronc1905a12021-06-05 11:11:14 +0200543 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000544
Paul Bakker33b43f12013-08-20 11:48:36 +0200545 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000546 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100547 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000548 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200549 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000550 {
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100551 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000552 }
553
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100554 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100556
Paul Bakkerbd51b262014-07-10 15:26:12 +0200557exit:
Hanno Beckerceb7a9d2017-08-23 08:33:08 +0100558 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000560}
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000562
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100564void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
565 int radix_Q, char * input_Q, int radix_N,
566 char * input_N, int radix_E, char * input_E,
567 int radix_D, char * input_D, int radix_DP,
568 char * input_DP, int radix_DQ,
569 char * input_DQ, int radix_QP,
570 char * input_QP, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000571{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000573
Ronald Cronc1905a12021-06-05 11:11:14 +0200574 mbedtls_rsa_init( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000575
Paul Bakker33b43f12013-08-20 11:48:36 +0200576 ctx.len = mod / 8;
577 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000580 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200581 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000584 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200585 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000588 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200589 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000592 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200593 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000596 }
Hanno Becker131134f2017-08-23 08:31:07 +0100597#if !defined(MBEDTLS_RSA_NO_CRT)
Paul Bakker33b43f12013-08-20 11:48:36 +0200598 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000601 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200602 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000605 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200606 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000609 }
Hanno Becker131134f2017-08-23 08:31:07 +0100610#else
611 ((void) radix_DP); ((void) input_DP);
612 ((void) radix_DQ); ((void) input_DQ);
613 ((void) radix_QP); ((void) input_QP);
614#endif
Paul Bakker821fb082009-07-12 13:26:42 +0000615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100617
Paul Bakkerbd51b262014-07-10 15:26:12 +0200618exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000620}
Paul Bakker33b43f12013-08-20 11:48:36 +0200621/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000622
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100623/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100624void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
625 int radix_Epub, char * input_Epub, int radix_P,
626 char * input_P, int radix_Q, char * input_Q,
627 int radix_N, char * input_N, int radix_E,
628 char * input_E, int radix_D, char * input_D,
629 int radix_DP, char * input_DP, int radix_DQ,
630 char * input_DQ, int radix_QP, char * input_QP,
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100631 int result )
632{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100634
Ronald Cronc1905a12021-06-05 11:11:14 +0200635 mbedtls_rsa_init( &pub );
636 mbedtls_rsa_init( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100637
638 pub.len = mod / 8;
639 prv.len = mod / 8;
640
641 if( strlen( input_Npub ) )
642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100644 }
645 if( strlen( input_Epub ) )
646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100648 }
649
650 if( strlen( input_P ) )
651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100653 }
654 if( strlen( input_Q ) )
655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100657 }
658 if( strlen( input_N ) )
659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100661 }
662 if( strlen( input_E ) )
663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100665 }
666 if( strlen( input_D ) )
667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100669 }
Hanno Becker131134f2017-08-23 08:31:07 +0100670#if !defined(MBEDTLS_RSA_NO_CRT)
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100671 if( strlen( input_DP ) )
672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100674 }
675 if( strlen( input_DQ ) )
676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100678 }
679 if( strlen( input_QP ) )
680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100682 }
Hanno Becker131134f2017-08-23 08:31:07 +0100683#else
684 ((void) radix_DP); ((void) input_DP);
685 ((void) radix_DQ); ((void) input_DQ);
686 ((void) radix_QP); ((void) input_QP);
687#endif
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100690
691exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 mbedtls_rsa_free( &pub );
693 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100694}
695/* END_CASE */
696
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100697/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000699{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_rsa_context ctx;
701 mbedtls_entropy_context entropy;
702 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200703 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000704
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200705 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_entropy_init( &entropy );
Ronald Cronc1905a12021-06-05 11:11:14 +0200707 mbedtls_rsa_init ( &ctx );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000708
Hanno Beckera47023e2017-12-22 17:08:03 +0000709 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
710 &entropy, (const unsigned char *) pers,
711 strlen( pers ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200714 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100717 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000718 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100719
Paul Bakkerbd51b262014-07-10 15:26:12 +0200720exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_rsa_free( &ctx );
722 mbedtls_ctr_drbg_free( &ctr_drbg );
723 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000724}
Paul Bakker33b43f12013-08-20 11:48:36 +0200725/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000726
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100727/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Becker0f65e0c2017-10-03 14:39:16 +0100728void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100729 int radix_D, char *input_D,
730 int radix_E, char *input_E,
731 int radix_P, char *output_P,
732 int radix_Q, char *output_Q,
733 int corrupt, int result )
734{
735 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
736
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100737 mbedtls_mpi_init( &N );
738 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
739 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
740 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
741
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100742 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
743 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
744 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
745 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
746 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
747
748 if( corrupt )
749 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
750
751 /* Try to deduce P, Q from N, D, E only. */
Hanno Beckerf9e184b2017-10-10 16:49:26 +0100752 TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100753
754 if( !corrupt )
755 {
756 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
757 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
758 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
759 }
760
761exit:
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100762 mbedtls_mpi_free( &N );
763 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
764 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
765 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100766}
767/* END_CASE */
768
Hanno Becker6b4ce492017-08-23 11:00:21 +0100769/* BEGIN_CASE */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100770void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
771 int radix_Q, char *input_Q,
772 int radix_E, char *input_E,
773 int radix_D, char *output_D,
774 int corrupt, int result )
Hanno Becker6b4ce492017-08-23 11:00:21 +0100775{
776 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
777
778 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
779 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
780 mbedtls_mpi_init( &E );
781 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
782
783 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
784 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
785 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
786 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
787
788 if( corrupt )
789 {
790 /* Make E even */
791 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
792 }
793
794 /* Try to deduce D from N, P, Q, E. */
Hanno Becker8ba6ce42017-10-03 14:36:26 +0100795 TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
796 &E, &D ) == result );
Hanno Becker6b4ce492017-08-23 11:00:21 +0100797
798 if( !corrupt )
799 {
800 /*
801 * Check that D and Dp agree modulo LCM(P-1, Q-1).
802 */
803
804 /* Replace P,Q by P-1, Q-1 */
805 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
806 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
807
808 /* Check D == Dp modulo P-1 */
809 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
810 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
811 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
812
813 /* Check D == Dp modulo Q-1 */
814 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
815 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
816 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
817 }
818
819exit:
820
821 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
822 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
823 mbedtls_mpi_free( &E );
824 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
825}
826/* END_CASE */
827
Hanno Beckerf40cdf92017-12-22 11:03:27 +0000828/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Hanno Beckerc77ab892017-08-23 11:01:06 +0100829void mbedtls_rsa_import( int radix_N, char *input_N,
830 int radix_P, char *input_P,
831 int radix_Q, char *input_Q,
832 int radix_D, char *input_D,
833 int radix_E, char *input_E,
834 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +0100835 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +0100836 int res_check,
837 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100838{
839 mbedtls_mpi N, P, Q, D, E;
840 mbedtls_rsa_context ctx;
841
Hanno Beckere1582a82017-09-29 11:51:05 +0100842 /* Buffers used for encryption-decryption test */
843 unsigned char *buf_orig = NULL;
844 unsigned char *buf_enc = NULL;
845 unsigned char *buf_dec = NULL;
846
Hanno Beckerc77ab892017-08-23 11:01:06 +0100847 mbedtls_entropy_context entropy;
848 mbedtls_ctr_drbg_context ctr_drbg;
849 const char *pers = "test_suite_rsa";
850
Hanno Becker4d6e8342017-09-29 11:50:18 +0100851 const int have_N = ( strlen( input_N ) > 0 );
852 const int have_P = ( strlen( input_P ) > 0 );
853 const int have_Q = ( strlen( input_Q ) > 0 );
854 const int have_D = ( strlen( input_D ) > 0 );
855 const int have_E = ( strlen( input_E ) > 0 );
856
Hanno Beckerc77ab892017-08-23 11:01:06 +0100857 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100858 mbedtls_entropy_init( &entropy );
Ronald Cronc1905a12021-06-05 11:11:14 +0200859 mbedtls_rsa_init( &ctx );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100860
861 mbedtls_mpi_init( &N );
862 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
863 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
864
Hanno Beckerd4d60572018-01-10 07:12:01 +0000865 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
866 (const unsigned char *) pers, strlen( pers ) ) == 0 );
867
Hanno Becker4d6e8342017-09-29 11:50:18 +0100868 if( have_N )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100869 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
870
Hanno Becker4d6e8342017-09-29 11:50:18 +0100871 if( have_P )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100872 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
873
Hanno Becker4d6e8342017-09-29 11:50:18 +0100874 if( have_Q )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100875 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
876
Hanno Becker4d6e8342017-09-29 11:50:18 +0100877 if( have_D )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100878 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
879
Hanno Becker4d6e8342017-09-29 11:50:18 +0100880 if( have_E )
Hanno Beckerc77ab892017-08-23 11:01:06 +0100881 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
882
883 if( !successive )
884 {
885 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100886 have_N ? &N : NULL,
887 have_P ? &P : NULL,
888 have_Q ? &Q : NULL,
889 have_D ? &D : NULL,
890 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100891 }
892 else
893 {
894 /* Import N, P, Q, D, E separately.
895 * This should make no functional difference. */
896
897 TEST_ASSERT( mbedtls_rsa_import( &ctx,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100898 have_N ? &N : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100899 NULL, NULL, NULL, NULL ) == 0 );
900
901 TEST_ASSERT( mbedtls_rsa_import( &ctx,
902 NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100903 have_P ? &P : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100904 NULL, NULL, NULL ) == 0 );
905
906 TEST_ASSERT( mbedtls_rsa_import( &ctx,
907 NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100908 have_Q ? &Q : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100909 NULL, NULL ) == 0 );
910
911 TEST_ASSERT( mbedtls_rsa_import( &ctx,
912 NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100913 have_D ? &D : NULL,
Hanno Beckerc77ab892017-08-23 11:01:06 +0100914 NULL ) == 0 );
915
916 TEST_ASSERT( mbedtls_rsa_import( &ctx,
917 NULL, NULL, NULL, NULL,
Hanno Becker4d6e8342017-09-29 11:50:18 +0100918 have_E ? &E : NULL ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100919 }
920
Hanno Becker04877a42017-10-11 10:01:33 +0100921 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +0100922
Hanno Beckere1582a82017-09-29 11:51:05 +0100923 /* On expected success, perform some public and private
924 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +0100925 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +0100926 {
Hanno Beckere1582a82017-09-29 11:51:05 +0100927 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +0100928 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
929 else
930 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
931
932 if( res_check != 0 )
933 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +0100934
935 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
936 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
937 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
938 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
939 goto exit;
940
941 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
942 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
943
944 /* Make sure the number we're generating is smaller than the modulus */
945 buf_orig[0] = 0x00;
946
947 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
948
949 if( is_priv )
950 {
951 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
952 &ctr_drbg, buf_enc,
953 buf_dec ) == 0 );
954
955 TEST_ASSERT( memcmp( buf_orig, buf_dec,
956 mbedtls_rsa_get_len( &ctx ) ) == 0 );
957 }
958 }
959
Hanno Beckerc77ab892017-08-23 11:01:06 +0100960exit:
961
Hanno Beckere1582a82017-09-29 11:51:05 +0100962 mbedtls_free( buf_orig );
963 mbedtls_free( buf_enc );
964 mbedtls_free( buf_dec );
965
Hanno Beckerc77ab892017-08-23 11:01:06 +0100966 mbedtls_rsa_free( &ctx );
967
968 mbedtls_ctr_drbg_free( &ctr_drbg );
969 mbedtls_entropy_free( &entropy );
970
971 mbedtls_mpi_free( &N );
972 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
973 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
974}
975/* END_CASE */
976
Hanno Becker417f2d62017-08-23 11:44:51 +0100977/* BEGIN_CASE */
978void mbedtls_rsa_export( int radix_N, char *input_N,
979 int radix_P, char *input_P,
980 int radix_Q, char *input_Q,
981 int radix_D, char *input_D,
982 int radix_E, char *input_E,
Hanno Beckere1582a82017-09-29 11:51:05 +0100983 int is_priv,
Hanno Becker417f2d62017-08-23 11:44:51 +0100984 int successive )
985{
986 /* Original MPI's with which we set up the RSA context */
987 mbedtls_mpi N, P, Q, D, E;
988
989 /* Exported MPI's */
990 mbedtls_mpi Ne, Pe, Qe, De, Ee;
991
992 const int have_N = ( strlen( input_N ) > 0 );
993 const int have_P = ( strlen( input_P ) > 0 );
994 const int have_Q = ( strlen( input_Q ) > 0 );
995 const int have_D = ( strlen( input_D ) > 0 );
996 const int have_E = ( strlen( input_E ) > 0 );
997
Hanno Becker417f2d62017-08-23 11:44:51 +0100998 mbedtls_rsa_context ctx;
999
Ronald Cronc1905a12021-06-05 11:11:14 +02001000 mbedtls_rsa_init( &ctx );
Hanno Becker417f2d62017-08-23 11:44:51 +01001001
1002 mbedtls_mpi_init( &N );
1003 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1004 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1005
1006 mbedtls_mpi_init( &Ne );
1007 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
1008 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
1009
1010 /* Setup RSA context */
1011
1012 if( have_N )
1013 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1014
1015 if( have_P )
1016 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1017
1018 if( have_Q )
1019 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1020
1021 if( have_D )
1022 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1023
1024 if( have_E )
1025 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1026
1027 TEST_ASSERT( mbedtls_rsa_import( &ctx,
1028 strlen( input_N ) ? &N : NULL,
1029 strlen( input_P ) ? &P : NULL,
1030 strlen( input_Q ) ? &Q : NULL,
1031 strlen( input_D ) ? &D : NULL,
1032 strlen( input_E ) ? &E : NULL ) == 0 );
1033
Hanno Becker7f25f852017-10-10 16:56:22 +01001034 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001035
1036 /*
1037 * Export parameters and compare to original ones.
1038 */
1039
1040 /* N and E must always be present. */
1041 if( !successive )
1042 {
1043 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
1044 }
1045 else
1046 {
1047 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
1048 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
1049 }
1050 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
1051 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
1052
1053 /* If we were providing enough information to setup a complete private context,
1054 * we expect to be able to export all core parameters. */
1055
1056 if( is_priv )
1057 {
1058 if( !successive )
1059 {
1060 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
1061 &De, NULL ) == 0 );
1062 }
1063 else
1064 {
1065 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
1066 NULL, NULL ) == 0 );
1067 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1068 NULL, NULL ) == 0 );
1069 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1070 &De, NULL ) == 0 );
1071 }
1072
1073 if( have_P )
1074 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1075
1076 if( have_Q )
1077 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1078
1079 if( have_D )
1080 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1081
1082 /* While at it, perform a sanity check */
Hanno Becker750e8b42017-08-25 07:54:27 +01001083 TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
1084 NULL, NULL ) == 0 );
Hanno Becker417f2d62017-08-23 11:44:51 +01001085 }
1086
1087exit:
1088
1089 mbedtls_rsa_free( &ctx );
1090
1091 mbedtls_mpi_free( &N );
1092 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1093 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1094
1095 mbedtls_mpi_free( &Ne );
1096 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1097 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1098}
1099/* END_CASE */
1100
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001101/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Hanno Becker750e8b42017-08-25 07:54:27 +01001102void mbedtls_rsa_validate_params( int radix_N, char *input_N,
1103 int radix_P, char *input_P,
1104 int radix_Q, char *input_Q,
1105 int radix_D, char *input_D,
1106 int radix_E, char *input_E,
1107 int prng, int result )
Hanno Beckerce002632017-08-23 13:22:36 +01001108{
1109 /* Original MPI's with which we set up the RSA context */
1110 mbedtls_mpi N, P, Q, D, E;
1111
1112 const int have_N = ( strlen( input_N ) > 0 );
1113 const int have_P = ( strlen( input_P ) > 0 );
1114 const int have_Q = ( strlen( input_Q ) > 0 );
1115 const int have_D = ( strlen( input_D ) > 0 );
1116 const int have_E = ( strlen( input_E ) > 0 );
1117
1118 mbedtls_entropy_context entropy;
1119 mbedtls_ctr_drbg_context ctr_drbg;
1120 const char *pers = "test_suite_rsa";
1121
1122 mbedtls_mpi_init( &N );
1123 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1124 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1125
1126 mbedtls_ctr_drbg_init( &ctr_drbg );
1127 mbedtls_entropy_init( &entropy );
1128 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1129 &entropy, (const unsigned char *) pers,
1130 strlen( pers ) ) == 0 );
1131
1132 if( have_N )
1133 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1134
1135 if( have_P )
1136 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1137
1138 if( have_Q )
1139 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1140
1141 if( have_D )
1142 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1143
1144 if( have_E )
1145 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1146
Hanno Becker750e8b42017-08-25 07:54:27 +01001147 TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
1148 have_P ? &P : NULL,
1149 have_Q ? &Q : NULL,
1150 have_D ? &D : NULL,
1151 have_E ? &E : NULL,
1152 prng ? mbedtls_ctr_drbg_random : NULL,
1153 prng ? &ctr_drbg : NULL ) == result );
Hanno Beckerce002632017-08-23 13:22:36 +01001154exit:
1155
1156 mbedtls_ctr_drbg_free( &ctr_drbg );
1157 mbedtls_entropy_free( &entropy );
1158
1159 mbedtls_mpi_free( &N );
1160 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1161 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1162}
1163/* END_CASE */
1164
Hanno Beckerc77ab892017-08-23 11:01:06 +01001165/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Azim Khan5fcca462018-06-29 11:05:32 +01001166void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
1167 data_t *input_Q, data_t *input_D,
1168 data_t *input_E, int is_priv,
Hanno Beckere1582a82017-09-29 11:51:05 +01001169 int successive )
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001170{
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001171 /* Exported buffers */
Ron Eldorfdc15bd2018-11-22 15:47:51 +02001172 unsigned char bufNe[256];
1173 unsigned char bufPe[128];
1174 unsigned char bufQe[128];
1175 unsigned char bufDe[256];
1176 unsigned char bufEe[1];
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001177
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001178 mbedtls_rsa_context ctx;
1179
Ronald Cronc1905a12021-06-05 11:11:14 +02001180 mbedtls_rsa_init( &ctx );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001181
1182 /* Setup RSA context */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001183 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001184 input_N->len ? input_N->x : NULL, input_N->len,
1185 input_P->len ? input_P->x : NULL, input_P->len,
1186 input_Q->len ? input_Q->x : NULL, input_Q->len,
1187 input_D->len ? input_D->x : NULL, input_D->len,
1188 input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001189
Hanno Becker7f25f852017-10-10 16:56:22 +01001190 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001191
1192 /*
1193 * Export parameters and compare to original ones.
1194 */
1195
1196 /* N and E must always be present. */
1197 if( !successive )
1198 {
Azim Khand30ca132017-06-09 04:32:58 +01001199 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001200 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001201 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001202 }
1203 else
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,
1207 NULL, 0 ) == 0 );
1208 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1209 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001210 bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001211 }
Azim Khand30ca132017-06-09 04:32:58 +01001212 TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
1213 TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001214
1215 /* If we were providing enough information to setup a complete private context,
1216 * we expect to be able to export all core parameters. */
1217
1218 if( is_priv )
1219 {
1220 if( !successive )
1221 {
1222 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001223 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
1224 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
1225 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001226 NULL, 0 ) == 0 );
1227 }
1228 else
1229 {
1230 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001231 bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001232 NULL, 0, NULL, 0,
1233 NULL, 0 ) == 0 );
1234
1235 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001236 bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001237 NULL, 0, NULL, 0 ) == 0 );
1238
Azim Khand30ca132017-06-09 04:32:58 +01001239 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
1240 bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001241 NULL, 0 ) == 0 );
1242 }
1243
Azim Khand30ca132017-06-09 04:32:58 +01001244 if( input_P->len )
1245 TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001246
Azim Khand30ca132017-06-09 04:32:58 +01001247 if( input_Q->len )
1248 TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001249
Azim Khand30ca132017-06-09 04:32:58 +01001250 if( input_D->len )
1251 TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001252
1253 }
1254
1255exit:
1256 mbedtls_rsa_free( &ctx );
1257}
1258/* END_CASE */
1259
Hanno Beckerf40cdf92017-12-22 11:03:27 +00001260/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Azim Khan5fcca462018-06-29 11:05:32 +01001261void mbedtls_rsa_import_raw( data_t *input_N,
1262 data_t *input_P, data_t *input_Q,
1263 data_t *input_D, data_t *input_E,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001264 int successive,
Hanno Beckere1582a82017-09-29 11:51:05 +01001265 int is_priv,
Hanno Becker04877a42017-10-11 10:01:33 +01001266 int res_check,
1267 int res_complete )
Hanno Beckerc77ab892017-08-23 11:01:06 +01001268{
Hanno Beckere1582a82017-09-29 11:51:05 +01001269 /* Buffers used for encryption-decryption test */
1270 unsigned char *buf_orig = NULL;
1271 unsigned char *buf_enc = NULL;
1272 unsigned char *buf_dec = NULL;
1273
Hanno Beckerc77ab892017-08-23 11:01:06 +01001274 mbedtls_rsa_context ctx;
Hanno Beckerc77ab892017-08-23 11:01:06 +01001275 mbedtls_entropy_context entropy;
1276 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Becker3f3ae852017-10-02 10:08:39 +01001277
Hanno Beckerc77ab892017-08-23 11:01:06 +01001278 const char *pers = "test_suite_rsa";
1279
1280 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001281 mbedtls_entropy_init( &entropy );
Ronald Cronc1905a12021-06-05 11:11:14 +02001282 mbedtls_rsa_init( &ctx );
Hanno Becker3f3ae852017-10-02 10:08:39 +01001283
Hanno Beckerc77ab892017-08-23 11:01:06 +01001284 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1285 &entropy, (const unsigned char *) pers,
1286 strlen( pers ) ) == 0 );
1287
Hanno Beckerc77ab892017-08-23 11:01:06 +01001288 if( !successive )
1289 {
1290 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001291 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
1292 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
1293 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
1294 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
1295 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001296 }
1297 else
1298 {
1299 /* Import N, P, Q, D, E separately.
1300 * This should make no functional difference. */
1301
1302 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
Azim Khand30ca132017-06-09 04:32:58 +01001303 ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001304 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1305
1306 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1307 NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001308 ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001309 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1310
1311 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1312 NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001313 ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001314 NULL, 0, NULL, 0 ) == 0 );
1315
1316 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1317 NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001318 ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
Hanno Beckerc77ab892017-08-23 11:01:06 +01001319 NULL, 0 ) == 0 );
1320
1321 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1322 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
Azim Khand30ca132017-06-09 04:32:58 +01001323 ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001324 }
1325
Hanno Becker04877a42017-10-11 10:01:33 +01001326 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
Hanno Beckerc77ab892017-08-23 11:01:06 +01001327
Hanno Beckere1582a82017-09-29 11:51:05 +01001328 /* On expected success, perform some public and private
1329 * key operations to check if the key is working properly. */
Hanno Becker04877a42017-10-11 10:01:33 +01001330 if( res_complete == 0 )
Hanno Beckere1582a82017-09-29 11:51:05 +01001331 {
Hanno Beckere1582a82017-09-29 11:51:05 +01001332 if( is_priv )
Hanno Becker04877a42017-10-11 10:01:33 +01001333 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
1334 else
1335 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
1336
1337 if( res_check != 0 )
1338 goto exit;
Hanno Beckere1582a82017-09-29 11:51:05 +01001339
1340 buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1341 buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1342 buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
1343 if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
1344 goto exit;
1345
1346 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
1347 buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
1348
1349 /* Make sure the number we're generating is smaller than the modulus */
1350 buf_orig[0] = 0x00;
1351
1352 TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
1353
1354 if( is_priv )
1355 {
1356 TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
1357 &ctr_drbg, buf_enc,
1358 buf_dec ) == 0 );
1359
1360 TEST_ASSERT( memcmp( buf_orig, buf_dec,
1361 mbedtls_rsa_get_len( &ctx ) ) == 0 );
1362 }
1363 }
1364
Hanno Beckerc77ab892017-08-23 11:01:06 +01001365exit:
1366
Hanno Becker3f3ae852017-10-02 10:08:39 +01001367 mbedtls_free( buf_orig );
1368 mbedtls_free( buf_enc );
1369 mbedtls_free( buf_dec );
1370
Hanno Beckerc77ab892017-08-23 11:01:06 +01001371 mbedtls_rsa_free( &ctx );
1372
1373 mbedtls_ctr_drbg_free( &ctr_drbg );
1374 mbedtls_entropy_free( &entropy );
1375
1376}
1377/* END_CASE */
1378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +01001380void rsa_selftest( )
Paul Bakker42a29bf2009-07-07 20:18:41 +00001381{
Andres AG93012e82016-09-09 09:10:28 +01001382 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001383}
Paul Bakker33b43f12013-08-20 11:48:36 +02001384/* END_CASE */