Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1 | BEGIN_HEADER |
| 2 | #include <polarssl/config.h> |
| 3 | #include <polarssl/rsa.h> |
| 4 | #include <polarssl/md.h> |
| 5 | #include <polarssl/md2.h> |
| 6 | #include <polarssl/md4.h> |
| 7 | #include <polarssl/md5.h> |
| 8 | #include <polarssl/sha1.h> |
| 9 | #include <polarssl/sha2.h> |
| 10 | #include <polarssl/sha4.h> |
| 11 | END_HEADER |
| 12 | |
| 13 | BEGIN_CASE |
| 14 | pkcs1_rsaes_oaep_encrypt:mod:radix_N:input_N:radix_E:input_E:hash:message_hex_string:seed:result_hex_str:result |
| 15 | { |
| 16 | unsigned char message_str[1000]; |
| 17 | unsigned char output[1000]; |
| 18 | unsigned char output_str[1000]; |
| 19 | unsigned char rnd_buf[1000]; |
| 20 | rsa_context ctx; |
| 21 | int msg_len; |
| 22 | rnd_info info; |
| 23 | |
| 24 | info.length = unhexify( rnd_buf, {seed} ); |
| 25 | info.buf = rnd_buf; |
| 26 | info.per_call = 1; |
| 27 | |
| 28 | rsa_init( &ctx, RSA_PKCS_V21, {hash} ); |
| 29 | memset( message_str, 0x00, 1000 ); |
| 30 | memset( output, 0x00, 1000 ); |
| 31 | memset( output_str, 0x00, 1000 ); |
| 32 | |
| 33 | ctx.len = {mod} / 8 + ( ( {mod} % 8 ) ? 1 : 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( rsa_check_pubkey( &ctx ) == 0 ); |
| 38 | |
| 39 | msg_len = unhexify( message_str, {message_hex_string} ); |
| 40 | |
| 41 | TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_buffer_rand, &info, RSA_PUBLIC, msg_len, message_str, output ) == {result} ); |
| 42 | if( {result} == 0 ) |
| 43 | { |
| 44 | hexify( output_str, output, ctx.len ); |
| 45 | |
| 46 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 47 | } |
| 48 | } |
| 49 | END_CASE |
| 50 | |
| 51 | BEGIN_CASE |
| 52 | pkcs1_rsaes_oaep_decrypt:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:hash:result_hex_str:seed:message_hex_string:result |
| 53 | { |
| 54 | unsigned char message_str[1000]; |
| 55 | unsigned char output[1000]; |
| 56 | unsigned char output_str[1000]; |
| 57 | rsa_context ctx; |
| 58 | mpi P1, Q1, H, G; |
| 59 | int output_len; |
| 60 | int msg_len; |
| 61 | |
| 62 | mpi_init( &P1, &Q1, &H, &G, NULL ); |
| 63 | rsa_init( &ctx, RSA_PKCS_V21, {hash} ); |
| 64 | |
| 65 | memset( message_str, 0x00, 1000 ); |
| 66 | memset( output, 0x00, 1000 ); |
| 67 | memset( output_str, 0x00, 1000 ); |
| 68 | |
| 69 | ctx.len = {mod} / 8 + ( ( {mod} % 8 ) ? 1 : 0 ); |
| 70 | TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 ); |
| 71 | TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 ); |
| 72 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 73 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 74 | |
| 75 | TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
| 76 | TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
| 77 | TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
| 78 | TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
| 79 | TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
| 80 | TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
| 81 | TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
| 82 | TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
| 83 | |
| 84 | TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 ); |
| 85 | |
| 86 | msg_len = unhexify( message_str, {message_hex_string} ); |
| 87 | |
| 88 | TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, 1000 ) == {result} ); |
| 89 | if( {result} == 0 ) |
| 90 | { |
| 91 | hexify( output_str, output, ctx.len ); |
| 92 | |
| 93 | TEST_ASSERT( strncasecmp( (char *) output_str, {result_hex_str}, strlen( {result_hex_str} ) ) == 0 ); |
| 94 | } |
| 95 | } |
| 96 | END_CASE |
| 97 | |
| 98 | BEGIN_CASE |
| 99 | pkcs1_rsassa_pss_sign:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:digest:hash:message_hex_string:salt:result_hex_str:result |
| 100 | { |
| 101 | unsigned char message_str[1000]; |
| 102 | unsigned char hash_result[1000]; |
| 103 | unsigned char output[1000]; |
| 104 | unsigned char output_str[1000]; |
| 105 | unsigned char rnd_buf[1000]; |
| 106 | rsa_context ctx; |
| 107 | mpi P1, Q1, H, G; |
| 108 | int msg_len; |
| 109 | rnd_info info; |
| 110 | |
| 111 | info.length = unhexify( rnd_buf, {salt} ); |
| 112 | info.buf = rnd_buf; |
| 113 | info.per_call = 1; |
| 114 | |
| 115 | mpi_init( &P1, &Q1, &H, &G, NULL ); |
| 116 | rsa_init( &ctx, RSA_PKCS_V21, {hash} ); |
| 117 | |
| 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 + ( ( {mod} % 8 ) ? 1 : 0 ); |
| 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 | |
| 140 | msg_len = unhexify( message_str, {message_hex_string} ); |
| 141 | |
| 142 | switch( {digest} ) |
| 143 | { |
| 144 | #ifdef POLARSSL_MD2_C |
| 145 | case SIG_RSA_MD2: |
| 146 | md2( message_str, msg_len, hash_result ); |
| 147 | break; |
| 148 | #endif |
| 149 | #ifdef POLARSSL_MD4_C |
| 150 | case SIG_RSA_MD4: |
| 151 | md4( message_str, msg_len, hash_result ); |
| 152 | break; |
| 153 | #endif |
| 154 | #ifdef POLARSSL_MD5_C |
| 155 | case SIG_RSA_MD5: |
| 156 | md5( message_str, msg_len, hash_result ); |
| 157 | break; |
| 158 | #endif |
| 159 | #ifdef POLARSSL_SHA1_C |
| 160 | case SIG_RSA_SHA1: |
| 161 | sha1( message_str, msg_len, hash_result ); |
| 162 | break; |
| 163 | #endif |
| 164 | #ifdef POLARSSL_SHA2_C |
| 165 | case SIG_RSA_SHA224: |
| 166 | sha2( message_str, msg_len, hash_result, 1 ); |
| 167 | break; |
| 168 | case SIG_RSA_SHA256: |
| 169 | sha2( message_str, msg_len, hash_result, 0 ); |
| 170 | break; |
| 171 | #endif |
| 172 | #ifdef POLARSSL_SHA4_C |
| 173 | case SIG_RSA_SHA384: |
| 174 | sha4( message_str, msg_len, hash_result, 1 ); |
| 175 | break; |
| 176 | case SIG_RSA_SHA512: |
| 177 | sha4( message_str, msg_len, hash_result, 0 ); |
| 178 | break; |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | TEST_ASSERT( rsa_pkcs1_sign( &ctx, &rnd_buffer_rand, &info, RSA_PRIVATE, {digest}, 0, hash_result, output ) == {result} ); |
| 183 | if( {result} == 0 ) |
| 184 | { |
| 185 | hexify( output_str, output, ctx.len); |
| 186 | |
| 187 | TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 ); |
| 188 | } |
| 189 | } |
| 190 | END_CASE |
| 191 | |
| 192 | BEGIN_CASE |
| 193 | pkcs1_rsassa_pss_verify:mod:radix_N:input_N:radix_E:input_E:digest:hash:message_hex_string:salt:result_hex_str:result |
| 194 | { |
| 195 | unsigned char message_str[1000]; |
| 196 | unsigned char hash_result[1000]; |
| 197 | unsigned char result_str[1000]; |
| 198 | rsa_context ctx; |
| 199 | int msg_len; |
| 200 | |
| 201 | rsa_init( &ctx, RSA_PKCS_V21, {hash} ); |
| 202 | memset( message_str, 0x00, 1000 ); |
| 203 | memset( hash_result, 0x00, 1000 ); |
| 204 | memset( result_str, 0x00, 1000 ); |
| 205 | |
| 206 | ctx.len = {mod} / 8 + ( ( {mod} % 8 ) ? 1 : 0 ); |
| 207 | TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 ); |
| 208 | TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 ); |
| 209 | |
| 210 | TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 ); |
| 211 | |
| 212 | msg_len = unhexify( message_str, {message_hex_string} ); |
| 213 | unhexify( result_str, {result_hex_str} ); |
| 214 | |
| 215 | switch( {digest} ) |
| 216 | { |
| 217 | #ifdef POLARSSL_MD2_C |
| 218 | case SIG_RSA_MD2: |
| 219 | md2( message_str, msg_len, hash_result ); |
| 220 | break; |
| 221 | #endif |
| 222 | #ifdef POLARSSL_MD4_C |
| 223 | case SIG_RSA_MD4: |
| 224 | md4( message_str, msg_len, hash_result ); |
| 225 | break; |
| 226 | #endif |
| 227 | #ifdef POLARSSL_MD5_C |
| 228 | case SIG_RSA_MD5: |
| 229 | md5( message_str, msg_len, hash_result ); |
| 230 | break; |
| 231 | #endif |
| 232 | #ifdef POLARSSL_SHA1_C |
| 233 | case SIG_RSA_SHA1: |
| 234 | sha1( message_str, msg_len, hash_result ); |
| 235 | break; |
| 236 | #endif |
| 237 | #ifdef POLARSSL_SHA2_C |
| 238 | case SIG_RSA_SHA224: |
| 239 | sha2( message_str, msg_len, hash_result, 1 ); |
| 240 | break; |
| 241 | case SIG_RSA_SHA256: |
| 242 | sha2( message_str, msg_len, hash_result, 0 ); |
| 243 | break; |
| 244 | #endif |
| 245 | #ifdef POLARSSL_SHA4_C |
| 246 | case SIG_RSA_SHA384: |
| 247 | sha4( message_str, msg_len, hash_result, 1 ); |
| 248 | break; |
| 249 | case SIG_RSA_SHA512: |
| 250 | sha4( message_str, msg_len, hash_result, 0 ); |
| 251 | break; |
| 252 | #endif |
| 253 | } |
| 254 | |
| 255 | TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, {digest}, 0, hash_result, result_str ) == {result} ); |
| 256 | } |
| 257 | END_CASE |