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