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