blob: 0075f61d4e5892f160ab725cc208f2d2b66e1edb [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"
Paul Bakker33b43f12013-08-20 11:48:36 +020011/* END_HEADER */
Paul Bakker42a29bf2009-07-07 20:18:41 +000012
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014 * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
Paul Bakker33b43f12013-08-20 11:48:36 +020015 * END_DEPENDENCIES
16 */
Paul Bakker5690efc2011-05-26 13:16:06 +000017
Paul Bakker33b43f12013-08-20 11:48:36 +020018/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019void mbedtls_rsa_pkcs1_sign( char *message_hex_string, int padding_mode, int digest,
Paul Bakker33b43f12013-08-20 11:48:36 +020020 int mod, int radix_P, char *input_P, int radix_Q,
21 char *input_Q, int radix_N, char *input_N, int radix_E,
22 char *input_E, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000023{
24 unsigned char message_str[1000];
25 unsigned char hash_result[1000];
26 unsigned char output[1000];
27 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028 mbedtls_rsa_context ctx;
29 mbedtls_mpi P1, Q1, H, G;
Paul Bakker69998dd2009-07-11 19:15:20 +000030 int msg_len;
Paul Bakker548957d2013-08-30 10:30:02 +020031 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
34 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000035
36 memset( message_str, 0x00, 1000 );
37 memset( hash_result, 0x00, 1000 );
38 memset( output, 0x00, 1000 );
39 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +020040 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +000041
Paul Bakker33b43f12013-08-20 11:48:36 +020042 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
44 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
45 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
46 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
49 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
50 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
51 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
52 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
53 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
54 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
55 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000058
Paul Bakker33b43f12013-08-20 11:48:36 +020059 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 if( mbedtls_md_info_from_type( digest ) != NULL )
62 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, digest, 0, hash_result, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +020065 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +000066 {
67 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +000068
Paul Bakker33b43f12013-08-20 11:48:36 +020069 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +000070 }
Paul Bakker6c591fa2011-05-05 11:49:20 +000071
Paul Bakkerbd51b262014-07-10 15:26:12 +020072exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
74 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +000075}
Paul Bakker33b43f12013-08-20 11:48:36 +020076/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +000077
Paul Bakker33b43f12013-08-20 11:48:36 +020078/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079void mbedtls_rsa_pkcs1_verify( char *message_hex_string, int padding_mode, int digest,
Paul Bakker33b43f12013-08-20 11:48:36 +020080 int mod, int radix_N, char *input_N, int radix_E,
81 char *input_E, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +000082{
83 unsigned char message_str[1000];
84 unsigned char hash_result[1000];
85 unsigned char result_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_rsa_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000087 int msg_len;
Paul Bakker42a29bf2009-07-07 20:18:41 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000090 memset( message_str, 0x00, 1000 );
91 memset( hash_result, 0x00, 1000 );
92 memset( result_str, 0x00, 1000 );
93
Paul Bakker33b43f12013-08-20 11:48:36 +020094 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
96 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +000099
Paul Bakker33b43f12013-08-20 11:48:36 +0200100 msg_len = unhexify( message_str, message_hex_string );
101 unhexify( result_str, result_hex_str );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 if( mbedtls_md_info_from_type( digest ) != NULL )
104 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 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 +0100107
Paul Bakkerbd51b262014-07-10 15:26:12 +0200108exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000110}
Paul Bakker33b43f12013-08-20 11:48:36 +0200111/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000112
Paul Bakker821fb082009-07-12 13:26:42 +0000113
Paul Bakker33b43f12013-08-20 11:48:36 +0200114/* BEGIN_CASE */
115void rsa_pkcs1_sign_raw( char *message_hex_string, char *hash_result_string,
116 int padding_mode, int mod, int radix_P, char *input_P,
117 int radix_Q, char *input_Q, int radix_N,
118 char *input_N, int radix_E, char *input_E,
119 char *result_hex_str )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000120{
121 unsigned char message_str[1000];
122 unsigned char hash_result[1000];
123 unsigned char output[1000];
124 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_rsa_context ctx;
126 mbedtls_mpi P1, Q1, H, G;
Paul Bakkereaf90d92011-07-13 14:21:52 +0000127 int hash_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200128 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
131 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000132
Paul Bakker42a29bf2009-07-07 20:18:41 +0000133 memset( message_str, 0x00, 1000 );
134 memset( hash_result, 0x00, 1000 );
135 memset( output, 0x00, 1000 );
136 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200137 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000138
Paul Bakker33b43f12013-08-20 11:48:36 +0200139 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
141 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
142 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
143 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
146 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
147 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
148 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
149 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
150 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
151 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
152 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000155
Paul Bakker33b43f12013-08-20 11:48:36 +0200156 unhexify( message_str, message_hex_string );
157 hash_len = unhexify( hash_result, hash_result_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE, hash_len, hash_result, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000160
161 hexify( output_str, output, ctx.len );
162
Paul Bakker33b43f12013-08-20 11:48:36 +0200163 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000164
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100165 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100167 {
168 memset( output, 0x00, 1000 );
169 memset( output_str, 0x00, 1000 );
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 TEST_ASSERT( mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
172 &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100173 hash_len, hash_result, output ) == 0 );
174
175 hexify( output_str, output, ctx.len );
176
177 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
178 }
179
Paul Bakkerbd51b262014-07-10 15:26:12 +0200180exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
182 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000183}
Paul Bakker33b43f12013-08-20 11:48:36 +0200184/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000185
Paul Bakker33b43f12013-08-20 11:48:36 +0200186/* BEGIN_CASE */
187void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string,
188 int padding_mode, int mod, int radix_N,
189 char *input_N, int radix_E, char *input_E,
190 char *result_hex_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000191{
192 unsigned char message_str[1000];
193 unsigned char hash_result[1000];
194 unsigned char result_str[1000];
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100195 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_rsa_context ctx;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100197 size_t hash_len, olen;
Paul Bakker821fb082009-07-12 13:26:42 +0000198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000200 memset( message_str, 0x00, 1000 );
201 memset( hash_result, 0x00, 1000 );
202 memset( result_str, 0x00, 1000 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100203 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000204
Paul Bakker33b43f12013-08-20 11:48:36 +0200205 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
207 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000210
Paul Bakker33b43f12013-08-20 11:48:36 +0200211 unhexify( message_str, message_hex_string );
212 hash_len = unhexify( hash_result, hash_result_string );
213 unhexify( result_str, result_hex_str );
Paul Bakker821fb082009-07-12 13:26:42 +0000214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 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 +0100216
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100217 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100219 {
220 int ok;
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 TEST_ASSERT( mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
223 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100224 &olen, result_str, output, sizeof( output ) ) == 0 );
225
226 ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0;
227 if( correct == 0 )
228 TEST_ASSERT( ok == 1 );
229 else
230 TEST_ASSERT( ok == 0 );
231 }
232
Paul Bakkerbd51b262014-07-10 15:26:12 +0200233exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000235}
Paul Bakker33b43f12013-08-20 11:48:36 +0200236/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000237
Paul Bakker33b43f12013-08-20 11:48:36 +0200238/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239void mbedtls_rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200240 int radix_N, char *input_N, int radix_E, char *input_E,
241 char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000242{
243 unsigned char message_str[1000];
244 unsigned char output[1000];
245 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000247 size_t msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000248 rnd_pseudo_info rnd_info;
249
250 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000253 memset( message_str, 0x00, 1000 );
254 memset( output, 0x00, 1000 );
255 memset( output_str, 0x00, 1000 );
256
Paul Bakker33b43f12013-08-20 11:48:36 +0200257 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
259 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000262
Paul Bakker33b43f12013-08-20 11:48:36 +0200263 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PUBLIC, msg_len, message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200266 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000267 {
268 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000269
Paul Bakker33b43f12013-08-20 11:48:36 +0200270 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000271 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100272
Paul Bakkerbd51b262014-07-10 15:26:12 +0200273exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000275}
Paul Bakker33b43f12013-08-20 11:48:36 +0200276/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000277
Paul Bakker33b43f12013-08-20 11:48:36 +0200278/* BEGIN_CASE */
279void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode,
280 int mod, int radix_N, char *input_N,
281 int radix_E, char *input_E,
282 char *result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000283{
284 unsigned char message_str[1000];
285 unsigned char output[1000];
286 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000288 size_t msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000291 memset( message_str, 0x00, 1000 );
292 memset( output, 0x00, 1000 );
293 memset( output_str, 0x00, 1000 );
294
Paul Bakker33b43f12013-08-20 11:48:36 +0200295 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
297 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000300
Paul Bakker33b43f12013-08-20 11:48:36 +0200301 msg_len = unhexify( message_str, message_hex_string );
Paul Bakkera6656852010-07-18 19:47:14 +0000302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL, MBEDTLS_RSA_PUBLIC, msg_len, message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200304 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000305 {
306 hexify( output_str, output, ctx.len );
307
Paul Bakker33b43f12013-08-20 11:48:36 +0200308 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000309 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100310
Paul Bakkerbd51b262014-07-10 15:26:12 +0200311exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000313}
Paul Bakker33b43f12013-08-20 11:48:36 +0200314/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000315
Paul Bakker33b43f12013-08-20 11:48:36 +0200316/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317void mbedtls_rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200318 int radix_P, char *input_P, int radix_Q, char *input_Q,
319 int radix_N, char *input_N, int radix_E, char *input_E,
320 int max_output, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000321{
322 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000323 unsigned char output[1000];
324 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_rsa_context ctx;
326 mbedtls_mpi P1, Q1, H, G;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000327 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200328 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
331 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000332
333 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000334 memset( output, 0x00, 1000 );
335 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200336 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000337
Paul Bakker33b43f12013-08-20 11:48:36 +0200338 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
340 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
341 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
342 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
345 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
346 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
347 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
348 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
349 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
350 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
351 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000354
Paul Bakker33b43f12013-08-20 11:48:36 +0200355 unhexify( message_str, message_hex_string );
Paul Bakker69998dd2009-07-11 19:15:20 +0000356 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 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 +0200359 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000360 {
361 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000362
Paul Bakker33b43f12013-08-20 11:48:36 +0200363 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000364 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000365
Paul Bakkerbd51b262014-07-10 15:26:12 +0200366exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
368 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000369}
Paul Bakker33b43f12013-08-20 11:48:36 +0200370/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000371
Paul Bakker33b43f12013-08-20 11:48:36 +0200372/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373void mbedtls_rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N,
Paul Bakker33b43f12013-08-20 11:48:36 +0200374 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000375{
376 unsigned char message_str[1000];
377 unsigned char output[1000];
378 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
382 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000383 memset( message_str, 0x00, 1000 );
384 memset( output, 0x00, 1000 );
385 memset( output_str, 0x00, 1000 );
386
Paul Bakker33b43f12013-08-20 11:48:36 +0200387 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
389 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000392
Paul Bakker33b43f12013-08-20 11:48:36 +0200393 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200396 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000397 {
398 hexify( output_str, output, ctx.len );
399
Paul Bakker33b43f12013-08-20 11:48:36 +0200400 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000401 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100402
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100403 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200405 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100409
410 memset( output, 0x00, 1000 );
411 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100413 if( result == 0 )
414 {
415 hexify( output_str, output, ctx2.len );
416
417 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
418 }
419
Paul Bakkerbd51b262014-07-10 15:26:12 +0200420exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_rsa_free( &ctx );
422 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000423}
Paul Bakker33b43f12013-08-20 11:48:36 +0200424/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000425
Paul Bakker33b43f12013-08-20 11:48:36 +0200426/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427void mbedtls_rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
Paul Bakker33b43f12013-08-20 11:48:36 +0200428 int radix_Q, char *input_Q, int radix_N, char *input_N,
429 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000430{
431 unsigned char message_str[1000];
432 unsigned char output[1000];
433 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
435 mbedtls_mpi P1, Q1, H, G;
Paul Bakker548957d2013-08-30 10:30:02 +0200436 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200437 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
440 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
441 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000442
443 memset( message_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200444 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000445
Paul Bakker33b43f12013-08-20 11:48:36 +0200446 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
448 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
449 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
450 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
453 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
454 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
455 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
456 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
457 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
458 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
459 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000462
Paul Bakker33b43f12013-08-20 11:48:36 +0200463 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000464
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200465 /* repeat three times to test updating of blinding values */
466 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000467 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200468 memset( output, 0x00, 1000 );
469 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200471 message_str, output ) == result );
472 if( result == 0 )
473 {
474 hexify( output_str, output, ctx.len );
Paul Bakker821fb082009-07-12 13:26:42 +0000475
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200476 TEST_ASSERT( strcasecmp( (char *) output_str,
477 result_hex_str ) == 0 );
478 }
Paul Bakker821fb082009-07-12 13:26:42 +0000479 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000480
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100481 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200483 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100487
488 memset( output, 0x00, 1000 );
489 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100491 message_str, output ) == result );
492 if( result == 0 )
493 {
494 hexify( output_str, output, ctx2.len );
495
496 TEST_ASSERT( strcasecmp( (char *) output_str,
497 result_hex_str ) == 0 );
498 }
499
Paul Bakkerbd51b262014-07-10 15:26:12 +0200500exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
502 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000503}
Paul Bakker33b43f12013-08-20 11:48:36 +0200504/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000505
Paul Bakker33b43f12013-08-20 11:48:36 +0200506/* BEGIN_CASE */
507void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000508{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_rsa_context ctx;
510 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000513}
Paul Bakker33b43f12013-08-20 11:48:36 +0200514/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000515
Paul Bakker33b43f12013-08-20 11:48:36 +0200516/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517void mbedtls_rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
Paul Bakker33b43f12013-08-20 11:48:36 +0200518 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000523
Paul Bakker33b43f12013-08-20 11:48:36 +0200524 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000527 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200528 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000531 }
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100534
Paul Bakkerbd51b262014-07-10 15:26:12 +0200535exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000537}
Paul Bakker33b43f12013-08-20 11:48:36 +0200538/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000539
Paul Bakker33b43f12013-08-20 11:48:36 +0200540/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541void mbedtls_rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
Paul Bakker33b43f12013-08-20 11:48:36 +0200542 char *input_Q, int radix_N, char *input_N,
543 int radix_E, char *input_E, int radix_D, char *input_D,
544 int radix_DP, char *input_DP, int radix_DQ,
545 char *input_DQ, int radix_QP, char *input_QP,
546 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000547{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000551
Paul Bakker33b43f12013-08-20 11:48:36 +0200552 ctx.len = mod / 8;
553 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000556 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200557 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000560 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200561 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000564 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200565 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000568 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200569 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000572 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200573 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000576 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200577 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000580 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200581 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000584 }
Paul Bakker821fb082009-07-12 13:26:42 +0000585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100587
Paul Bakkerbd51b262014-07-10 15:26:12 +0200588exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000590}
Paul Bakker33b43f12013-08-20 11:48:36 +0200591/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000592
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100593/* BEGIN_CASE */
594void rsa_check_pubpriv( int mod, int radix_Npub, char *input_Npub,
595 int radix_Epub, char *input_Epub,
596 int radix_P, char *input_P, int radix_Q,
597 char *input_Q, int radix_N, char *input_N,
598 int radix_E, char *input_E, int radix_D, char *input_D,
599 int radix_DP, char *input_DP, int radix_DQ,
600 char *input_DQ, int radix_QP, char *input_QP,
601 int result )
602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
606 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100607
608 pub.len = mod / 8;
609 prv.len = mod / 8;
610
611 if( strlen( input_Npub ) )
612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100614 }
615 if( strlen( input_Epub ) )
616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100618 }
619
620 if( strlen( input_P ) )
621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100623 }
624 if( strlen( input_Q ) )
625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100627 }
628 if( strlen( input_N ) )
629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100631 }
632 if( strlen( input_E ) )
633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100635 }
636 if( strlen( input_D ) )
637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100639 }
640 if( strlen( input_DP ) )
641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100643 }
644 if( strlen( input_DQ ) )
645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100647 }
648 if( strlen( input_QP ) )
649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100651 }
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100654
655exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_rsa_free( &pub );
657 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100658}
659/* END_CASE */
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
662void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000663{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_rsa_context ctx;
665 mbedtls_entropy_context entropy;
666 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200667 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_entropy_init( &entropy );
670 TEST_ASSERT( mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200671 (const unsigned char *) pers, strlen( pers ) ) == 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200676 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000679 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100680
Paul Bakkerbd51b262014-07-10 15:26:12 +0200681exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_rsa_free( &ctx );
683 mbedtls_ctr_drbg_free( &ctr_drbg );
684 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000685}
Paul Bakker33b43f12013-08-20 11:48:36 +0200686/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200689void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +0000690{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 TEST_ASSERT( mbedtls_rsa_self_test( 0 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000692}
Paul Bakker33b43f12013-08-20 11:48:36 +0200693/* END_CASE */