blob: 1f3c3b3395baa441e7818f9404d40dbbf0b2ef9f [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
Hanno Becker8fd55482017-08-23 14:07:48 +0100159 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
160 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
161 hash_len, hash_result, output ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000162
163 hexify( output_str, output, ctx.len );
164
Paul Bakker33b43f12013-08-20 11:48:36 +0200165 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000166
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100167 /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100169 {
170 memset( output, 0x00, 1000 );
171 memset( output_str, 0x00, 1000 );
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 TEST_ASSERT( mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
174 &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100175 hash_len, hash_result, output ) == 0 );
176
177 hexify( output_str, output, ctx.len );
178
179 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
180 }
181
Paul Bakkerbd51b262014-07-10 15:26:12 +0200182exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
184 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000185}
Paul Bakker33b43f12013-08-20 11:48:36 +0200186/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000187
Paul Bakker33b43f12013-08-20 11:48:36 +0200188/* BEGIN_CASE */
189void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string,
190 int padding_mode, int mod, int radix_N,
191 char *input_N, int radix_E, char *input_E,
192 char *result_hex_str, int correct )
Paul Bakker821fb082009-07-12 13:26:42 +0000193{
194 unsigned char message_str[1000];
195 unsigned char hash_result[1000];
196 unsigned char result_str[1000];
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100197 unsigned char output[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_rsa_context ctx;
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100199 size_t hash_len, olen;
Paul Bakker821fb082009-07-12 13:26:42 +0000200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000202 memset( message_str, 0x00, 1000 );
203 memset( hash_result, 0x00, 1000 );
204 memset( result_str, 0x00, 1000 );
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100205 memset( output, 0x00, sizeof( output ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000206
Paul Bakker33b43f12013-08-20 11:48:36 +0200207 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
209 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000212
Paul Bakker33b43f12013-08-20 11:48:36 +0200213 unhexify( message_str, message_hex_string );
214 hash_len = unhexify( hash_result, hash_result_string );
215 unhexify( result_str, result_hex_str );
Paul Bakker821fb082009-07-12 13:26:42 +0000216
Hanno Becker8fd55482017-08-23 14:07:48 +0100217 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
218 MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE,
219 hash_len, hash_result,
220 result_str ) == correct );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100221
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100222 /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100224 {
225 int ok;
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 TEST_ASSERT( mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
228 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100229 &olen, result_str, output, sizeof( output ) ) == 0 );
230
231 ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0;
232 if( correct == 0 )
233 TEST_ASSERT( ok == 1 );
234 else
235 TEST_ASSERT( ok == 0 );
236 }
237
Paul Bakkerbd51b262014-07-10 15:26:12 +0200238exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000240}
Paul Bakker33b43f12013-08-20 11:48:36 +0200241/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000242
Paul Bakker33b43f12013-08-20 11:48:36 +0200243/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244void mbedtls_rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200245 int radix_N, char *input_N, int radix_E, char *input_E,
246 char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000247{
248 unsigned char message_str[1000];
249 unsigned char output[1000];
250 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000252 size_t msg_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000253 rnd_pseudo_info rnd_info;
254
255 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000258 memset( message_str, 0x00, 1000 );
259 memset( output, 0x00, 1000 );
260 memset( output_str, 0x00, 1000 );
261
Paul Bakker33b43f12013-08-20 11:48:36 +0200262 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
264 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000267
Paul Bakker33b43f12013-08-20 11:48:36 +0200268 msg_len = unhexify( message_str, message_hex_string );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 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 +0200271 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000272 {
273 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000274
Paul Bakker33b43f12013-08-20 11:48:36 +0200275 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000276 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100277
Paul Bakkerbd51b262014-07-10 15:26:12 +0200278exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_rsa_free( &ctx );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000280}
Paul Bakker33b43f12013-08-20 11:48:36 +0200281/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000282
Paul Bakker33b43f12013-08-20 11:48:36 +0200283/* BEGIN_CASE */
284void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode,
285 int mod, int radix_N, char *input_N,
286 int radix_E, char *input_E,
287 char *result_hex_str, int result )
Paul Bakkera6656852010-07-18 19:47:14 +0000288{
289 unsigned char message_str[1000];
290 unsigned char output[1000];
291 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_rsa_context ctx;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000293 size_t msg_len;
Paul Bakkera6656852010-07-18 19:47:14 +0000294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000296 memset( message_str, 0x00, 1000 );
297 memset( output, 0x00, 1000 );
298 memset( output_str, 0x00, 1000 );
299
Paul Bakker33b43f12013-08-20 11:48:36 +0200300 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
302 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000305
Paul Bakker33b43f12013-08-20 11:48:36 +0200306 msg_len = unhexify( message_str, message_hex_string );
Paul Bakkera6656852010-07-18 19:47:14 +0000307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 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 +0200309 if( result == 0 )
Paul Bakkera6656852010-07-18 19:47:14 +0000310 {
311 hexify( output_str, output, ctx.len );
312
Paul Bakker33b43f12013-08-20 11:48:36 +0200313 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakkera6656852010-07-18 19:47:14 +0000314 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100315
Paul Bakkerbd51b262014-07-10 15:26:12 +0200316exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_rsa_free( &ctx );
Paul Bakkera6656852010-07-18 19:47:14 +0000318}
Paul Bakker33b43f12013-08-20 11:48:36 +0200319/* END_CASE */
Paul Bakkera6656852010-07-18 19:47:14 +0000320
Paul Bakker33b43f12013-08-20 11:48:36 +0200321/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322void mbedtls_rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod,
Paul Bakker33b43f12013-08-20 11:48:36 +0200323 int radix_P, char *input_P, int radix_Q, char *input_Q,
324 int radix_N, char *input_N, int radix_E, char *input_E,
325 int max_output, char *result_hex_str, int result )
Paul Bakker42a29bf2009-07-07 20:18:41 +0000326{
327 unsigned char message_str[1000];
Paul Bakker42a29bf2009-07-07 20:18:41 +0000328 unsigned char output[1000];
329 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_rsa_context ctx;
331 mbedtls_mpi P1, Q1, H, G;
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000332 size_t output_len;
Paul Bakker548957d2013-08-30 10:30:02 +0200333 rnd_pseudo_info rnd_info;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
336 mbedtls_rsa_init( &ctx, padding_mode, 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000337
338 memset( message_str, 0x00, 1000 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000339 memset( output, 0x00, 1000 );
340 memset( output_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200341 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000342
Paul Bakker33b43f12013-08-20 11:48:36 +0200343 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
345 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
346 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
347 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
350 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
351 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
352 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
353 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
354 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
355 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
356 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000359
Paul Bakker33b43f12013-08-20 11:48:36 +0200360 unhexify( message_str, message_hex_string );
Paul Bakker69998dd2009-07-11 19:15:20 +0000361 output_len = 0;
Paul Bakker42a29bf2009-07-07 20:18:41 +0000362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 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 +0200364 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000365 {
366 hexify( output_str, output, ctx.len );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000367
Paul Bakker33b43f12013-08-20 11:48:36 +0200368 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000369 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000370
Paul Bakkerbd51b262014-07-10 15:26:12 +0200371exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
373 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000374}
Paul Bakker33b43f12013-08-20 11:48:36 +0200375/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000376
Paul Bakker33b43f12013-08-20 11:48:36 +0200377/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378void mbedtls_rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N,
Paul Bakker33b43f12013-08-20 11:48:36 +0200379 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000380{
381 unsigned char message_str[1000];
382 unsigned char output[1000];
383 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
Paul Bakker821fb082009-07-12 13:26:42 +0000385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
387 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000388 memset( message_str, 0x00, 1000 );
389 memset( output, 0x00, 1000 );
390 memset( output_str, 0x00, 1000 );
391
Paul Bakker33b43f12013-08-20 11:48:36 +0200392 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
394 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000397
Paul Bakker33b43f12013-08-20 11:48:36 +0200398 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str, output ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200401 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000402 {
403 hexify( output_str, output, ctx.len );
404
Paul Bakker33b43f12013-08-20 11:48:36 +0200405 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000406 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100407
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100408 /* And now with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200410 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100414
415 memset( output, 0x00, 1000 );
416 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str, output ) == result );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100418 if( result == 0 )
419 {
420 hexify( output_str, output, ctx2.len );
421
422 TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 );
423 }
424
Paul Bakkerbd51b262014-07-10 15:26:12 +0200425exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_rsa_free( &ctx );
427 mbedtls_rsa_free( &ctx2 );
Paul Bakker821fb082009-07-12 13:26:42 +0000428}
Paul Bakker33b43f12013-08-20 11:48:36 +0200429/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000430
Paul Bakker33b43f12013-08-20 11:48:36 +0200431/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432void mbedtls_rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P,
Paul Bakker33b43f12013-08-20 11:48:36 +0200433 int radix_Q, char *input_Q, int radix_N, char *input_N,
434 int radix_E, char *input_E, char *result_hex_str, int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000435{
436 unsigned char message_str[1000];
437 unsigned char output[1000];
438 unsigned char output_str[1000];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
440 mbedtls_mpi P1, Q1, H, G;
Paul Bakker548957d2013-08-30 10:30:02 +0200441 rnd_pseudo_info rnd_info;
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200442 int i;
Paul Bakker821fb082009-07-12 13:26:42 +0000443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
445 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
446 mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000447
448 memset( message_str, 0x00, 1000 );
Paul Bakker548957d2013-08-30 10:30:02 +0200449 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
Paul Bakker821fb082009-07-12 13:26:42 +0000450
Paul Bakker33b43f12013-08-20 11:48:36 +0200451 ctx.len = mod / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
453 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
454 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
455 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
458 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
459 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
460 TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 );
461 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
462 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
463 TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
464 TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000467
Paul Bakker33b43f12013-08-20 11:48:36 +0200468 unhexify( message_str, message_hex_string );
Paul Bakker821fb082009-07-12 13:26:42 +0000469
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200470 /* repeat three times to test updating of blinding values */
471 for( i = 0; i < 3; i++ )
Paul Bakker821fb082009-07-12 13:26:42 +0000472 {
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200473 memset( output, 0x00, 1000 );
474 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200476 message_str, output ) == result );
477 if( result == 0 )
478 {
479 hexify( output_str, output, ctx.len );
Paul Bakker821fb082009-07-12 13:26:42 +0000480
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200481 TEST_ASSERT( strcasecmp( (char *) output_str,
482 result_hex_str ) == 0 );
483 }
Paul Bakker821fb082009-07-12 13:26:42 +0000484 }
Paul Bakker6c591fa2011-05-05 11:49:20 +0000485
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100486 /* And now one more time with the copy */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200488 /* clear the original to be sure */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_rsa_free( &ctx );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100492
493 memset( output, 0x00, 1000 );
494 memset( output_str, 0x00, 1000 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
Manuel Pégourié-Gonnardc4919bc2014-02-03 11:16:44 +0100496 message_str, output ) == result );
497 if( result == 0 )
498 {
499 hexify( output_str, output, ctx2.len );
500
501 TEST_ASSERT( strcasecmp( (char *) output_str,
502 result_hex_str ) == 0 );
503 }
504
Paul Bakkerbd51b262014-07-10 15:26:12 +0200505exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
507 mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
Paul Bakker42a29bf2009-07-07 20:18:41 +0000508}
Paul Bakker33b43f12013-08-20 11:48:36 +0200509/* END_CASE */
Paul Bakker42a29bf2009-07-07 20:18:41 +0000510
Paul Bakker33b43f12013-08-20 11:48:36 +0200511/* BEGIN_CASE */
512void rsa_check_privkey_null()
Paul Bakker37940d9f2009-07-10 22:38:58 +0000513{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 mbedtls_rsa_context ctx;
515 memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000518}
Paul Bakker33b43f12013-08-20 11:48:36 +0200519/* END_CASE */
Paul Bakker37940d9f2009-07-10 22:38:58 +0000520
Paul Bakker33b43f12013-08-20 11:48:36 +0200521/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522void mbedtls_rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E,
Paul Bakker33b43f12013-08-20 11:48:36 +0200523 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000524{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000528
Paul Bakker33b43f12013-08-20 11:48:36 +0200529 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000532 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200533 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000536 }
537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100539
Paul Bakkerbd51b262014-07-10 15:26:12 +0200540exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000542}
Paul Bakker33b43f12013-08-20 11:48:36 +0200543/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000544
Paul Bakker33b43f12013-08-20 11:48:36 +0200545/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546void mbedtls_rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q,
Paul Bakker33b43f12013-08-20 11:48:36 +0200547 char *input_Q, int radix_N, char *input_N,
548 int radix_E, char *input_E, int radix_D, char *input_D,
549 int radix_DP, char *input_DP, int radix_DQ,
550 char *input_DQ, int radix_QP, char *input_QP,
551 int result )
Paul Bakker821fb082009-07-12 13:26:42 +0000552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_rsa_context ctx;
Paul Bakker821fb082009-07-12 13:26:42 +0000554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000556
Paul Bakker33b43f12013-08-20 11:48:36 +0200557 ctx.len = mod / 8;
558 if( strlen( input_P ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000561 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200562 if( strlen( input_Q ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000565 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200566 if( strlen( input_N ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000569 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200570 if( strlen( input_E ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000573 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200574 if( strlen( input_D ) )
Paul Bakker821fb082009-07-12 13:26:42 +0000575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000577 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200578 if( strlen( input_DP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000581 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200582 if( strlen( input_DQ ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000585 }
Paul Bakker33b43f12013-08-20 11:48:36 +0200586 if( strlen( input_QP ) )
Paul Bakker31417a72012-09-27 20:41:37 +0000587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
Paul Bakker31417a72012-09-27 20:41:37 +0000589 }
Paul Bakker821fb082009-07-12 13:26:42 +0000590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100592
Paul Bakkerbd51b262014-07-10 15:26:12 +0200593exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_rsa_free( &ctx );
Paul Bakker821fb082009-07-12 13:26:42 +0000595}
Paul Bakker33b43f12013-08-20 11:48:36 +0200596/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000597
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100598/* BEGIN_CASE */
599void rsa_check_pubpriv( int mod, int radix_Npub, char *input_Npub,
600 int radix_Epub, char *input_Epub,
601 int radix_P, char *input_P, int radix_Q,
602 char *input_Q, int radix_N, char *input_N,
603 int radix_E, char *input_E, int radix_D, char *input_D,
604 int radix_DP, char *input_DP, int radix_DQ,
605 char *input_DQ, int radix_QP, char *input_QP,
606 int result )
607{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_rsa_context pub, prv;
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
611 mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100612
613 pub.len = mod / 8;
614 prv.len = mod / 8;
615
616 if( strlen( input_Npub ) )
617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100619 }
620 if( strlen( input_Epub ) )
621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100623 }
624
625 if( strlen( input_P ) )
626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100628 }
629 if( strlen( input_Q ) )
630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100632 }
633 if( strlen( input_N ) )
634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100636 }
637 if( strlen( input_E ) )
638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100640 }
641 if( strlen( input_D ) )
642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100644 }
645 if( strlen( input_DP ) )
646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100648 }
649 if( strlen( input_DQ ) )
650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100652 }
653 if( strlen( input_QP ) )
654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100656 }
657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100659
660exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_rsa_free( &pub );
662 mbedtls_rsa_free( &prv );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100663}
664/* END_CASE */
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
667void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
Paul Bakker821fb082009-07-12 13:26:42 +0000668{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_rsa_context ctx;
670 mbedtls_entropy_context entropy;
671 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200672 const char *pers = "test_suite_rsa";
Paul Bakker821fb082009-07-12 13:26:42 +0000673
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200674 mbedtls_ctr_drbg_init( &ctr_drbg );
675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200677 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200678 (const unsigned char *) pers, strlen( pers ) ) == 0 );
Paul Bakkerc0a1a312011-12-04 17:12:15 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_rsa_init( &ctx, 0, 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200683 if( result == 0 )
Paul Bakker821fb082009-07-12 13:26:42 +0000684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
Janos Follathef441782016-09-21 13:18:12 +0100686 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
Paul Bakker821fb082009-07-12 13:26:42 +0000687 }
Paul Bakker58ef6ec2013-01-03 11:33:48 +0100688
Paul Bakkerbd51b262014-07-10 15:26:12 +0200689exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_rsa_free( &ctx );
691 mbedtls_ctr_drbg_free( &ctr_drbg );
692 mbedtls_entropy_free( &entropy );
Paul Bakker821fb082009-07-12 13:26:42 +0000693}
Paul Bakker33b43f12013-08-20 11:48:36 +0200694/* END_CASE */
Paul Bakker821fb082009-07-12 13:26:42 +0000695
Hanno Beckere78fd8d2017-08-23 11:00:44 +0100696/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
697void mbedtls_rsa_deduce_moduli( int radix_N, char *input_N,
698 int radix_D, char *input_D,
699 int radix_E, char *input_E,
700 int radix_P, char *output_P,
701 int radix_Q, char *output_Q,
702 int corrupt, int result )
703{
704 mbedtls_mpi N, P, Pp, Q, Qp, D, E;
705
706 mbedtls_entropy_context entropy;
707 mbedtls_ctr_drbg_context ctr_drbg;
708 const char *pers = "test_suite_rsa";
709
710 mbedtls_mpi_init( &N );
711 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
712 mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
713 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
714
715 mbedtls_ctr_drbg_init( &ctr_drbg );
716 mbedtls_entropy_init( &entropy );
717 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
718 (const unsigned char *) pers, strlen( pers ) ) == 0 );
719
720 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
721 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
722 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
723 TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
724 TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
725
726 if( corrupt )
727 TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
728
729 /* Try to deduce P, Q from N, D, E only. */
730 TEST_ASSERT( mbedtls_rsa_deduce_moduli( &N, &D, &E, mbedtls_ctr_drbg_random,
731 &ctr_drbg, &P, &Q ) == result );
732
733 if( !corrupt )
734 {
735 /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
736 TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
737 ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
738 }
739
740exit:
741
742 mbedtls_mpi_free( &N );
743 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
744 mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
745 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
746
747 mbedtls_ctr_drbg_free( &ctr_drbg );
748 mbedtls_entropy_free( &entropy );
749}
750/* END_CASE */
751
Hanno Becker6b4ce492017-08-23 11:00:21 +0100752/* BEGIN_CASE */
753void mbedtls_rsa_deduce_private( int radix_P, char *input_P,
754 int radix_Q, char *input_Q,
755 int radix_E, char *input_E,
756 int radix_D, char *output_D,
757 int corrupt, int result )
758{
759 mbedtls_mpi P, Q, D, Dp, E, R, Rp;
760
761 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
762 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
763 mbedtls_mpi_init( &E );
764 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
765
766 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
767 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
768 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
769 TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
770
771 if( corrupt )
772 {
773 /* Make E even */
774 TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
775 }
776
777 /* Try to deduce D from N, P, Q, E. */
778 TEST_ASSERT( mbedtls_rsa_deduce_private( &P, &Q, &D, &E ) == result );
779
780 if( !corrupt )
781 {
782 /*
783 * Check that D and Dp agree modulo LCM(P-1, Q-1).
784 */
785
786 /* Replace P,Q by P-1, Q-1 */
787 TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
788 TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
789
790 /* Check D == Dp modulo P-1 */
791 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
792 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
793 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
794
795 /* Check D == Dp modulo Q-1 */
796 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
797 TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
798 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
799 }
800
801exit:
802
803 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
804 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
805 mbedtls_mpi_free( &E );
806 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
807}
808/* END_CASE */
809
Hanno Beckerc77ab892017-08-23 11:01:06 +0100810/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
811void mbedtls_rsa_import( int radix_N, char *input_N,
812 int radix_P, char *input_P,
813 int radix_Q, char *input_Q,
814 int radix_D, char *input_D,
815 int radix_E, char *input_E,
816 int successive,
817 int result )
818{
819 mbedtls_mpi N, P, Q, D, E;
820 mbedtls_rsa_context ctx;
821
822 mbedtls_entropy_context entropy;
823 mbedtls_ctr_drbg_context ctr_drbg;
824 const char *pers = "test_suite_rsa";
825
826 mbedtls_ctr_drbg_init( &ctr_drbg );
827
828 mbedtls_entropy_init( &entropy );
829 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
830 (const unsigned char *) pers, strlen( pers ) ) == 0 );
831
832 mbedtls_rsa_init( &ctx, 0, 0 );
833
834 mbedtls_mpi_init( &N );
835 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
836 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
837
838 if( strlen( input_N ) )
839 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
840
841 if( strlen( input_P ) )
842 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
843
844 if( strlen( input_Q ) )
845 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
846
847 if( strlen( input_D ) )
848 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
849
850 if( strlen( input_E ) )
851 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
852
853 if( !successive )
854 {
855 TEST_ASSERT( mbedtls_rsa_import( &ctx,
856 strlen( input_N ) ? &N : NULL,
857 strlen( input_P ) ? &P : NULL,
858 strlen( input_Q ) ? &Q : NULL,
859 strlen( input_D ) ? &D : NULL,
860 strlen( input_E ) ? &E : NULL ) == 0 );
861 }
862 else
863 {
864 /* Import N, P, Q, D, E separately.
865 * This should make no functional difference. */
866
867 TEST_ASSERT( mbedtls_rsa_import( &ctx,
868 strlen( input_N ) ? &N : NULL,
869 NULL, NULL, NULL, NULL ) == 0 );
870
871 TEST_ASSERT( mbedtls_rsa_import( &ctx,
872 NULL,
873 strlen( input_P ) ? &P : NULL,
874 NULL, NULL, NULL ) == 0 );
875
876 TEST_ASSERT( mbedtls_rsa_import( &ctx,
877 NULL, NULL,
878 strlen( input_Q ) ? &Q : NULL,
879 NULL, NULL ) == 0 );
880
881 TEST_ASSERT( mbedtls_rsa_import( &ctx,
882 NULL, NULL, NULL,
883 strlen( input_D ) ? &D : NULL,
884 NULL ) == 0 );
885
886 TEST_ASSERT( mbedtls_rsa_import( &ctx,
887 NULL, NULL, NULL, NULL,
888 strlen( input_E ) ? &E : NULL ) == 0 );
889 }
890
891 TEST_ASSERT( mbedtls_rsa_complete( &ctx,
892 mbedtls_ctr_drbg_random,
893 &ctr_drbg ) == result );
894
895exit:
896
897 mbedtls_rsa_free( &ctx );
898
899 mbedtls_ctr_drbg_free( &ctr_drbg );
900 mbedtls_entropy_free( &entropy );
901
902 mbedtls_mpi_free( &N );
903 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
904 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
905}
906/* END_CASE */
907
Hanno Becker417f2d62017-08-23 11:44:51 +0100908/* BEGIN_CASE */
909void mbedtls_rsa_export( int radix_N, char *input_N,
910 int radix_P, char *input_P,
911 int radix_Q, char *input_Q,
912 int radix_D, char *input_D,
913 int radix_E, char *input_E,
914 int successive )
915{
916 /* Original MPI's with which we set up the RSA context */
917 mbedtls_mpi N, P, Q, D, E;
918
919 /* Exported MPI's */
920 mbedtls_mpi Ne, Pe, Qe, De, Ee;
921
922 const int have_N = ( strlen( input_N ) > 0 );
923 const int have_P = ( strlen( input_P ) > 0 );
924 const int have_Q = ( strlen( input_Q ) > 0 );
925 const int have_D = ( strlen( input_D ) > 0 );
926 const int have_E = ( strlen( input_E ) > 0 );
927
928 const int is_priv = have_P || have_Q || have_D;
929
930 mbedtls_rsa_context ctx;
931
932 mbedtls_rsa_init( &ctx, 0, 0 );
933
934 mbedtls_mpi_init( &N );
935 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
936 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
937
938 mbedtls_mpi_init( &Ne );
939 mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
940 mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
941
942 /* Setup RSA context */
943
944 if( have_N )
945 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
946
947 if( have_P )
948 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
949
950 if( have_Q )
951 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
952
953 if( have_D )
954 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
955
956 if( have_E )
957 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
958
959 TEST_ASSERT( mbedtls_rsa_import( &ctx,
960 strlen( input_N ) ? &N : NULL,
961 strlen( input_P ) ? &P : NULL,
962 strlen( input_Q ) ? &Q : NULL,
963 strlen( input_D ) ? &D : NULL,
964 strlen( input_E ) ? &E : NULL ) == 0 );
965
966 TEST_ASSERT( mbedtls_rsa_complete( &ctx, NULL, NULL ) == 0 );
967
968 /*
969 * Export parameters and compare to original ones.
970 */
971
972 /* N and E must always be present. */
973 if( !successive )
974 {
975 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
976 }
977 else
978 {
979 TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
980 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
981 }
982 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
983 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
984
985 /* If we were providing enough information to setup a complete private context,
986 * we expect to be able to export all core parameters. */
987
988 if( is_priv )
989 {
990 if( !successive )
991 {
992 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
993 &De, NULL ) == 0 );
994 }
995 else
996 {
997 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
998 NULL, NULL ) == 0 );
999 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
1000 NULL, NULL ) == 0 );
1001 TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
1002 &De, NULL ) == 0 );
1003 }
1004
1005 if( have_P )
1006 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
1007
1008 if( have_Q )
1009 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
1010
1011 if( have_D )
1012 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
1013
1014 /* While at it, perform a sanity check */
1015 TEST_ASSERT( mbedtls_rsa_check_params( &Ne, &Pe, &Qe, &De, &Ee,
1016 NULL, NULL ) == 0 );
1017 }
1018
1019exit:
1020
1021 mbedtls_rsa_free( &ctx );
1022
1023 mbedtls_mpi_free( &N );
1024 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
1025 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
1026
1027 mbedtls_mpi_free( &Ne );
1028 mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
1029 mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
1030}
1031/* END_CASE */
1032
Hanno Beckerce002632017-08-23 13:22:36 +01001033/* BEGIN_CASE */
1034void mbedtls_rsa_check_params( int radix_N, char *input_N,
1035 int radix_P, char *input_P,
1036 int radix_Q, char *input_Q,
1037 int radix_D, char *input_D,
1038 int radix_E, char *input_E,
1039 int prng, int result )
1040{
1041 /* Original MPI's with which we set up the RSA context */
1042 mbedtls_mpi N, P, Q, D, E;
1043
1044 const int have_N = ( strlen( input_N ) > 0 );
1045 const int have_P = ( strlen( input_P ) > 0 );
1046 const int have_Q = ( strlen( input_Q ) > 0 );
1047 const int have_D = ( strlen( input_D ) > 0 );
1048 const int have_E = ( strlen( input_E ) > 0 );
1049
1050 mbedtls_entropy_context entropy;
1051 mbedtls_ctr_drbg_context ctr_drbg;
1052 const char *pers = "test_suite_rsa";
1053
1054 mbedtls_mpi_init( &N );
1055 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
1056 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
1057
1058 mbedtls_ctr_drbg_init( &ctr_drbg );
1059 mbedtls_entropy_init( &entropy );
1060 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1061 &entropy, (const unsigned char *) pers,
1062 strlen( pers ) ) == 0 );
1063
1064 if( have_N )
1065 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
1066
1067 if( have_P )
1068 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
1069
1070 if( have_Q )
1071 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
1072
1073 if( have_D )
1074 TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
1075
1076 if( have_E )
1077 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
1078
1079 TEST_ASSERT( mbedtls_rsa_check_params( have_N ? &N : NULL,
1080 have_P ? &P : NULL,
1081 have_Q ? &Q : NULL,
1082 have_D ? &D : NULL,
1083 have_E ? &E : NULL,
1084 prng ? mbedtls_ctr_drbg_random : NULL,
1085 prng ? &ctr_drbg : NULL ) == result );
1086exit:
1087
1088 mbedtls_ctr_drbg_free( &ctr_drbg );
1089 mbedtls_entropy_free( &entropy );
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/* END_CASE */
1096
Hanno Beckerc77ab892017-08-23 11:01:06 +01001097/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Beckerf1b9a2c2017-08-23 11:49:22 +01001098void mbedtls_rsa_export_raw( char *input_N, char *input_P,
1099 char *input_Q, char *input_D,
1100 char *input_E, int successive )
1101{
1102 /* Original raw buffers with which we set up the RSA context */
1103 unsigned char bufN[1000];
1104 unsigned char bufP[1000];
1105 unsigned char bufQ[1000];
1106 unsigned char bufD[1000];
1107 unsigned char bufE[1000];
1108
1109 size_t lenN = 0;
1110 size_t lenP = 0;
1111 size_t lenQ = 0;
1112 size_t lenD = 0;
1113 size_t lenE = 0;
1114
1115 /* Exported buffers */
1116 unsigned char bufNe[ sizeof( bufN ) ];
1117 unsigned char bufPe[ sizeof( bufP ) ];
1118 unsigned char bufQe[ sizeof( bufQ ) ];
1119 unsigned char bufDe[ sizeof( bufD ) ];
1120 unsigned char bufEe[ sizeof( bufE ) ];
1121
1122 const int have_N = ( strlen( input_N ) > 0 );
1123 const int have_P = ( strlen( input_P ) > 0 );
1124 const int have_Q = ( strlen( input_Q ) > 0 );
1125 const int have_D = ( strlen( input_D ) > 0 );
1126 const int have_E = ( strlen( input_E ) > 0 );
1127
1128 const int is_priv = have_P || have_Q || have_D;
1129
1130 mbedtls_rsa_context ctx;
1131
1132 mbedtls_rsa_init( &ctx, 0, 0 );
1133
1134 /* Setup RSA context */
1135
1136 if( have_N )
1137 lenN = unhexify( bufN, input_N );
1138
1139 if( have_P )
1140 lenP = unhexify( bufP, input_P );
1141
1142 if( have_Q )
1143 lenQ = unhexify( bufQ, input_Q );
1144
1145 if( have_D )
1146 lenD = unhexify( bufD, input_D );
1147
1148 if( have_E )
1149 lenE = unhexify( bufE, input_E );
1150
1151 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1152 have_N ? bufN : NULL, lenN,
1153 have_P ? bufP : NULL, lenP,
1154 have_Q ? bufQ : NULL, lenQ,
1155 have_D ? bufD : NULL, lenD,
1156 have_E ? bufE : NULL, lenE ) == 0 );
1157
1158 TEST_ASSERT( mbedtls_rsa_complete( &ctx, NULL, NULL ) == 0 );
1159
1160 /*
1161 * Export parameters and compare to original ones.
1162 */
1163
1164 /* N and E must always be present. */
1165 if( !successive )
1166 {
1167 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, lenN,
1168 NULL, 0, NULL, 0, NULL, 0,
1169 bufEe, lenE ) == 0 );
1170 }
1171 else
1172 {
1173 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, lenN,
1174 NULL, 0, NULL, 0, NULL, 0,
1175 NULL, 0 ) == 0 );
1176 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1177 NULL, 0, NULL, 0, NULL, 0,
1178 bufEe, lenE ) == 0 );
1179 }
1180 TEST_ASSERT( memcmp( bufN, bufNe, lenN ) == 0 );
1181 TEST_ASSERT( memcmp( bufE, bufEe, lenE ) == 0 );
1182
1183 /* If we were providing enough information to setup a complete private context,
1184 * we expect to be able to export all core parameters. */
1185
1186 if( is_priv )
1187 {
1188 if( !successive )
1189 {
1190 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1191 bufPe, lenP ? lenP : sizeof( bufPe ),
1192 bufQe, lenQ ? lenQ : sizeof( bufQe ),
1193 bufDe, lenD ? lenD : sizeof( bufDe ),
1194 NULL, 0 ) == 0 );
1195 }
1196 else
1197 {
1198 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
1199 bufPe, lenP ? lenP : sizeof( bufPe ),
1200 NULL, 0, NULL, 0,
1201 NULL, 0 ) == 0 );
1202
1203 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
1204 bufQe, lenQ ? lenQ : sizeof( bufQe ),
1205 NULL, 0, NULL, 0 ) == 0 );
1206
1207 TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
1208 NULL, 0, bufDe, lenD ? lenD : sizeof( bufDe ),
1209 NULL, 0 ) == 0 );
1210 }
1211
1212 if( have_P )
1213 TEST_ASSERT( memcmp( bufP, bufPe, lenP ) == 0 );
1214
1215 if( have_Q )
1216 TEST_ASSERT( memcmp( bufQ, bufQe, lenQ ) == 0 );
1217
1218 if( have_D )
1219 TEST_ASSERT( memcmp( bufD, bufDe, lenD ) == 0 );
1220
1221 }
1222
1223exit:
1224 mbedtls_rsa_free( &ctx );
1225}
1226/* END_CASE */
1227
1228/* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
Hanno Beckerc77ab892017-08-23 11:01:06 +01001229void mbedtls_rsa_import_raw( char *input_N,
1230 char *input_P, char *input_Q,
1231 char *input_D, char *input_E,
1232 int successive,
1233 int result )
1234{
1235 unsigned char bufN[1000];
1236 unsigned char bufP[1000];
1237 unsigned char bufQ[1000];
1238 unsigned char bufD[1000];
1239 unsigned char bufE[1000];
1240
1241 size_t lenN = 0;
1242 size_t lenP = 0;
1243 size_t lenQ = 0;
1244 size_t lenD = 0;
1245 size_t lenE = 0;
1246
1247 mbedtls_rsa_context ctx;
1248
1249 mbedtls_entropy_context entropy;
1250 mbedtls_ctr_drbg_context ctr_drbg;
1251 const char *pers = "test_suite_rsa";
1252
1253 mbedtls_ctr_drbg_init( &ctr_drbg );
1254
1255 mbedtls_entropy_init( &entropy );
1256 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
1257 &entropy, (const unsigned char *) pers,
1258 strlen( pers ) ) == 0 );
1259
1260 mbedtls_rsa_init( &ctx, 0, 0 );
1261
1262 if( strlen( input_N ) )
1263 lenN = unhexify( bufN, input_N );
1264
1265 if( strlen( input_P ) )
1266 lenP = unhexify( bufP, input_P );
1267
1268 if( strlen( input_Q ) )
1269 lenQ = unhexify( bufQ, input_Q );
1270
1271 if( strlen( input_D ) )
1272 lenD = unhexify( bufD, input_D );
1273
1274 if( strlen( input_E ) )
1275 lenE = unhexify( bufE, input_E );
1276
1277 if( !successive )
1278 {
1279 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1280 ( lenN > 0 ) ? bufN : NULL, lenN,
1281 ( lenP > 0 ) ? bufP : NULL, lenP,
1282 ( lenQ > 0 ) ? bufQ : NULL, lenQ,
1283 ( lenD > 0 ) ? bufD : NULL, lenD,
1284 ( lenE > 0 ) ? bufE : NULL, lenE ) == 0 );
1285 }
1286 else
1287 {
1288 /* Import N, P, Q, D, E separately.
1289 * This should make no functional difference. */
1290
1291 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1292 ( lenN > 0 ) ? bufN : NULL, lenN,
1293 NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1294
1295 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1296 NULL, 0,
1297 ( lenP > 0 ) ? bufP : NULL, lenP,
1298 NULL, 0, NULL, 0, NULL, 0 ) == 0 );
1299
1300 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1301 NULL, 0, NULL, 0,
1302 ( lenQ > 0 ) ? bufQ : NULL, lenQ,
1303 NULL, 0, NULL, 0 ) == 0 );
1304
1305 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1306 NULL, 0, NULL, 0, NULL, 0,
1307 ( lenD > 0 ) ? bufD : NULL, lenD,
1308 NULL, 0 ) == 0 );
1309
1310 TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
1311 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1312 ( lenE > 0 ) ? bufE : NULL, lenE ) == 0 );
1313 }
1314
1315 TEST_ASSERT( mbedtls_rsa_complete( &ctx,
1316 mbedtls_ctr_drbg_random,
1317 &ctr_drbg ) == result );
1318
1319exit:
1320
1321 mbedtls_rsa_free( &ctx );
1322
1323 mbedtls_ctr_drbg_free( &ctr_drbg );
1324 mbedtls_entropy_free( &entropy );
1325
1326}
1327/* END_CASE */
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +02001330void rsa_selftest()
Paul Bakker42a29bf2009-07-07 20:18:41 +00001331{
Andres AG93012e82016-09-09 09:10:28 +01001332 TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
Paul Bakker42a29bf2009-07-07 20:18:41 +00001333}
Paul Bakker33b43f12013-08-20 11:48:36 +02001334/* END_CASE */