Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 1 | BEGIN_HEADER |
| 2 | #include <polarssl/rsa.h> |
| 3 | #include <polarssl/sha1.h> |
| 4 | #include <polarssl/sha2.h> |
| 5 | #include <polarssl/sha4.h> |
| 6 | END_HEADER |
| 7 | |
| 8 | BEGIN_CASE |
| 9 | rsa_pkcs1_sign:message_hex_string:digest:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str |
| 10 | { |
| 11 | unsigned char message_str[1000]; |
| 12 | unsigned char hash_result[1000]; |
| 13 | unsigned char output[1000]; |
| 14 | unsigned char output_str[1000]; |
| 15 | rsa_context ctx; |
| 16 | mpi P1, Q1, H, G; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 17 | int msg_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 18 | |
| 19 | mpi_init( &P1, &Q1, &H, &G, NULL ); |
| 20 | rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL ); |
| 21 | |
| 22 | memset( message_str, 0x00, 1000 ); |
| 23 | memset( hash_result, 0x00, 1000 ); |
| 24 | memset( output, 0x00, 1000 ); |
| 25 | memset( output_str, 0x00, 1000 ); |
| 26 | |
| 27 | ctx.len = {mod} / 8; |
| 28 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 29 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 30 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 31 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 32 | |
| 33 | TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
| 34 | TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
| 35 | TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
| 36 | TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
| 37 | TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
| 38 | TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
| 39 | TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
| 40 | TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
| 41 | |
| 42 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 43 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 44 | msg_len = unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 45 | |
| 46 | if( {digest} == SIG_RSA_SHA1 ) |
| 47 | sha1( message_str, msg_len, hash_result ); |
| 48 | else if( {digest} == SIG_RSA_SHA224 ) |
| 49 | sha2( message_str, msg_len, hash_result, 1 ); |
| 50 | else if( {digest} == SIG_RSA_SHA256 ) |
| 51 | sha2( message_str, msg_len, hash_result, 0 ); |
| 52 | else if( {digest} == SIG_RSA_SHA384 ) |
| 53 | sha4( message_str, msg_len, hash_result, 1 ); |
| 54 | else if( {digest} == SIG_RSA_SHA512 ) |
| 55 | sha4( message_str, msg_len, hash_result, 0 ); |
| 56 | else |
| 57 | TEST_ASSERT( 0 ); |
| 58 | |
| 59 | TEST_ASSERT( rsa_pkcs1_sign( &ctx, RSA_PRIVATE, {digest}, 0, hash_result, output ) == 0 ); |
| 60 | |
| 61 | hexify( output_str, output, ctx.len ); |
| 62 | |
| 63 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 64 | } |
| 65 | END_CASE |
| 66 | |
| 67 | BEGIN_CASE |
| 68 | rsa_pkcs1_verify:message_hex_string:digest:mod:radix_N:input_N:radix_E:input_E:result_hex_str:correct |
| 69 | { |
| 70 | unsigned char message_str[1000]; |
| 71 | unsigned char hash_result[1000]; |
| 72 | unsigned char result_str[1000]; |
| 73 | rsa_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 74 | int msg_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 75 | |
| 76 | rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL ); |
| 77 | memset( message_str, 0x00, 1000 ); |
| 78 | memset( hash_result, 0x00, 1000 ); |
| 79 | memset( result_str, 0x00, 1000 ); |
| 80 | |
| 81 | ctx.len = {mod} / 8; |
| 82 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 83 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 84 | |
| 85 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 ); |
| 86 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 87 | msg_len = unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 88 | unhexify( result_str, {result_hex_str} ); |
| 89 | |
| 90 | if( {digest} == SIG_RSA_SHA1 ) |
| 91 | sha1( message_str, msg_len, hash_result ); |
| 92 | else if( {digest} == SIG_RSA_SHA224 ) |
| 93 | sha2( message_str, msg_len, hash_result, 1 ); |
| 94 | else if( {digest} == SIG_RSA_SHA256 ) |
| 95 | sha2( message_str, msg_len, hash_result, 0 ); |
| 96 | else if( {digest} == SIG_RSA_SHA384 ) |
| 97 | sha4( message_str, msg_len, hash_result, 1 ); |
| 98 | else if( {digest} == SIG_RSA_SHA512 ) |
| 99 | sha4( message_str, msg_len, hash_result, 0 ); |
| 100 | else |
| 101 | TEST_ASSERT( 0 ); |
| 102 | |
| 103 | TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, {digest}, 0, hash_result, result_str ) == {correct} ); |
| 104 | } |
| 105 | END_CASE |
| 106 | |
| 107 | BEGIN_CASE |
| 108 | rsa_pkcs1_encrypt:message_hex_string:mod:radix_N:input_N:radix_E:input_E:result_hex_str |
| 109 | { |
| 110 | unsigned char message_str[1000]; |
| 111 | unsigned char hash_result[1000]; |
| 112 | unsigned char output[1000]; |
| 113 | unsigned char output_str[1000]; |
| 114 | rsa_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 115 | int msg_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 116 | |
| 117 | rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL ); |
| 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; |
| 124 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 125 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 126 | |
| 127 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 ); |
| 128 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 129 | msg_len = unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 130 | |
| 131 | TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, RSA_PUBLIC, msg_len, message_str, output ) == 0 ); |
| 132 | |
| 133 | hexify( output_str, output, ctx.len ); |
| 134 | |
| 135 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 136 | } |
| 137 | END_CASE |
| 138 | |
| 139 | BEGIN_CASE |
| 140 | rsa_pkcs1_decrypt:message_hex_string:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str |
| 141 | { |
| 142 | unsigned char message_str[1000]; |
| 143 | unsigned char hash_result[1000]; |
| 144 | unsigned char output[1000]; |
| 145 | unsigned char output_str[1000]; |
| 146 | rsa_context ctx; |
| 147 | mpi P1, Q1, H, G; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 148 | int output_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 149 | |
| 150 | mpi_init( &P1, &Q1, &H, &G, NULL ); |
| 151 | rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL ); |
| 152 | |
| 153 | memset( message_str, 0x00, 1000 ); |
| 154 | memset( hash_result, 0x00, 1000 ); |
| 155 | memset( output, 0x00, 1000 ); |
| 156 | memset( output_str, 0x00, 1000 ); |
| 157 | |
| 158 | ctx.len = {mod} / 8; |
| 159 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 160 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 161 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 162 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 163 | |
| 164 | TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
| 165 | TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
| 166 | TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
| 167 | TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
| 168 | TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
| 169 | TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
| 170 | TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
| 171 | TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
| 172 | |
| 173 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 174 | |
| 175 | unhexify( message_str, {message_hex_string} ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 176 | output_len = 0; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 177 | |
| 178 | TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, 1000 ) == 0 ); |
| 179 | |
| 180 | hexify( output_str, output, ctx.len ); |
| 181 | |
| 182 | TEST_ASSERT( strncasecmp( (char *) output_str, {result_hex_str}, strlen( {result_hex_str} ) ) == 0 ); |
| 183 | } |
| 184 | END_CASE |
| 185 | |
| 186 | BEGIN_CASE |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 187 | rsa_check_privkey_null: |
| 188 | { |
| 189 | rsa_context ctx; |
| 190 | memset( &ctx, 0x00, sizeof( rsa_context ) ); |
| 191 | |
| 192 | TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED ); |
| 193 | } |
| 194 | END_CASE |
| 195 | |
| 196 | BEGIN_CASE |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 197 | rsa_selftest: |
| 198 | { |
| 199 | TEST_ASSERT( rsa_self_test( 0 ) == 0 ); |
| 200 | } |
| 201 | END_CASE |