From 3ec773cd2b9d32c5a4bb3a7a6ae86e49fd278c8f Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Sun, 18 Mar 2012 20:02:25 +0000 Subject: [crypto] Force caller to provide temporary storage for modular calculations bigint_mod_multiply() and bigint_mod_exp() require a fixed amount of temporary storage for intermediate results. (The amount of temporary storage required depends upon the size of the integers involved.) When performing calculations for 4096-bit RSA the amount of temporary storage space required will exceed 2.5kB, which is too much to allocate on the stack. Avoid this problem by forcing the caller to allocate temporary storage. Signed-off-by: Michael Brown --- src/tests/bigint_test.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'src/tests') diff --git a/src/tests/bigint_test.c b/src/tests/bigint_test.c index 8c9f188ed..4052131fd 100644 --- a/src/tests/bigint_test.c +++ b/src/tests/bigint_test.c @@ -162,7 +162,8 @@ void bigint_mod_multiply_sample ( const bigint_element_t *multiplicand0, const bigint_element_t *multiplier0, const bigint_element_t *modulus0, bigint_element_t *result0, - unsigned int size ) { + unsigned int size, + void *tmp ) { const bigint_t ( size ) *multiplicand __attribute__ (( may_alias )) = ( ( const void * ) multiplicand0 ); const bigint_t ( size ) *multiplier __attribute__ (( may_alias )) @@ -172,14 +173,15 @@ void bigint_mod_multiply_sample ( const bigint_element_t *multiplicand0, bigint_t ( size ) *result __attribute__ (( may_alias )) = ( ( void * ) result0 ); - bigint_mod_multiply ( multiplicand, multiplier, modulus, result ); + bigint_mod_multiply ( multiplicand, multiplier, modulus, result, tmp ); } void bigint_mod_exp_sample ( const bigint_element_t *base0, const bigint_element_t *modulus0, const bigint_element_t *exponent0, bigint_element_t *result0, - unsigned int size, unsigned int exponent_size ) { + unsigned int size, unsigned int exponent_size, + void *tmp ) { const bigint_t ( size ) *base __attribute__ (( may_alias )) = ( ( const void * ) base0 ); const bigint_t ( size ) *modulus __attribute__ (( may_alias )) @@ -189,7 +191,7 @@ void bigint_mod_exp_sample ( const bigint_element_t *base0, bigint_t ( size ) *result __attribute__ (( may_alias )) = ( ( void * ) result0 ); - bigint_mod_exp ( base, modulus, exponent, result ); + bigint_mod_exp ( base, modulus, exponent, result, tmp ); } /** @@ -471,6 +473,8 @@ void bigint_mod_exp_sample ( const bigint_element_t *base0, bigint_t ( size ) multiplier_temp; \ bigint_t ( size ) modulus_temp; \ bigint_t ( size ) result_temp; \ + size_t tmp_len = bigint_mod_multiply_tmp_len ( &modulus_temp ); \ + uint8_t tmp[tmp_len]; \ {} /* Fix emacs alignment */ \ \ assert ( bigint_size ( &multiplier_temp ) == \ @@ -490,7 +494,7 @@ void bigint_mod_exp_sample ( const bigint_element_t *base0, DBG_HDA ( 0, &multiplier_temp, sizeof ( multiplier_temp ) ); \ DBG_HDA ( 0, &modulus_temp, sizeof ( modulus_temp ) ); \ bigint_mod_multiply ( &multiplicand_temp, &multiplier_temp, \ - &modulus_temp, &result_temp ); \ + &modulus_temp, &result_temp, tmp ); \ DBG_HDA ( 0, &result_temp, sizeof ( result_temp ) ); \ bigint_done ( &result_temp, result_raw, sizeof ( result_raw ) );\ \ @@ -520,6 +524,9 @@ void bigint_mod_exp_sample ( const bigint_element_t *base0, bigint_t ( size ) modulus_temp; \ bigint_t ( exponent_size ) exponent_temp; \ bigint_t ( size ) result_temp; \ + size_t tmp_len = bigint_mod_exp_tmp_len ( &modulus_temp, \ + &exponent_temp ); \ + uint8_t tmp[tmp_len]; \ {} /* Fix emacs alignment */ \ \ assert ( bigint_size ( &modulus_temp ) == \ @@ -536,7 +543,7 @@ void bigint_mod_exp_sample ( const bigint_element_t *base0, DBG_HDA ( 0, &modulus_temp, sizeof ( modulus_temp ) ); \ DBG_HDA ( 0, &exponent_temp, sizeof ( exponent_temp ) ); \ bigint_mod_exp ( &base_temp, &modulus_temp, &exponent_temp, \ - &result_temp ); \ + &result_temp, tmp ); \ DBG_HDA ( 0, &result_temp, sizeof ( result_temp ) ); \ bigint_done ( &result_temp, result_raw, sizeof ( result_raw ) );\ \ -- cgit v1.2.3-55-g7522