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 | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 9 | #include <polarssl/havege.h> |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 10 | END_HEADER |
| 11 | |
| 12 | BEGIN_CASE |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 13 | 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] | 14 | { |
| 15 | unsigned char message_str[1000]; |
| 16 | unsigned char hash_result[1000]; |
| 17 | unsigned char output[1000]; |
| 18 | unsigned char output_str[1000]; |
| 19 | rsa_context ctx; |
| 20 | mpi P1, Q1, H, G; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 21 | int msg_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 22 | |
| 23 | mpi_init( &P1, &Q1, &H, &G, NULL ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 24 | rsa_init( &ctx, {padding_mode}, 0, NULL, NULL ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 25 | |
| 26 | memset( message_str, 0x00, 1000 ); |
| 27 | memset( hash_result, 0x00, 1000 ); |
| 28 | memset( output, 0x00, 1000 ); |
| 29 | memset( output_str, 0x00, 1000 ); |
| 30 | |
| 31 | ctx.len = {mod} / 8; |
| 32 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 33 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 34 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 35 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 36 | |
| 37 | TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
| 38 | TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
| 39 | TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
| 40 | TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
| 41 | TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
| 42 | TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
| 43 | TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
| 44 | TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
| 45 | |
| 46 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 47 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 48 | msg_len = unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 49 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 50 | if( {digest} == SIG_RSA_MD2 ) |
| 51 | md2( message_str, msg_len, hash_result ); |
| 52 | else if( {digest} == SIG_RSA_MD4 ) |
| 53 | md4( message_str, msg_len, hash_result ); |
| 54 | else if( {digest} == SIG_RSA_MD5 ) |
| 55 | md5( message_str, msg_len, hash_result ); |
| 56 | else if( {digest} == SIG_RSA_SHA1 ) |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 57 | sha1( message_str, msg_len, hash_result ); |
| 58 | else if( {digest} == SIG_RSA_SHA224 ) |
| 59 | sha2( message_str, msg_len, hash_result, 1 ); |
| 60 | else if( {digest} == SIG_RSA_SHA256 ) |
| 61 | sha2( message_str, msg_len, hash_result, 0 ); |
| 62 | else if( {digest} == SIG_RSA_SHA384 ) |
| 63 | sha4( message_str, msg_len, hash_result, 1 ); |
| 64 | else if( {digest} == SIG_RSA_SHA512 ) |
| 65 | sha4( message_str, msg_len, hash_result, 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 66 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 67 | TEST_ASSERT( rsa_pkcs1_sign( &ctx, RSA_PRIVATE, {digest}, 0, hash_result, output ) == {result} ); |
| 68 | if( {result} == 0 ) |
| 69 | { |
| 70 | hexify( output_str, output, ctx.len ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 71 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 72 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 73 | } |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 74 | } |
| 75 | END_CASE |
| 76 | |
| 77 | BEGIN_CASE |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 78 | 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] | 79 | { |
| 80 | unsigned char message_str[1000]; |
| 81 | unsigned char hash_result[1000]; |
| 82 | unsigned char result_str[1000]; |
| 83 | rsa_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 84 | int msg_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 85 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 86 | rsa_init( &ctx, {padding_mode}, 0, NULL, NULL ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 87 | memset( message_str, 0x00, 1000 ); |
| 88 | memset( hash_result, 0x00, 1000 ); |
| 89 | memset( result_str, 0x00, 1000 ); |
| 90 | |
| 91 | ctx.len = {mod} / 8; |
| 92 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 93 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 94 | |
| 95 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 ); |
| 96 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 97 | msg_len = unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 98 | unhexify( result_str, {result_hex_str} ); |
| 99 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 100 | if( {digest} == SIG_RSA_MD2 ) |
| 101 | md2( message_str, msg_len, hash_result ); |
| 102 | else if( {digest} == SIG_RSA_MD4 ) |
| 103 | md4( message_str, msg_len, hash_result ); |
| 104 | else if( {digest} == SIG_RSA_MD5 ) |
| 105 | md5( message_str, msg_len, hash_result ); |
| 106 | else if( {digest} == SIG_RSA_SHA1 ) |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 107 | sha1( message_str, msg_len, hash_result ); |
| 108 | else if( {digest} == SIG_RSA_SHA224 ) |
| 109 | sha2( message_str, msg_len, hash_result, 1 ); |
| 110 | else if( {digest} == SIG_RSA_SHA256 ) |
| 111 | sha2( message_str, msg_len, hash_result, 0 ); |
| 112 | else if( {digest} == SIG_RSA_SHA384 ) |
| 113 | sha4( message_str, msg_len, hash_result, 1 ); |
| 114 | else if( {digest} == SIG_RSA_SHA512 ) |
| 115 | sha4( message_str, msg_len, hash_result, 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 116 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 117 | TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, {digest}, 0, hash_result, result_str ) == {result} ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 118 | } |
| 119 | END_CASE |
| 120 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 121 | |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 122 | BEGIN_CASE |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 123 | 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] | 124 | { |
| 125 | unsigned char message_str[1000]; |
| 126 | unsigned char hash_result[1000]; |
| 127 | unsigned char output[1000]; |
| 128 | unsigned char output_str[1000]; |
| 129 | rsa_context ctx; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 130 | mpi P1, Q1, H, G; |
| 131 | int msg_len, hash_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 132 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 133 | mpi_init( &P1, &Q1, &H, &G, NULL ); |
| 134 | rsa_init( &ctx, {padding_mode}, 0, NULL, NULL ); |
| 135 | |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 136 | memset( message_str, 0x00, 1000 ); |
| 137 | memset( hash_result, 0x00, 1000 ); |
| 138 | memset( output, 0x00, 1000 ); |
| 139 | memset( output_str, 0x00, 1000 ); |
| 140 | |
| 141 | ctx.len = {mod} / 8; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 142 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 143 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 144 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 145 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 146 | |
| 147 | TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
| 148 | TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
| 149 | TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
| 150 | TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
| 151 | TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
| 152 | TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
| 153 | TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
| 154 | TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
| 155 | |
| 156 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 157 | |
| 158 | msg_len = unhexify( message_str, {message_hex_string} ); |
| 159 | hash_len = unhexify( hash_result, {hash_result_string} ); |
| 160 | |
| 161 | TEST_ASSERT( rsa_pkcs1_sign( &ctx, RSA_PRIVATE, RSA_RAW, hash_len, hash_result, output ) == 0 ); |
| 162 | |
| 163 | hexify( output_str, output, ctx.len ); |
| 164 | |
| 165 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 166 | } |
| 167 | END_CASE |
| 168 | |
| 169 | BEGIN_CASE |
| 170 | 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 |
| 171 | { |
| 172 | unsigned char message_str[1000]; |
| 173 | unsigned char hash_result[1000]; |
| 174 | unsigned char result_str[1000]; |
| 175 | rsa_context ctx; |
| 176 | int msg_len, hash_len; |
| 177 | |
| 178 | rsa_init( &ctx, {padding_mode}, 0, NULL, NULL ); |
| 179 | memset( message_str, 0x00, 1000 ); |
| 180 | memset( hash_result, 0x00, 1000 ); |
| 181 | memset( result_str, 0x00, 1000 ); |
| 182 | |
| 183 | ctx.len = {mod} / 8; |
| 184 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 185 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 186 | |
| 187 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 ); |
| 188 | |
| 189 | msg_len = unhexify( message_str, {message_hex_string} ); |
| 190 | hash_len = unhexify( hash_result, {hash_result_string} ); |
| 191 | unhexify( result_str, {result_hex_str} ); |
| 192 | |
| 193 | TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, RSA_RAW, hash_len, hash_result, result_str ) == {correct} ); |
| 194 | } |
| 195 | END_CASE |
| 196 | |
| 197 | BEGIN_CASE |
| 198 | rsa_pkcs1_encrypt:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result |
| 199 | { |
| 200 | unsigned char message_str[1000]; |
| 201 | unsigned char output[1000]; |
| 202 | unsigned char output_str[1000]; |
| 203 | rsa_context ctx; |
| 204 | int msg_len; |
| 205 | |
| 206 | rsa_init( &ctx, {padding_mode}, 0, NULL, NULL ); |
| 207 | memset( message_str, 0x00, 1000 ); |
| 208 | memset( output, 0x00, 1000 ); |
| 209 | memset( output_str, 0x00, 1000 ); |
| 210 | |
| 211 | ctx.len = {mod} / 8; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 212 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 213 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 214 | |
| 215 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 ); |
| 216 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 217 | msg_len = unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 218 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 219 | TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, RSA_PUBLIC, msg_len, message_str, output ) == {result} ); |
| 220 | if( {result} == 0 ) |
| 221 | { |
| 222 | hexify( output_str, output, ctx.len ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 223 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 224 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 225 | } |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 226 | } |
| 227 | END_CASE |
| 228 | |
| 229 | BEGIN_CASE |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 230 | 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] | 231 | { |
| 232 | unsigned char message_str[1000]; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 233 | unsigned char output[1000]; |
| 234 | unsigned char output_str[1000]; |
| 235 | rsa_context ctx; |
| 236 | mpi P1, Q1, H, G; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 237 | int output_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 238 | |
| 239 | mpi_init( &P1, &Q1, &H, &G, NULL ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 240 | rsa_init( &ctx, {padding_mode}, 0, NULL, NULL ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 241 | |
| 242 | memset( message_str, 0x00, 1000 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 243 | memset( output, 0x00, 1000 ); |
| 244 | memset( output_str, 0x00, 1000 ); |
| 245 | |
| 246 | ctx.len = {mod} / 8; |
| 247 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 248 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 249 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 250 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 251 | |
| 252 | TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
| 253 | TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
| 254 | TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
| 255 | TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
| 256 | TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
| 257 | TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
| 258 | TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
| 259 | TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
| 260 | |
| 261 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 262 | |
| 263 | unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 264 | output_len = 0; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 265 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 266 | TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, {max_output} ) == {result} ); |
| 267 | if( {result} == 0 ) |
| 268 | { |
| 269 | hexify( output_str, output, ctx.len ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 270 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 271 | TEST_ASSERT( strncasecmp( (char *) output_str, {result_hex_str}, strlen( {result_hex_str} ) ) == 0 ); |
| 272 | } |
| 273 | } |
| 274 | END_CASE |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 275 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 276 | BEGIN_CASE |
| 277 | rsa_public:message_hex_string:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result |
| 278 | { |
| 279 | unsigned char message_str[1000]; |
| 280 | unsigned char output[1000]; |
| 281 | unsigned char output_str[1000]; |
| 282 | rsa_context ctx; |
| 283 | |
| 284 | rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL ); |
| 285 | memset( message_str, 0x00, 1000 ); |
| 286 | memset( output, 0x00, 1000 ); |
| 287 | memset( output_str, 0x00, 1000 ); |
| 288 | |
| 289 | ctx.len = {mod} / 8; |
| 290 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 291 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 292 | |
| 293 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 ); |
| 294 | |
| 295 | unhexify( message_str, {message_hex_string} ); |
| 296 | |
| 297 | TEST_ASSERT( rsa_public( &ctx, message_str, output ) == {result} ); |
| 298 | if( {result} == 0 ) |
| 299 | { |
| 300 | hexify( output_str, output, ctx.len ); |
| 301 | |
| 302 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 303 | } |
| 304 | } |
| 305 | END_CASE |
| 306 | |
| 307 | BEGIN_CASE |
| 308 | 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 |
| 309 | { |
| 310 | unsigned char message_str[1000]; |
| 311 | unsigned char output[1000]; |
| 312 | unsigned char output_str[1000]; |
| 313 | rsa_context ctx; |
| 314 | mpi P1, Q1, H, G; |
| 315 | |
| 316 | mpi_init( &P1, &Q1, &H, &G, NULL ); |
| 317 | rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL ); |
| 318 | |
| 319 | memset( message_str, 0x00, 1000 ); |
| 320 | memset( output, 0x00, 1000 ); |
| 321 | memset( output_str, 0x00, 1000 ); |
| 322 | |
| 323 | ctx.len = {mod} / 8; |
| 324 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 325 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 326 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 327 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 328 | |
| 329 | TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
| 330 | TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
| 331 | TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
| 332 | TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
| 333 | TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
| 334 | TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
| 335 | TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
| 336 | TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
| 337 | |
| 338 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 339 | |
| 340 | unhexify( message_str, {message_hex_string} ); |
| 341 | |
| 342 | TEST_ASSERT( rsa_private( &ctx, message_str, output ) == {result} ); |
| 343 | if( {result} == 0 ) |
| 344 | { |
| 345 | hexify( output_str, output, ctx.len ); |
| 346 | |
| 347 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 348 | } |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 349 | } |
| 350 | END_CASE |
| 351 | |
| 352 | BEGIN_CASE |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 353 | rsa_check_privkey_null: |
| 354 | { |
| 355 | rsa_context ctx; |
| 356 | memset( &ctx, 0x00, sizeof( rsa_context ) ); |
| 357 | |
| 358 | TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED ); |
| 359 | } |
| 360 | END_CASE |
| 361 | |
| 362 | BEGIN_CASE |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame^] | 363 | rsa_check_pubkey:radix_N:input_N:radix_E:input_E:result |
| 364 | { |
| 365 | rsa_context ctx; |
| 366 | |
| 367 | rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL ); |
| 368 | |
| 369 | if( strlen( {input_N} ) ) |
| 370 | { |
| 371 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 372 | } |
| 373 | if( strlen( {input_E} ) ) |
| 374 | { |
| 375 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 376 | } |
| 377 | |
| 378 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == {result} ); |
| 379 | } |
| 380 | END_CASE |
| 381 | |
| 382 | BEGIN_CASE |
| 383 | rsa_check_privkey:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:radix_D:input_D:result |
| 384 | { |
| 385 | rsa_context ctx; |
| 386 | |
| 387 | rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL ); |
| 388 | |
| 389 | ctx.len = {mod} / 8; |
| 390 | if( strlen( {input_P} ) ) |
| 391 | { |
| 392 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 393 | } |
| 394 | if( strlen( {input_Q} ) ) |
| 395 | { |
| 396 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 397 | } |
| 398 | if( strlen( {input_N} ) ) |
| 399 | { |
| 400 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 401 | } |
| 402 | if( strlen( {input_E} ) ) |
| 403 | { |
| 404 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 405 | } |
| 406 | if( strlen( {input_D} ) ) |
| 407 | { |
| 408 | TEST_ASSERT( mpi_read_string( &ctx.D, {radix_D}, {input_D} ) == 0 ); |
| 409 | } |
| 410 | |
| 411 | TEST_ASSERT( rsa_check_privkey( &ctx ) == {result} ); |
| 412 | } |
| 413 | END_CASE |
| 414 | |
| 415 | BEGIN_CASE |
| 416 | rsa_gen_key:nrbits:exponent:result |
| 417 | { |
| 418 | rsa_context ctx; |
| 419 | havege_state hs; |
| 420 | |
| 421 | havege_init( &hs ); |
| 422 | rsa_init( &ctx, 0, 0, havege_rand, &hs ); |
| 423 | |
| 424 | TEST_ASSERT( rsa_gen_key( &ctx, {nrbits}, {exponent} ) == {result} ); |
| 425 | if( {result} == 0 ) |
| 426 | { |
| 427 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 428 | } |
| 429 | } |
| 430 | END_CASE |
| 431 | |
| 432 | BEGIN_CASE |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 433 | rsa_selftest: |
| 434 | { |
| 435 | TEST_ASSERT( rsa_self_test( 0 ) == 0 ); |
| 436 | } |
| 437 | END_CASE |