summaryrefslogtreecommitdiffstats
path: root/src/tests/bigint_test.c
diff options
context:
space:
mode:
authorMichael Brown2012-03-18 21:02:25 +0100
committerMichael Brown2012-03-18 21:22:43 +0100
commit3ec773cd2b9d32c5a4bb3a7a6ae86e49fd278c8f (patch)
tree743d5b133e4469d9644319fd54f81c06822711c5 /src/tests/bigint_test.c
parent[rng] Add Linux entropy source using /dev/random (diff)
downloadipxe-3ec773cd2b9d32c5a4bb3a7a6ae86e49fd278c8f.tar.gz
ipxe-3ec773cd2b9d32c5a4bb3a7a6ae86e49fd278c8f.tar.xz
ipxe-3ec773cd2b9d32c5a4bb3a7a6ae86e49fd278c8f.zip
[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 <mcb30@ipxe.org>
Diffstat (limited to 'src/tests/bigint_test.c')
-rw-r--r--src/tests/bigint_test.c19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/tests/bigint_test.c b/src/tests/bigint_test.c
index 8c9f188e..4052131f 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 ) );\
\