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