blob: 09309d687e6770a7fd366ad70395eeffa6531a16 [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"
3#include "mbedtls/md2.h"
4#include "mbedtls/md4.h"
5#include "mbedtls/md5.h"
6#include "mbedtls/sha1.h"
7#include "mbedtls/sha256.h"
8#include "mbedtls/sha512.h"
9#include "mbedtls/entropy.h"
10#include "mbedtls/ctr_drbg.h"
Hanno Becker47deec42017-07-24 12:27:09 +010011
Paul Bakker33b43f12013-08-20 11:48:36 +020012/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +000013
Paul Bakker33b43f12013-08-20 11:48:36 +020014/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +020016 * END_DEPENDENCIES
17 */
Paul Bakker5690efc2011-05-26 13:16:06 +000018
Paul Bakker33b43f12013-08-20 11:48:36 +020019/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020void mbedtls_rsa_pkcs1_sign( char *message_hex_string, int padding_mode, int digest,
Paul Bakker33b43f12013-08-20 11:48:36 +020021 int mod, int radix_P, char *input_P, int radix_Q,
22 char *input_Q, int radix_N, char *input_N, int radix_E,
23 char *input_E, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000024{
25 unsigned char message_str[1000];
26 unsigned char hash_result[1000];
27 unsigned char output[1000];
28 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 mbedtls_rsa_context ctx;
30 mbedtls_mpi P1, Q1, H, G;
Paul Bakker69998dd2009-07-11 19:15:20 +000031 int msg_len;
Paul Bakker548957d2013-08-30 10:30:02 +020032 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
35 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000036
37 memset( message_str, 0x00, 1000 );
38 memset( hash_result, 0x00, 1000 );
39 memset( output, 0x00, 1000 );
40 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +020041 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +000042
Paul Bakker33b43f12013-08-20 11:48:36 +020043 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
45 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
46 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
47 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
50 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
51 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
52 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
53 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
54 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
55 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
56 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000059
Paul Bakker33b43f12013-08-20 11:48:36 +020060 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 if( mbedtls_md_info_from_type( digest ) != NULL )
Hanno Beckerf8b56d42017-10-05 10:16:37 +010063 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ),
64 message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000065
Hanno Beckerf8b56d42017-10-05 10:16:37 +010066 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
67 MBEDTLS_RSA_PRIVATE, digest, 0,
68 hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020069 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +000070 {
71 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +000072
Paul Bakker33b43f12013-08-20 11:48:36 +020073 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +000074 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000075
Paul Bakkerbd51b262014-07-10 15:26:12 +020076exit:
Hanno Beckerf8b56d42017-10-05 10:16:37 +010077 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
78 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +000080}
Paul Bakker33b43f12013-08-20 11:48:36 +020081/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +000082
Paul Bakker33b43f12013-08-20 11:48:36 +020083/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_rsa_pkcs1_verify( char *message_hex_string, int padding_mode, int digest,
Paul Bakker33b43f12013-08-20 11:48:36 +020085 int mod, int radix_N, char *input_N, int radix_E,
86 char *input_E, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000087{
88 unsigned char message_str[1000];
89 unsigned char hash_result[1000];
90 unsigned char result_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000092 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000095 memset( message_str, 0x00, 1000 );
96 memset( hash_result, 0x00, 1000 );
97 memset( result_str, 0x00, 1000 );
98
Paul Bakker33b43f12013-08-20 11:48:36 +020099 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
101 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000104
Paul Bakker33b43f12013-08-20 11:48:36 +0200105 msg_len = unhexify( message_str, message_hex_string );
106 unhexify( result_str, result_hex_str );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 if( mbedtls_md_info_from_type( digest ) != NULL )
109 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100112
Paul Bakkerbd51b262014-07-10 15:26:12 +0200113exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000115}
Paul Bakker33b43f12013-08-20 11:48:36 +0200116/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000117
Paul Bakker821fb082009-07-12 13:26:42 +0000118
Paul Bakker33b43f12013-08-20 11:48:36 +0200119/* BEGIN_CASE */
120void rsa_pkcs1_sign_raw( char *message_hex_string, char *hash_result_string,
121 int padding_mode, int mod, int radix_P, char *input_P,
122 int radix_Q, char *input_Q, int radix_N,
123 char *input_N, int radix_E, char *input_E,
124 char *result_hex_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000125{
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100126 int res;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000127 unsigned char message_str[1000];
128 unsigned char hash_result[1000];
129 unsigned char output[1000];
130 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_rsa_context ctx;
132 mbedtls_mpi P1, Q1, H, G;
Paul Bakkereaf90d92011-07-13 14:21:52 +0000133 int hash_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200134 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
137 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000138
Paul Bakker42a29bf2009-07-07 20:18:41 +0000139 memset( message_str, 0x00, 1000 );
140 memset( hash_result, 0x00, 1000 );
141 memset( output, 0x00, 1000 );
142 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200143 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000144
Paul Bakker33b43f12013-08-20 11:48:36 +0200145 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
147 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
148 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
149 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
152 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
153 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
154 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
155 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
156 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
157 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
158 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000161
Paul Bakker33b43f12013-08-20 11:48:36 +0200162 unhexify( message_str, message_hex_string );
163 hash_len = unhexify( hash_result, hash_result_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000164
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100165 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
166 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
167 hash_len, hash_result, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000168
169 hexify( output_str, output, ctx.len );
170
Paul Bakker33b43f12013-08-20 11:48:36 +0200171 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000172
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100173 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100175 {
176 memset( output, 0x00, 1000 );
177 memset( output_str, 0x00, 1000 );
178
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100179 res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100181 hash_len, hash_result, output );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100182
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100183#if !defined(MBEDTLS_RSA_ALT)
184 TEST_ASSERT( res == 0 );
185#else
186 TEST_ASSERT( ( res == 0 ) ||
187 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
188#endif
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100189
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100190 if( res == 0 )
191 {
192 hexify( output_str, output, ctx.len );
193 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
194 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100195 }
196
Paul Bakkerbd51b262014-07-10 15:26:12 +0200197exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
199 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000200}
Paul Bakker33b43f12013-08-20 11:48:36 +0200201/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000202
Paul Bakker33b43f12013-08-20 11:48:36 +0200203/* BEGIN_CASE */
204void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string,
205 int padding_mode, int mod, int radix_N,
206 char *input_N, int radix_E, char *input_E,
207 char *result_hex_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000208{
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100209 int res;
Paul Bakker821fb082009-07-12 13:26:42 +0000210 unsigned char message_str[1000];
211 unsigned char hash_result[1000];
212 unsigned char result_str[1000];
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100213 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_rsa_context ctx;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100215 size_t hash_len, olen;
Paul Bakker821fb082009-07-12 13:26:42 +0000216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000218 memset( message_str, 0x00, 1000 );
219 memset( hash_result, 0x00, 1000 );
220 memset( result_str, 0x00, 1000 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100221 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000222
Paul Bakker33b43f12013-08-20 11:48:36 +0200223 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
225 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000228
Paul Bakker33b43f12013-08-20 11:48:36 +0200229 unhexify( message_str, message_hex_string );
230 hash_len = unhexify( hash_result, hash_result_string );
231 unhexify( result_str, result_hex_str );
Paul Bakker821fb082009-07-12 13:26:42 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE, hash_len, hash_result, result_str ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100234
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100235 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100237 {
238 int ok;
239
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100240 res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100242 &olen, result_str, output, sizeof( output ) );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100243
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100244#if !defined(MBEDTLS_RSA_ALT)
245 TEST_ASSERT( res == 0 );
246#else
247 TEST_ASSERT( ( res == 0 ) ||
248 ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
249#endif
250
251 if( res == 0 )
252 {
253 ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0;
254 if( correct == 0 )
255 TEST_ASSERT( ok == 1 );
256 else
257 TEST_ASSERT( ok == 0 );
258 }
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100259 }
260
Paul Bakkerbd51b262014-07-10 15:26:12 +0200261exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000263}
Paul Bakker33b43f12013-08-20 11:48:36 +0200264/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000265
Paul Bakker33b43f12013-08-20 11:48:36 +0200266/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267void mbedtls_rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200268 int radix_N, char *input_N, int radix_E, char *input_E,
269 char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000270{
271 unsigned char message_str[1000];
272 unsigned char output[1000];
273 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000275 size_t msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000276 rnd_pseudo_info rnd_info;
277
278 memset( &rnd_info, 0, sizeof( 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 );
Paul Bakker821fb082009-07-12 13:26:42 +0000281 memset( message_str, 0x00, 1000 );
282 memset( output, 0x00, 1000 );
283 memset( output_str, 0x00, 1000 );
284
Paul Bakker33b43f12013-08-20 11:48:36 +0200285 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
287 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000290
Paul Bakker33b43f12013-08-20 11:48:36 +0200291 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000292
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100293 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
294 MBEDTLS_RSA_PUBLIC, msg_len,
295 message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200296 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000297 {
298 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000299
Paul Bakker33b43f12013-08-20 11:48:36 +0200300 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 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:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000305}
Paul Bakker33b43f12013-08-20 11:48:36 +0200306/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000307
Paul Bakker33b43f12013-08-20 11:48:36 +0200308/* BEGIN_CASE */
309void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode,
310 int mod, int radix_N, char *input_N,
311 int radix_E, char *input_E,
312 char *result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000313{
314 unsigned char message_str[1000];
315 unsigned char output[1000];
316 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000318 size_t msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000321 memset( message_str, 0x00, 1000 );
322 memset( output, 0x00, 1000 );
323 memset( output_str, 0x00, 1000 );
324
Paul Bakker33b43f12013-08-20 11:48:36 +0200325 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
327 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000328
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 Bakker33b43f12013-08-20 11:48:36 +0200331 msg_len = unhexify( message_str, message_hex_string );
Paul Bakkera6656852010-07-18 19:47:14 +0000332
Hanno Beckerf8b56d42017-10-05 10:16:37 +0100333 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL,
334 MBEDTLS_RSA_PUBLIC, msg_len,
335 message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200336 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000337 {
338 hexify( output_str, output, ctx.len );
339
Paul Bakker33b43f12013-08-20 11:48:36 +0200340 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 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:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000345}
Paul Bakker33b43f12013-08-20 11:48:36 +0200346/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000347
Paul Bakker33b43f12013-08-20 11:48:36 +0200348/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349void mbedtls_rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200350 int radix_P, char *input_P, int radix_Q, char *input_Q,
351 int radix_N, char *input_N, int radix_E, char *input_E,
352 int max_output, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000353{
354 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000355 unsigned char output[1000];
356 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_rsa_context ctx;
358 mbedtls_mpi P1, Q1, H, G;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000359 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200360 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
363 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000364
365 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000366 memset( output, 0x00, 1000 );
367 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200368 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000369
Paul Bakker33b43f12013-08-20 11:48:36 +0200370 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
372 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
373 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
374 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
377 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
378 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
379 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
380 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
381 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
382 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
383 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000386
Paul Bakker33b43f12013-08-20 11:48:36 +0200387 unhexify( message_str, message_hex_string );
Paul Bakker69998dd2009-07-11 19:15:20 +0000388 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str, output, max_output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200391 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000392 {
393 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000394
Paul Bakker33b43f12013-08-20 11:48:36 +0200395 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000396 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000397
Paul Bakkerbd51b262014-07-10 15:26:12 +0200398exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
400 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000401}
Paul Bakker33b43f12013-08-20 11:48:36 +0200402/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000403
Paul Bakker33b43f12013-08-20 11:48:36 +0200404/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405void mbedtls_rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N,
Paul Bakker33b43f12013-08-20 11:48:36 +0200406 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000407{
408 unsigned char message_str[1000];
409 unsigned char output[1000];
410 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
414 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000415 memset( message_str, 0x00, 1000 );
416 memset( output, 0x00, 1000 );
417 memset( output_str, 0x00, 1000 );
418
Paul Bakker33b43f12013-08-20 11:48:36 +0200419 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
421 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000422
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 Bakker33b43f12013-08-20 11:48:36 +0200425 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200428 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000429 {
430 hexify( output_str, output, ctx.len );
431
Paul Bakker33b43f12013-08-20 11:48:36 +0200432 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000433 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100434
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100435 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200437 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100441
442 memset( output, 0x00, 1000 );
443 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100445 if( result == 0 )
446 {
447 hexify( output_str, output, ctx2.len );
448
449 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
450 }
451
Paul Bakkerbd51b262014-07-10 15:26:12 +0200452exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_rsa_free( &ctx );
454 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000455}
Paul Bakker33b43f12013-08-20 11:48:36 +0200456/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000457
Paul Bakker33b43f12013-08-20 11:48:36 +0200458/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459void mbedtls_rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
Paul Bakker33b43f12013-08-20 11:48:36 +0200460 int radix_Q, char *input_Q, int radix_N, char *input_N,
461 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000462{
463 unsigned char message_str[1000];
464 unsigned char output[1000];
465 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
467 mbedtls_mpi P1, Q1, H, G;
Paul Bakker548957d2013-08-30 10:30:02 +0200468 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200469 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
472 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
475 memset( message_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200476 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000477
Paul Bakker33b43f12013-08-20 11:48:36 +0200478 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
480 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
481 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
482 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
485 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
486 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
487 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
488 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
489 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
490 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
491 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000494
Paul Bakker33b43f12013-08-20 11:48:36 +0200495 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000496
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200497 /* repeat three times to test updating of blinding values */
498 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000499 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200500 memset( output, 0x00, 1000 );
501 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200503 message_str, output ) == result );
504 if( result == 0 )
505 {
506 hexify( output_str, output, ctx.len );
Paul Bakker821fb082009-07-12 13:26:42 +0000507
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200508 TEST_ASSERT( strcasecmp( (char *) output_str,
509 result_hex_str ) == 0 );
510 }
Paul Bakker821fb082009-07-12 13:26:42 +0000511 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000512
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100513 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200515 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100519
520 memset( output, 0x00, 1000 );
521 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100523 message_str, output ) == result );
524 if( result == 0 )
525 {
526 hexify( output_str, output, ctx2.len );
527
528 TEST_ASSERT( strcasecmp( (char *) output_str,
529 result_hex_str ) == 0 );
530 }
531
Paul Bakkerbd51b262014-07-10 15:26:12 +0200532exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
534 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000535}
Paul Bakker33b43f12013-08-20 11:48:36 +0200536/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000537
Paul Bakker33b43f12013-08-20 11:48:36 +0200538/* BEGIN_CASE */
539void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000540{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_rsa_context ctx;
542 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000545}
Paul Bakker33b43f12013-08-20 11:48:36 +0200546/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000547
Paul Bakker33b43f12013-08-20 11:48:36 +0200548/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549void mbedtls_rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
Paul Bakker33b43f12013-08-20 11:48:36 +0200550 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000555
Paul Bakker33b43f12013-08-20 11:48:36 +0200556 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000559 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200560 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000563 }
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100566
Paul Bakkerbd51b262014-07-10 15:26:12 +0200567exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000569}
Paul Bakker33b43f12013-08-20 11:48:36 +0200570/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000571
Paul Bakker33b43f12013-08-20 11:48:36 +0200572/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573void mbedtls_rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
Paul Bakker33b43f12013-08-20 11:48:36 +0200574 char *input_Q, int radix_N, char *input_N,
575 int radix_E, char *input_E, int radix_D, char *input_D,
576 int radix_DP, char *input_DP, int radix_DQ,
577 char *input_DQ, int radix_QP, char *input_QP,
578 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000579{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000583
Paul Bakker33b43f12013-08-20 11:48:36 +0200584 ctx.len = mod / 8;
585 if( strlen( input_P ) )
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.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000588 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200589 if( strlen( input_Q ) )
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.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000592 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200593 if( strlen( input_N ) )
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.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000596 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200597 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000600 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200601 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000604 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200605 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000608 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200609 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000612 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200613 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000616 }
Paul Bakker821fb082009-07-12 13:26:42 +0000617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100619
Paul Bakkerbd51b262014-07-10 15:26:12 +0200620exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000622}
Paul Bakker33b43f12013-08-20 11:48:36 +0200623/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000624
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100625/* BEGIN_CASE */
626void rsa_check_pubpriv( int mod, int radix_Npub, char *input_Npub,
627 int radix_Epub, char *input_Epub,
628 int radix_P, char *input_P, int radix_Q,
629 char *input_Q, int radix_N, char *input_N,
630 int radix_E, char *input_E, int radix_D, char *input_D,
631 int radix_DP, char *input_DP, int radix_DQ,
632 char *input_DQ, int radix_QP, char *input_QP,
633 int result )
634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
638 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100639
640 pub.len = mod / 8;
641 prv.len = mod / 8;
642
643 if( strlen( input_Npub ) )
644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100646 }
647 if( strlen( input_Epub ) )
648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100650 }
651
652 if( strlen( input_P ) )
653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100655 }
656 if( strlen( input_Q ) )
657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100659 }
660 if( strlen( input_N ) )
661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100663 }
664 if( strlen( input_E ) )
665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100667 }
668 if( strlen( input_D ) )
669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100671 }
672 if( strlen( input_DP ) )
673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100675 }
676 if( strlen( input_DQ ) )
677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100679 }
680 if( strlen( input_QP ) )
681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100683 }
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100686
687exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_rsa_free( &pub );
689 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100690}
691/* END_CASE */
692
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100693/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000695{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_rsa_context ctx;
697 mbedtls_entropy_context entropy;
698 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200699 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000700
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200701 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_entropy_init( &entropy );
Hanno Becker7e8e57c2017-07-23 10:19:29 +0100703 mbedtls_rsa_init ( &ctx, 0, 0 );
704
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200705 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200706 (const unsigned char *) pers, strlen( pers ) ) == 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200709 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100712 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000713 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100714
Paul Bakkerbd51b262014-07-10 15:26:12 +0200715exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_rsa_free( &ctx );
717 mbedtls_ctr_drbg_free( &ctr_drbg );
718 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000719}
Paul Bakker33b43f12013-08-20 11:48:36 +0200720/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200723void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +0000724{
Andres AG93012e82016-09-09 09:10:28 +0100725 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000726}
Paul Bakker33b43f12013-08-20 11:48:36 +0200727/* END_CASE */