Skip to content

Commit 5b8f120

Browse files
Ron EldorRon Eldor
authored andcommitted
Reduce stack usage of test_suite_pkcs1_v21
Reduce the stack usage of the `test_suite_pkcs1_v21` by reducing the size of the buffers used in the tests, to a reasonable big enough size, and change the size sent to the API to sizeof output.
1 parent fdc15bd commit 5b8f120

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

tests/suites/test_suite_pkcs1_v21.function

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void pkcs1_rsaes_oaep_encrypt( int mod, int radix_N, char * input_N,
1414
data_t * message_str, data_t * rnd_buf,
1515
data_t * result_hex_str, int result )
1616
{
17-
unsigned char output[1000];
17+
unsigned char output[256];
1818
mbedtls_rsa_context ctx;
1919
rnd_buf_info info;
2020
mbedtls_mpi N, E;
@@ -24,7 +24,7 @@ void pkcs1_rsaes_oaep_encrypt( int mod, int radix_N, char * input_N,
2424

2525
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
2626
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, hash );
27-
memset( output, 0x00, 1000 );
27+
memset( output, 0x00, sizeof( output ) );
2828

2929
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
3030
TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
@@ -54,7 +54,7 @@ void pkcs1_rsaes_oaep_decrypt( int mod, int radix_P, char * input_P,
5454
char * seed, data_t * message_str,
5555
int result )
5656
{
57-
unsigned char output[1000];
57+
unsigned char output[64];
5858
mbedtls_rsa_context ctx;
5959
size_t output_len;
6060
rnd_pseudo_info rnd_info;
@@ -66,7 +66,7 @@ void pkcs1_rsaes_oaep_decrypt( int mod, int radix_P, char * input_P,
6666

6767
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, hash );
6868

69-
memset( output, 0x00, 1000 );
69+
memset( output, 0x00, sizeof( output ) );
7070
memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
7171

7272
TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
@@ -81,11 +81,16 @@ void pkcs1_rsaes_oaep_decrypt( int mod, int radix_P, char * input_P,
8181

8282
if( result_hex_str->len == 0 )
8383
{
84-
TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str->x, NULL, 0 ) == result );
84+
TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
85+
MBEDTLS_RSA_PRIVATE, &output_len,
86+
message_str->x, NULL, 0 ) == result );
8587
}
8688
else
8789
{
88-
TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str->x, output, 1000 ) == result );
90+
TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
91+
MBEDTLS_RSA_PRIVATE, &output_len,
92+
message_str->x, output,
93+
sizeof( output ) ) == result );
8994
if( result == 0 )
9095
{
9196
TEST_ASSERT( hexcmp( output, result_hex_str->x, output_len, result_hex_str->len ) == 0 );
@@ -106,8 +111,8 @@ void pkcs1_rsassa_pss_sign( int mod, int radix_P, char * input_P, int radix_Q,
106111
data_t * message_str, data_t * rnd_buf,
107112
data_t * result_hex_str, int result )
108113
{
109-
unsigned char hash_result[1000];
110-
unsigned char output[1000];
114+
unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
115+
unsigned char output[256];
111116
mbedtls_rsa_context ctx;
112117
rnd_buf_info info;
113118
mbedtls_mpi N, P, Q, E;
@@ -119,8 +124,8 @@ void pkcs1_rsassa_pss_sign( int mod, int radix_P, char * input_P, int radix_Q,
119124
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
120125
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, hash );
121126

122-
memset( hash_result, 0x00, 1000 );
123-
memset( output, 0x00, 1000 );
127+
memset( hash_result, 0x00, sizeof( hash_result ) );
128+
memset( output, 0x00, sizeof( output ) );
124129

125130
TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
126131
TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
@@ -157,14 +162,14 @@ void pkcs1_rsassa_pss_verify( int mod, int radix_N, char * input_N,
157162
int hash, data_t * message_str, char * salt,
158163
data_t * result_str, int result )
159164
{
160-
unsigned char hash_result[1000];
165+
unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
161166
mbedtls_rsa_context ctx;
162167
mbedtls_mpi N, E;
163168
((void) salt);
164169

165170
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
166171
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, hash );
167-
memset( hash_result, 0x00, 1000 );
172+
memset( hash_result, 0x00, sizeof( hash_result ) );
168173

169174
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
170175
TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
@@ -194,14 +199,14 @@ void pkcs1_rsassa_pss_verify_ext( int mod, int radix_N, char * input_N,
194199
data_t * result_str, int result_simple,
195200
int result_full )
196201
{
197-
unsigned char hash_result[1000];
202+
unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
198203
mbedtls_rsa_context ctx;
199204
size_t hash_len;
200205
mbedtls_mpi N, E;
201206

202207
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
203208
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, ctx_hash );
204-
memset( hash_result, 0x00, 1000 );
209+
memset( hash_result, 0x00, sizeof( hash_result ) );
205210

206211
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
207212
TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );

0 commit comments

Comments
 (0)