[crypto] Add bigint_swap() to conditionally swap big integers

Add a helper function bigint_swap() that can be used to conditionally
swap a pair of big integers in constant time.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2024-01-19 12:34:02 +00:00
parent 13e390d54e
commit bac13ba1f6
3 changed files with 94 additions and 0 deletions

View File

@@ -159,6 +159,16 @@ void bigint_copy_sample ( const bigint_element_t *source0,
bigint_copy ( source, dest );
}
void bigint_swap_sample ( bigint_element_t *first0, bigint_element_t *second0,
unsigned int size, int swap ) {
bigint_t ( size ) *first __attribute__ (( may_alias ))
= ( ( void * ) first0 );
bigint_t ( size ) *second __attribute__ (( may_alias ))
= ( ( void * ) second0 );
bigint_swap ( first, second, swap );
}
void bigint_multiply_sample ( const bigint_element_t *multiplicand0,
unsigned int multiplicand_size,
const bigint_element_t *multiplier0,
@@ -430,6 +440,42 @@ void bigint_mod_exp_sample ( const bigint_element_t *base0,
ok ( max_set_bit == (expected) ); \
} while ( 0 )
/**
* Report result of big integer swap test
*
* @v first Big integer to be conditionally swapped
* @v second Big integer to be conditionally swapped
*/
#define bigint_swap_ok( first, second ) do { \
static const uint8_t first_raw[] = first; \
static const uint8_t second_raw[] = second; \
uint8_t temp[ sizeof ( first_raw ) ]; \
unsigned int size = bigint_required_size ( sizeof ( temp) ); \
bigint_t ( size ) first_temp; \
bigint_t ( size ) second_temp; \
{} /* Fix emacs alignment */ \
\
assert ( sizeof ( first_raw ) == sizeof ( temp ) ); \
assert ( sizeof ( second_raw ) == sizeof ( temp ) ); \
bigint_init ( &first_temp, first_raw, sizeof ( first_raw ) ); \
bigint_init ( &second_temp, second_raw, sizeof ( second_raw ) );\
bigint_swap ( &first_temp, &second_temp, 0 ); \
bigint_done ( &first_temp, temp, sizeof ( temp ) ); \
ok ( memcmp ( temp, first_raw, sizeof ( temp ) ) == 0 ); \
bigint_done ( &second_temp, temp, sizeof ( temp ) ); \
ok ( memcmp ( temp, second_raw, sizeof ( temp ) ) == 0 ); \
bigint_swap ( &first_temp, &second_temp, 1 ); \
bigint_done ( &first_temp, temp, sizeof ( temp ) ); \
ok ( memcmp ( temp, second_raw, sizeof ( temp ) ) == 0 ); \
bigint_done ( &second_temp, temp, sizeof ( temp ) ); \
ok ( memcmp ( temp, first_raw, sizeof ( temp ) ) == 0 ); \
bigint_swap ( &first_temp, &second_temp, 1 ); \
bigint_done ( &first_temp, temp, sizeof ( temp ) ); \
ok ( memcmp ( temp, first_raw, sizeof ( temp ) ) == 0 ); \
bigint_done ( &second_temp, temp, sizeof ( temp ) ); \
ok ( memcmp ( temp, second_raw, sizeof ( temp ) ) == 0 ); \
} while ( 0 )
/**
* Report result of big integer multiplication test
*
@@ -1373,6 +1419,14 @@ static void bigint_test_exec ( void ) {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff ),
1024 );
bigint_swap_ok ( BIGINT ( 0x68, 0x65, 0x6c, 0x6c, 0x6f ),
BIGINT ( 0x77, 0x6f, 0x72, 0x6c, 0x64 ) );
bigint_swap_ok ( BIGINT ( 0xc8, 0x1c, 0x31, 0xd7, 0x13, 0x69, 0x47,
0x32, 0xb0, 0x0a, 0xf7, 0x2d, 0xb9, 0xc3,
0x35, 0x96 ),
BIGINT ( 0x8b, 0x1d, 0x8f, 0x21, 0x76, 0x16, 0x4c,
0xf8, 0xb2, 0x63, 0xed, 0x89, 0x5e, 0x6b,
0x35, 0x7c ) );
bigint_multiply_ok ( BIGINT ( 0xf0 ),
BIGINT ( 0xeb ),
BIGINT ( 0xdc, 0x50 ) );