Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 1 | BEGIN_HEADER |
| 2 | #include <polarssl/rsa.h> |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 3 | #include <polarssl/md2.h> |
| 4 | #include <polarssl/md4.h> |
| 5 | #include <polarssl/md5.h> |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 6 | #include <polarssl/sha1.h> |
| 7 | #include <polarssl/sha2.h> |
| 8 | #include <polarssl/sha4.h> |
Paul Bakker | c0a1a31 | 2011-12-04 17:12:15 +0000 | [diff] [blame] | 9 | #include <polarssl/entropy.h> |
| 10 | #include <polarssl/ctr_drbg.h> |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 11 | END_HEADER |
| 12 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 13 | BEGIN_DEPENDENCIES |
Paul Bakker | d7d8dbe | 2011-05-26 15:29:38 +0000 | [diff] [blame] | 14 | depends_on:POLARSSL_RSA_C:POLARSSL_BIGNUM_C:POLARSSL_GENPRIME |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 15 | END_DEPENDENCIES |
| 16 | |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 17 | BEGIN_CASE |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 18 | rsa_pkcs1_sign:message_hex_string:padding_mode:digest:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str:result |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 19 | { |
| 20 | unsigned char message_str[1000]; |
| 21 | unsigned char hash_result[1000]; |
| 22 | unsigned char output[1000]; |
| 23 | unsigned char output_str[1000]; |
| 24 | rsa_context ctx; |
| 25 | mpi P1, Q1, H, G; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 26 | int msg_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 27 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 28 | mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G ); |
Paul Bakker | ebcef6d | 2010-08-16 11:10:49 +0000 | [diff] [blame] | 29 | rsa_init( &ctx, {padding_mode}, 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 30 | |
| 31 | memset( message_str, 0x00, 1000 ); |
| 32 | memset( hash_result, 0x00, 1000 ); |
| 33 | memset( output, 0x00, 1000 ); |
| 34 | memset( output_str, 0x00, 1000 ); |
| 35 | |
| 36 | ctx.len = {mod} / 8; |
| 37 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 38 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 39 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 40 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 41 | |
| 42 | TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
| 43 | TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
| 44 | TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
| 45 | TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
| 46 | TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
| 47 | TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
| 48 | TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
| 49 | TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
| 50 | |
| 51 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 52 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 53 | msg_len = unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 54 | |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame^] | 55 | if( md_info_from_type( {digest} ) != NULL ) |
| 56 | TEST_ASSERT( md( md_info_from_type( {digest} ), message_str, msg_len, hash_result ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 57 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 58 | TEST_ASSERT( rsa_pkcs1_sign( &ctx, NULL, NULL, RSA_PRIVATE, {digest}, 0, hash_result, output ) == {result} ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 59 | if( {result} == 0 ) |
| 60 | { |
| 61 | hexify( output_str, output, ctx.len ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 62 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 63 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 64 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 65 | |
| 66 | mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 67 | rsa_free( &ctx ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 68 | } |
| 69 | END_CASE |
| 70 | |
| 71 | BEGIN_CASE |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 72 | rsa_pkcs1_verify:message_hex_string:padding_mode:digest:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 73 | { |
| 74 | unsigned char message_str[1000]; |
| 75 | unsigned char hash_result[1000]; |
| 76 | unsigned char result_str[1000]; |
| 77 | rsa_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 78 | int msg_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 79 | |
Paul Bakker | ebcef6d | 2010-08-16 11:10:49 +0000 | [diff] [blame] | 80 | rsa_init( &ctx, {padding_mode}, 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 81 | memset( message_str, 0x00, 1000 ); |
| 82 | memset( hash_result, 0x00, 1000 ); |
| 83 | memset( result_str, 0x00, 1000 ); |
| 84 | |
| 85 | ctx.len = {mod} / 8; |
| 86 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 87 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 88 | |
| 89 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 ); |
| 90 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 91 | msg_len = unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 92 | unhexify( result_str, {result_hex_str} ); |
| 93 | |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame^] | 94 | if( md_info_from_type( {digest} ) != NULL ) |
| 95 | TEST_ASSERT( md( md_info_from_type( {digest} ), message_str, msg_len, hash_result ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 96 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 97 | TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, {digest}, 0, hash_result, result_str ) == {result} ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 98 | |
| 99 | rsa_free( &ctx ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 100 | } |
| 101 | END_CASE |
| 102 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 103 | |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 104 | BEGIN_CASE |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 105 | rsa_pkcs1_sign_raw:message_hex_string:hash_result_string:padding_mode:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 106 | { |
| 107 | unsigned char message_str[1000]; |
| 108 | unsigned char hash_result[1000]; |
| 109 | unsigned char output[1000]; |
| 110 | unsigned char output_str[1000]; |
| 111 | rsa_context ctx; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 112 | mpi P1, Q1, H, G; |
Paul Bakker | eaf90d9 | 2011-07-13 14:21:52 +0000 | [diff] [blame] | 113 | int hash_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 114 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 115 | mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G ); |
Paul Bakker | ebcef6d | 2010-08-16 11:10:49 +0000 | [diff] [blame] | 116 | rsa_init( &ctx, {padding_mode}, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 117 | |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 118 | memset( message_str, 0x00, 1000 ); |
| 119 | memset( hash_result, 0x00, 1000 ); |
| 120 | memset( output, 0x00, 1000 ); |
| 121 | memset( output_str, 0x00, 1000 ); |
| 122 | |
| 123 | ctx.len = {mod} / 8; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 124 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 125 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 126 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 127 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 128 | |
| 129 | TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
| 130 | TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
| 131 | TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
| 132 | TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
| 133 | TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
| 134 | TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
| 135 | TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
| 136 | TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
| 137 | |
| 138 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 139 | |
Paul Bakker | eaf90d9 | 2011-07-13 14:21:52 +0000 | [diff] [blame] | 140 | unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 141 | hash_len = unhexify( hash_result, {hash_result_string} ); |
| 142 | |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame^] | 143 | TEST_ASSERT( rsa_pkcs1_sign( &ctx, NULL, NULL, RSA_PRIVATE, POLARSSL_MD_NONE, hash_len, hash_result, output ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 144 | |
| 145 | hexify( output_str, output, ctx.len ); |
| 146 | |
| 147 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 148 | |
| 149 | mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 150 | rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 151 | } |
| 152 | END_CASE |
| 153 | |
| 154 | BEGIN_CASE |
| 155 | rsa_pkcs1_verify_raw:message_hex_string:hash_result_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:correct |
| 156 | { |
| 157 | unsigned char message_str[1000]; |
| 158 | unsigned char hash_result[1000]; |
| 159 | unsigned char result_str[1000]; |
| 160 | rsa_context ctx; |
Paul Bakker | eaf90d9 | 2011-07-13 14:21:52 +0000 | [diff] [blame] | 161 | size_t hash_len; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 162 | |
Paul Bakker | ebcef6d | 2010-08-16 11:10:49 +0000 | [diff] [blame] | 163 | rsa_init( &ctx, {padding_mode}, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 164 | memset( message_str, 0x00, 1000 ); |
| 165 | memset( hash_result, 0x00, 1000 ); |
| 166 | memset( result_str, 0x00, 1000 ); |
| 167 | |
| 168 | ctx.len = {mod} / 8; |
| 169 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 170 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 171 | |
| 172 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 ); |
| 173 | |
Paul Bakker | eaf90d9 | 2011-07-13 14:21:52 +0000 | [diff] [blame] | 174 | unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 175 | hash_len = unhexify( hash_result, {hash_result_string} ); |
| 176 | unhexify( result_str, {result_hex_str} ); |
| 177 | |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame^] | 178 | TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, POLARSSL_MD_NONE, hash_len, hash_result, result_str ) == {correct} ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 179 | |
| 180 | rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 181 | } |
| 182 | END_CASE |
| 183 | |
| 184 | BEGIN_CASE |
| 185 | rsa_pkcs1_encrypt:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result |
| 186 | { |
| 187 | unsigned char message_str[1000]; |
| 188 | unsigned char output[1000]; |
| 189 | unsigned char output_str[1000]; |
| 190 | rsa_context ctx; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 191 | size_t msg_len; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 192 | rnd_pseudo_info rnd_info; |
| 193 | |
| 194 | memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 195 | |
Paul Bakker | ebcef6d | 2010-08-16 11:10:49 +0000 | [diff] [blame] | 196 | rsa_init( &ctx, {padding_mode}, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 197 | memset( message_str, 0x00, 1000 ); |
| 198 | memset( output, 0x00, 1000 ); |
| 199 | memset( output_str, 0x00, 1000 ); |
| 200 | |
| 201 | ctx.len = {mod} / 8; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 202 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 203 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 204 | |
| 205 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 ); |
| 206 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 207 | msg_len = unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 208 | |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 209 | TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PUBLIC, msg_len, message_str, output ) == {result} ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 210 | if( {result} == 0 ) |
| 211 | { |
| 212 | hexify( output_str, output, ctx.len ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 213 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 214 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 215 | } |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 216 | |
| 217 | rsa_free( &ctx ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 218 | } |
| 219 | END_CASE |
| 220 | |
| 221 | BEGIN_CASE |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 222 | rsa_pkcs1_encrypt_bad_rng:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result |
| 223 | { |
| 224 | unsigned char message_str[1000]; |
| 225 | unsigned char output[1000]; |
| 226 | unsigned char output_str[1000]; |
| 227 | rsa_context ctx; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 228 | size_t msg_len; |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 229 | |
Paul Bakker | ebcef6d | 2010-08-16 11:10:49 +0000 | [diff] [blame] | 230 | rsa_init( &ctx, {padding_mode}, 0 ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 231 | memset( message_str, 0x00, 1000 ); |
| 232 | memset( output, 0x00, 1000 ); |
| 233 | memset( output_str, 0x00, 1000 ); |
| 234 | |
| 235 | ctx.len = {mod} / 8; |
| 236 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 237 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 238 | |
| 239 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 ); |
| 240 | |
| 241 | msg_len = unhexify( message_str, {message_hex_string} ); |
| 242 | |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 243 | TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL, RSA_PUBLIC, msg_len, message_str, output ) == {result} ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 244 | if( {result} == 0 ) |
| 245 | { |
| 246 | hexify( output_str, output, ctx.len ); |
| 247 | |
| 248 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 249 | } |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 250 | |
| 251 | rsa_free( &ctx ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 252 | } |
| 253 | END_CASE |
| 254 | |
| 255 | BEGIN_CASE |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 256 | rsa_pkcs1_decrypt:message_hex_string:padding_mode:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:max_output:result_hex_str:result |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 257 | { |
| 258 | unsigned char message_str[1000]; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 259 | unsigned char output[1000]; |
| 260 | unsigned char output_str[1000]; |
| 261 | rsa_context ctx; |
| 262 | mpi P1, Q1, H, G; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 263 | size_t output_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 264 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 265 | mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G ); |
Paul Bakker | ebcef6d | 2010-08-16 11:10:49 +0000 | [diff] [blame] | 266 | rsa_init( &ctx, {padding_mode}, 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 267 | |
| 268 | memset( message_str, 0x00, 1000 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 269 | memset( output, 0x00, 1000 ); |
| 270 | memset( output_str, 0x00, 1000 ); |
| 271 | |
| 272 | ctx.len = {mod} / 8; |
| 273 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 274 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 275 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 276 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 277 | |
| 278 | TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
| 279 | TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
| 280 | TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
| 281 | TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
| 282 | TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
| 283 | TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
| 284 | TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
| 285 | TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
| 286 | |
| 287 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 288 | |
| 289 | unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 290 | output_len = 0; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 291 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 292 | TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, {max_output} ) == {result} ); |
| 293 | if( {result} == 0 ) |
| 294 | { |
| 295 | hexify( output_str, output, ctx.len ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 296 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 297 | TEST_ASSERT( strncasecmp( (char *) output_str, {result_hex_str}, strlen( {result_hex_str} ) ) == 0 ); |
| 298 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 299 | |
| 300 | mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 301 | rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 302 | } |
| 303 | END_CASE |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 304 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 305 | BEGIN_CASE |
| 306 | rsa_public:message_hex_string:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result |
| 307 | { |
| 308 | unsigned char message_str[1000]; |
| 309 | unsigned char output[1000]; |
| 310 | unsigned char output_str[1000]; |
| 311 | rsa_context ctx; |
| 312 | |
Paul Bakker | ebcef6d | 2010-08-16 11:10:49 +0000 | [diff] [blame] | 313 | rsa_init( &ctx, RSA_PKCS_V15, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 314 | memset( message_str, 0x00, 1000 ); |
| 315 | memset( output, 0x00, 1000 ); |
| 316 | memset( output_str, 0x00, 1000 ); |
| 317 | |
| 318 | ctx.len = {mod} / 8; |
| 319 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 320 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 321 | |
| 322 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 ); |
| 323 | |
| 324 | unhexify( message_str, {message_hex_string} ); |
| 325 | |
| 326 | TEST_ASSERT( rsa_public( &ctx, message_str, output ) == {result} ); |
| 327 | if( {result} == 0 ) |
| 328 | { |
| 329 | hexify( output_str, output, ctx.len ); |
| 330 | |
| 331 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 332 | } |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 333 | |
| 334 | rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 335 | } |
| 336 | END_CASE |
| 337 | |
| 338 | BEGIN_CASE |
| 339 | rsa_private:message_hex_string:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str:result |
| 340 | { |
| 341 | unsigned char message_str[1000]; |
| 342 | unsigned char output[1000]; |
| 343 | unsigned char output_str[1000]; |
| 344 | rsa_context ctx; |
| 345 | mpi P1, Q1, H, G; |
| 346 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 347 | mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G ); |
Paul Bakker | ebcef6d | 2010-08-16 11:10:49 +0000 | [diff] [blame] | 348 | rsa_init( &ctx, RSA_PKCS_V15, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 349 | |
| 350 | memset( message_str, 0x00, 1000 ); |
| 351 | memset( output, 0x00, 1000 ); |
| 352 | memset( output_str, 0x00, 1000 ); |
| 353 | |
| 354 | ctx.len = {mod} / 8; |
| 355 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 356 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 357 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 358 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 359 | |
| 360 | TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
| 361 | TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
| 362 | TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
| 363 | TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
| 364 | TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
| 365 | TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
| 366 | TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
| 367 | TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
| 368 | |
| 369 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 370 | |
| 371 | unhexify( message_str, {message_hex_string} ); |
| 372 | |
| 373 | TEST_ASSERT( rsa_private( &ctx, message_str, output ) == {result} ); |
| 374 | if( {result} == 0 ) |
| 375 | { |
| 376 | hexify( output_str, output, ctx.len ); |
| 377 | |
| 378 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 379 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 380 | |
| 381 | mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 382 | rsa_free( &ctx ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 383 | } |
| 384 | END_CASE |
| 385 | |
| 386 | BEGIN_CASE |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 387 | rsa_check_privkey_null: |
| 388 | { |
| 389 | rsa_context ctx; |
| 390 | memset( &ctx, 0x00, sizeof( rsa_context ) ); |
| 391 | |
| 392 | TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED ); |
| 393 | } |
| 394 | END_CASE |
| 395 | |
| 396 | BEGIN_CASE |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 397 | rsa_check_pubkey:radix_N:input_N:radix_E:input_E:result |
| 398 | { |
| 399 | rsa_context ctx; |
| 400 | |
Paul Bakker | ebcef6d | 2010-08-16 11:10:49 +0000 | [diff] [blame] | 401 | rsa_init( &ctx, RSA_PKCS_V15, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 402 | |
| 403 | if( strlen( {input_N} ) ) |
| 404 | { |
| 405 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 406 | } |
| 407 | if( strlen( {input_E} ) ) |
| 408 | { |
| 409 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 410 | } |
| 411 | |
| 412 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == {result} ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 413 | |
| 414 | rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 415 | } |
| 416 | END_CASE |
| 417 | |
| 418 | BEGIN_CASE |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 419 | rsa_check_privkey:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:radix_D:input_D:radix_DP:input_DP:radix_DQ:input_DQ:radix_QP:input_QP:result |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 420 | { |
| 421 | rsa_context ctx; |
| 422 | |
Paul Bakker | ebcef6d | 2010-08-16 11:10:49 +0000 | [diff] [blame] | 423 | rsa_init( &ctx, RSA_PKCS_V15, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 424 | |
| 425 | ctx.len = {mod} / 8; |
| 426 | if( strlen( {input_P} ) ) |
| 427 | { |
| 428 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 429 | } |
| 430 | if( strlen( {input_Q} ) ) |
| 431 | { |
| 432 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 433 | } |
| 434 | if( strlen( {input_N} ) ) |
| 435 | { |
| 436 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 437 | } |
| 438 | if( strlen( {input_E} ) ) |
| 439 | { |
| 440 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 441 | } |
| 442 | if( strlen( {input_D} ) ) |
| 443 | { |
| 444 | TEST_ASSERT( mpi_read_string( &ctx.D, {radix_D}, {input_D} ) == 0 ); |
| 445 | } |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 446 | if( strlen( {input_DP} ) ) |
| 447 | { |
| 448 | TEST_ASSERT( mpi_read_string( &ctx.DP, {radix_DP}, {input_DP} ) == 0 ); |
| 449 | } |
| 450 | if( strlen( {input_DQ} ) ) |
| 451 | { |
| 452 | TEST_ASSERT( mpi_read_string( &ctx.DQ, {radix_DQ}, {input_DQ} ) == 0 ); |
| 453 | } |
| 454 | if( strlen( {input_QP} ) ) |
| 455 | { |
| 456 | TEST_ASSERT( mpi_read_string( &ctx.QP, {radix_QP}, {input_QP} ) == 0 ); |
| 457 | } |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 458 | |
| 459 | TEST_ASSERT( rsa_check_privkey( &ctx ) == {result} ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 460 | |
| 461 | rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 462 | } |
| 463 | END_CASE |
| 464 | |
| 465 | BEGIN_CASE |
| 466 | rsa_gen_key:nrbits:exponent:result |
| 467 | { |
| 468 | rsa_context ctx; |
Paul Bakker | c0a1a31 | 2011-12-04 17:12:15 +0000 | [diff] [blame] | 469 | entropy_context entropy; |
| 470 | ctr_drbg_context ctr_drbg; |
| 471 | char *pers = "test_suite_rsa"; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 472 | |
Paul Bakker | c0a1a31 | 2011-12-04 17:12:15 +0000 | [diff] [blame] | 473 | entropy_init( &entropy ); |
| 474 | TEST_ASSERT( ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 475 | (unsigned char *) pers, strlen( pers ) ) == 0 ); |
| 476 | |
Paul Bakker | ebcef6d | 2010-08-16 11:10:49 +0000 | [diff] [blame] | 477 | rsa_init( &ctx, 0, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 478 | |
Paul Bakker | c0a1a31 | 2011-12-04 17:12:15 +0000 | [diff] [blame] | 479 | TEST_ASSERT( rsa_gen_key( &ctx, ctr_drbg_random, &ctr_drbg, {nrbits}, {exponent} ) == {result} ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 480 | if( {result} == 0 ) |
| 481 | { |
| 482 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 483 | } |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 484 | |
| 485 | rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 486 | } |
| 487 | END_CASE |
| 488 | |
| 489 | BEGIN_CASE |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 490 | rsa_selftest: |
| 491 | { |
| 492 | TEST_ASSERT( rsa_self_test( 0 ) == 0 ); |
| 493 | } |
| 494 | END_CASE |