blob: 9cf3b1934f3adbe63915086217c2022b4a25b553 [file] [log] [blame]
Janos Follath8a49a012016-02-12 13:18:20 +00001/* BEGIN_HEADER */
2#include "mbedtls/rsa.h"
3#include "mbedtls/md.h"
4/* END_HEADER */
5
6/* BEGIN_DEPENDENCIES
7 * depends_on:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_SHA1_C
8 * END_DEPENDENCIES
9 */
10
11/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010012void pkcs1_rsaes_v15_encrypt( int mod, int radix_N, char * input_N,
13 int radix_E, char * input_E, int hash,
Azim Khand30ca132017-06-09 04:32:58 +010014 HexParam_t * message_str, HexParam_t * rnd_buf,
15 HexParam_t * result_hex_str, int result )
Janos Follath8a49a012016-02-12 13:18:20 +000016{
Janos Follath8a49a012016-02-12 13:18:20 +000017 unsigned char output[1000];
Janos Follath8a49a012016-02-12 13:18:20 +000018 mbedtls_rsa_context ctx;
Janos Follath8a49a012016-02-12 13:18:20 +000019 rnd_buf_info info;
Hanno Becker6d43f9e2017-08-23 06:35:17 +010020 mbedtls_mpi N, E;
Janos Follath8a49a012016-02-12 13:18:20 +000021
Azim Khand30ca132017-06-09 04:32:58 +010022 info.buf = rnd_buf->x;
23 info.length = rnd_buf->len;
Janos Follath8a49a012016-02-12 13:18:20 +000024
Hanno Becker6d43f9e2017-08-23 06:35:17 +010025 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Janos Follath8a49a012016-02-12 13:18:20 +000026 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, hash );
Janos Follath8a49a012016-02-12 13:18:20 +000027 memset( output, 0x00, 1000 );
Janos Follath8a49a012016-02-12 13:18:20 +000028
Hanno Becker6d43f9e2017-08-23 06:35:17 +010029 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
30 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
31 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
32 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( ( mod + 7 ) / 8 ) );
Janos Follath8a49a012016-02-12 13:18:20 +000033 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
34
Janos Follath8a49a012016-02-12 13:18:20 +000035
Azim Khand30ca132017-06-09 04:32:58 +010036 TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_buffer_rand, &info, MBEDTLS_RSA_PUBLIC, message_str->len, message_str->x, output ) == result );
Janos Follath8a49a012016-02-12 13:18:20 +000037 if( result == 0 )
38 {
Janos Follath8a49a012016-02-12 13:18:20 +000039
Azim Khand30ca132017-06-09 04:32:58 +010040 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Janos Follath8a49a012016-02-12 13:18:20 +000041 }
42
43exit:
Hanno Becker6d43f9e2017-08-23 06:35:17 +010044 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Janos Follath8a49a012016-02-12 13:18:20 +000045 mbedtls_rsa_free( &ctx );
46}
47/* END_CASE */
48
49/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010050void pkcs1_rsaes_v15_decrypt( int mod, int radix_P, char * input_P,
51 int radix_Q, char * input_Q, int radix_N,
52 char * input_N, int radix_E, char * input_E,
Azim Khand30ca132017-06-09 04:32:58 +010053 int hash, HexParam_t * result_hex_str,
54 char * seed, HexParam_t * message_str,
Azim Khanf1aaec92017-05-30 14:23:15 +010055 int result )
Janos Follath8a49a012016-02-12 13:18:20 +000056{
Janos Follath8a49a012016-02-12 13:18:20 +000057 unsigned char output[1000];
Janos Follath8a49a012016-02-12 13:18:20 +000058 mbedtls_rsa_context ctx;
Janos Follath8a49a012016-02-12 13:18:20 +000059 size_t output_len;
60 rnd_pseudo_info rnd_info;
Hanno Becker6d43f9e2017-08-23 06:35:17 +010061 mbedtls_mpi N, P, Q, E;
Janos Follath8a49a012016-02-12 13:18:20 +000062 ((void) seed);
63
Hanno Becker6d43f9e2017-08-23 06:35:17 +010064 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
65 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Janos Follath8a49a012016-02-12 13:18:20 +000066 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, hash );
67
Janos Follath8a49a012016-02-12 13:18:20 +000068 memset( output, 0x00, 1000 );
Janos Follath8a49a012016-02-12 13:18:20 +000069 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
70
Hanno Becker6d43f9e2017-08-23 06:35:17 +010071 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
72 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
73 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
74 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Janos Follath8a49a012016-02-12 13:18:20 +000075
Hanno Becker6d43f9e2017-08-23 06:35:17 +010076 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
77 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( ( mod + 7 ) / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +010078 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Janos Follath8a49a012016-02-12 13:18:20 +000079 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
80
Janos Follath8a49a012016-02-12 13:18:20 +000081
Azim Khand30ca132017-06-09 04:32:58 +010082 TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str->x, output, 1000 ) == result );
Janos Follath8a49a012016-02-12 13:18:20 +000083 if( result == 0 )
84 {
Janos Follath8a49a012016-02-12 13:18:20 +000085
Azim Khand30ca132017-06-09 04:32:58 +010086 TEST_ASSERT( hexcmp( output, result_hex_str->x, output_len, result_hex_str->len) == 0 );
Janos Follath8a49a012016-02-12 13:18:20 +000087 }
88
89exit:
Hanno Becker6d43f9e2017-08-23 06:35:17 +010090 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
91 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Janos Follath8a49a012016-02-12 13:18:20 +000092 mbedtls_rsa_free( &ctx );
93}
94/* END_CASE */
95
Janos Follathe6aef9f2016-03-16 16:39:41 +000096/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010097void pkcs1_rsassa_v15_sign( int mod, int radix_P, char * input_P, int radix_Q,
98 char * input_Q, int radix_N, char * input_N,
99 int radix_E, char * input_E, int digest, int hash,
Azim Khand30ca132017-06-09 04:32:58 +0100100 HexParam_t * message_str, HexParam_t * rnd_buf,
101 HexParam_t * result_hex_str, int result )
Janos Follathe6aef9f2016-03-16 16:39:41 +0000102{
Janos Follathe6aef9f2016-03-16 16:39:41 +0000103 unsigned char hash_result[1000];
104 unsigned char output[1000];
Janos Follathe6aef9f2016-03-16 16:39:41 +0000105 mbedtls_rsa_context ctx;
Hanno Becker6d43f9e2017-08-23 06:35:17 +0100106 mbedtls_mpi N, P, Q, E;
Janos Follathe6aef9f2016-03-16 16:39:41 +0000107 rnd_buf_info info;
108
Azim Khand30ca132017-06-09 04:32:58 +0100109 info.buf = rnd_buf->x;
110 info.length = rnd_buf->len;
Janos Follathe6aef9f2016-03-16 16:39:41 +0000111
Hanno Becker6d43f9e2017-08-23 06:35:17 +0100112 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
113 mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000114 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, hash );
115
Janos Follathe6aef9f2016-03-16 16:39:41 +0000116 memset( hash_result, 0x00, 1000 );
117 memset( output, 0x00, 1000 );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000118
Hanno Becker6d43f9e2017-08-23 06:35:17 +0100119 TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
120 TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
121 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
122 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000123
Hanno Becker6d43f9e2017-08-23 06:35:17 +0100124 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
125 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( ( mod + 7 ) / 8 ) );
Hanno Becker7f25f852017-10-10 16:56:22 +0100126 TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000127 TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
128
Janos Follathe6aef9f2016-03-16 16:39:41 +0000129
130 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100131 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000132
133 TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_buffer_rand, &info, MBEDTLS_RSA_PRIVATE, digest, 0, hash_result, output ) == result );
134 if( result == 0 )
135 {
Janos Follathe6aef9f2016-03-16 16:39:41 +0000136
Azim Khand30ca132017-06-09 04:32:58 +0100137 TEST_ASSERT( hexcmp( output, result_hex_str->x, ctx.len, result_hex_str->len ) == 0 );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000138 }
139
140exit:
Hanno Becker6d43f9e2017-08-23 06:35:17 +0100141 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
142 mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000143 mbedtls_rsa_free( &ctx );
144}
145/* END_CASE */
146
147/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100148void pkcs1_rsassa_v15_verify( int mod, int radix_N, char * input_N,
149 int radix_E, char * input_E, int digest,
Azim Khand30ca132017-06-09 04:32:58 +0100150 int hash, HexParam_t * message_str, char * salt,
151 HexParam_t * result_str, int result )
Janos Follathe6aef9f2016-03-16 16:39:41 +0000152{
Janos Follathe6aef9f2016-03-16 16:39:41 +0000153 unsigned char hash_result[1000];
Janos Follathe6aef9f2016-03-16 16:39:41 +0000154 mbedtls_rsa_context ctx;
Hanno Becker6d43f9e2017-08-23 06:35:17 +0100155 mbedtls_mpi N, E;
Janos Follathe6aef9f2016-03-16 16:39:41 +0000156 ((void) salt);
157
Hanno Becker6d43f9e2017-08-23 06:35:17 +0100158 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000159 mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, hash );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000160 memset( hash_result, 0x00, 1000 );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000161
Hanno Becker6d43f9e2017-08-23 06:35:17 +0100162 TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
163 TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
164 TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
165 TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( ( mod + 7 ) / 8 ) );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000166 TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
167
Janos Follathe6aef9f2016-03-16 16:39:41 +0000168
169 if( mbedtls_md_info_from_type( digest ) != NULL )
Azim Khand30ca132017-06-09 04:32:58 +0100170 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000171
Azim Khand30ca132017-06-09 04:32:58 +0100172 TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str->x ) == result );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000173
174exit:
Hanno Becker6d43f9e2017-08-23 06:35:17 +0100175 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
Janos Follathe6aef9f2016-03-16 16:39:41 +0000176 mbedtls_rsa_free( &ctx );
177}
178/* END_CASE */