blob: 2b7d4561b91357c6fb8d7047a111b027cb29a4f8 [file] [log] [blame]
Janos Follathf1225ea2016-02-12 13:18:20 +00001/* BEGIN_HEADER */
Janos Follath53eb0d12016-03-16 10:26:12 +00002#include "polarssl/rsa.h"
3#include "polarssl/md.h"
Janos Follathf1225ea2016-02-12 13:18:20 +00004/* END_HEADER */
5
6/* BEGIN_DEPENDENCIES
Janos Follath53eb0d12016-03-16 10:26:12 +00007 * depends_on:POLARSSL_PKCS1_V15:POLARSSL_RSA_C:POLARSSL_SHA1_C
Janos Follathf1225ea2016-02-12 13:18:20 +00008 * END_DEPENDENCIES
9 */
10
11/* BEGIN_CASE */
12void pkcs1_rsaes_v15_encrypt( int mod, int radix_N, char *input_N, int radix_E,
13 char *input_E, int hash,
14 char *message_hex_string, char *seed,
15 char *result_hex_str, int result )
16{
17 unsigned char message_str[1000];
18 unsigned char output[1000];
19 unsigned char output_str[1000];
20 unsigned char rnd_buf[1000];
Janos Follath53eb0d12016-03-16 10:26:12 +000021 rsa_context ctx;
Janos Follathf1225ea2016-02-12 13:18:20 +000022 size_t msg_len;
23 rnd_buf_info info;
24
25 info.length = unhexify( rnd_buf, seed );
26 info.buf = rnd_buf;
27
Janos Follath53eb0d12016-03-16 10:26:12 +000028 rsa_init( &ctx, RSA_PKCS_V15, hash );
Janos Follathf1225ea2016-02-12 13:18:20 +000029 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 );
Janos Follath53eb0d12016-03-16 10:26:12 +000034 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 );
Janos Follathf1225ea2016-02-12 13:18:20 +000036
Janos Follath53eb0d12016-03-16 10:26:12 +000037 TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
Janos Follathf1225ea2016-02-12 13:18:20 +000038
39 msg_len = unhexify( message_str, message_hex_string );
40
Janos Follath53eb0d12016-03-16 10:26:12 +000041 TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_buffer_rand, &info, RSA_PUBLIC, msg_len, message_str, output ) == result );
Janos Follathf1225ea2016-02-12 13:18:20 +000042 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
49exit:
Janos Follath53eb0d12016-03-16 10:26:12 +000050 rsa_free( &ctx );
Janos Follathf1225ea2016-02-12 13:18:20 +000051}
52/* END_CASE */
53
54/* BEGIN_CASE */
55void pkcs1_rsaes_v15_decrypt( int mod, int radix_P, char *input_P,
56 int radix_Q, char *input_Q, int radix_N,
57 char *input_N, int radix_E, char *input_E,
58 int hash, char *result_hex_str, char *seed,
59 char *message_hex_string, int result )
60{
61 unsigned char message_str[1000];
62 unsigned char output[1000];
63 unsigned char output_str[1000];
Janos Follath53eb0d12016-03-16 10:26:12 +000064 rsa_context ctx;
65 mpi P1, Q1, H, G;
Janos Follathf1225ea2016-02-12 13:18:20 +000066 size_t output_len;
67 rnd_pseudo_info rnd_info;
68 ((void) seed);
69
Janos Follath53eb0d12016-03-16 10:26:12 +000070 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
71 rsa_init( &ctx, RSA_PKCS_V15, hash );
Janos Follathf1225ea2016-02-12 13:18:20 +000072
73 memset( message_str, 0x00, 1000 );
74 memset( output, 0x00, 1000 );
75 memset( output_str, 0x00, 1000 );
76 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
77
78 ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 );
Janos Follath53eb0d12016-03-16 10:26:12 +000079 TEST_ASSERT( mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
80 TEST_ASSERT( mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
81 TEST_ASSERT( mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
82 TEST_ASSERT( mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
Janos Follathf1225ea2016-02-12 13:18:20 +000083
Janos Follath53eb0d12016-03-16 10:26:12 +000084 TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
85 TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
86 TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
87 TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
88 TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
89 TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
90 TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
91 TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
Janos Follathf1225ea2016-02-12 13:18:20 +000092
Janos Follath53eb0d12016-03-16 10:26:12 +000093 TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
Janos Follathf1225ea2016-02-12 13:18:20 +000094
95 unhexify( message_str, message_hex_string );
96
Janos Follath53eb0d12016-03-16 10:26:12 +000097 TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PRIVATE, &output_len, message_str, output, 1000 ) == result );
Janos Follathf1225ea2016-02-12 13:18:20 +000098 if( result == 0 )
99 {
100 hexify( output_str, output, ctx.len );
101
102 TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 );
103 }
104
105exit:
Janos Follath53eb0d12016-03-16 10:26:12 +0000106 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
107 rsa_free( &ctx );
Janos Follathf1225ea2016-02-12 13:18:20 +0000108}
109/* END_CASE */
110