[crypto] Expose shifted out bit from big integer shifts

Expose the bit shifted out as a result of shifting a big integer left
or right.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2025-02-13 14:18:15 +00:00
parent bd90abf487
commit 5056e8ad93
7 changed files with 146 additions and 85 deletions

View File

@@ -78,18 +78,18 @@ int bigint_subtract_sample ( const bigint_element_t *subtrahend0,
return bigint_subtract ( subtrahend, value );
}
void bigint_shl_sample ( bigint_element_t *value0, unsigned int size ) {
int bigint_shl_sample ( bigint_element_t *value0, unsigned int size ) {
bigint_t ( size ) *value __attribute__ (( may_alias ))
= ( ( void * ) value0 );
bigint_shl ( value );
return bigint_shl ( value );
}
void bigint_shr_sample ( bigint_element_t *value0, unsigned int size ) {
int bigint_shr_sample ( bigint_element_t *value0, unsigned int size ) {
bigint_t ( size ) *value __attribute__ (( may_alias ))
= ( ( void * ) value0 );
bigint_shr ( value );
return bigint_shr ( value );
}
int bigint_is_zero_sample ( const bigint_element_t *value0,
@@ -321,25 +321,31 @@ void bigint_mod_exp_sample ( const bigint_element_t *base0,
*
* @v value Big integer
* @v expected Big integer expected result
* @v bit Expected bit shifted out
*/
#define bigint_shl_ok( value, expected ) do { \
#define bigint_shl_ok( value, expected, bit ) do { \
static const uint8_t value_raw[] = value; \
static const uint8_t expected_raw[] = expected; \
uint8_t result_raw[ sizeof ( expected_raw ) ]; \
unsigned int size = \
bigint_required_size ( sizeof ( value_raw ) ); \
unsigned int msb = ( 8 * sizeof ( value_raw ) ); \
bigint_t ( size ) value_temp; \
int out; \
{} /* Fix emacs alignment */ \
\
bigint_init ( &value_temp, value_raw, sizeof ( value_raw ) ); \
DBG ( "Shift left:\n" ); \
DBG_HDA ( 0, &value_temp, sizeof ( value_temp ) ); \
bigint_shl ( &value_temp ); \
out = bigint_shl ( &value_temp ); \
DBG_HDA ( 0, &value_temp, sizeof ( value_temp ) ); \
bigint_done ( &value_temp, result_raw, sizeof ( result_raw ) ); \
\
ok ( memcmp ( result_raw, expected_raw, \
sizeof ( result_raw ) ) == 0 ); \
if ( sizeof ( result_raw ) < sizeof ( value_temp ) ) \
out += bigint_bit_is_set ( &value_temp, msb ); \
ok ( out == bit ); \
} while ( 0 )
/**
@@ -347,25 +353,28 @@ void bigint_mod_exp_sample ( const bigint_element_t *base0,
*
* @v value Big integer
* @v expected Big integer expected result
* @v bit Expected bit shifted out
*/
#define bigint_shr_ok( value, expected ) do { \
#define bigint_shr_ok( value, expected, bit ) do { \
static const uint8_t value_raw[] = value; \
static const uint8_t expected_raw[] = expected; \
uint8_t result_raw[ sizeof ( expected_raw ) ]; \
unsigned int size = \
bigint_required_size ( sizeof ( value_raw ) ); \
bigint_t ( size ) value_temp; \
int out; \
{} /* Fix emacs alignment */ \
\
bigint_init ( &value_temp, value_raw, sizeof ( value_raw ) ); \
DBG ( "Shift right:\n" ); \
DBG_HDA ( 0, &value_temp, sizeof ( value_temp ) ); \
bigint_shr ( &value_temp ); \
out = bigint_shr ( &value_temp ); \
DBG_HDA ( 0, &value_temp, sizeof ( value_temp ) ); \
bigint_done ( &value_temp, result_raw, sizeof ( result_raw ) ); \
\
ok ( memcmp ( result_raw, expected_raw, \
sizeof ( result_raw ) ) == 0 ); \
ok ( out == bit ); \
} while ( 0 )
/**
@@ -896,13 +905,13 @@ static void bigint_test_exec ( void ) {
0x7f, 0xcb, 0x94, 0x31, 0x1d, 0xbc,
0x44, 0x1a ), 0 );
bigint_shl_ok ( BIGINT ( 0xe0 ),
BIGINT ( 0xc0 ) );
BIGINT ( 0xc0 ), 1 );
bigint_shl_ok ( BIGINT ( 0x43, 0x1d ),
BIGINT ( 0x86, 0x3a ) );
BIGINT ( 0x86, 0x3a ), 0 );
bigint_shl_ok ( BIGINT ( 0xac, 0xed, 0x9b ),
BIGINT ( 0x59, 0xdb, 0x36 ) );
BIGINT ( 0x59, 0xdb, 0x36 ), 1 );
bigint_shl_ok ( BIGINT ( 0x2c, 0xe8, 0x3a, 0x22 ),
BIGINT ( 0x59, 0xd0, 0x74, 0x44 ) );
BIGINT ( 0x59, 0xd0, 0x74, 0x44 ), 0 );
bigint_shl_ok ( BIGINT ( 0x4e, 0x88, 0x4a, 0x05, 0x5e, 0x10, 0xee,
0x5b, 0xc6, 0x40, 0x0e, 0x03, 0xd7, 0x0d,
0x28, 0xa5, 0x55, 0xb2, 0x50, 0xef, 0x69,
@@ -910,7 +919,19 @@ static void bigint_test_exec ( void ) {
BIGINT ( 0x9d, 0x10, 0x94, 0x0a, 0xbc, 0x21, 0xdc,
0xb7, 0x8c, 0x80, 0x1c, 0x07, 0xae, 0x1a,
0x51, 0x4a, 0xab, 0x64, 0xa1, 0xde, 0xd3,
0xa2, 0x3a ) );
0xa2, 0x3a ), 0 );
bigint_shl_ok ( BIGINT ( 0x84, 0x56, 0xaa, 0xb4, 0x23, 0xd4, 0x4e,
0xea, 0x92, 0x34, 0x61, 0x11, 0x3e, 0x38,
0x31, 0x8b ),
BIGINT ( 0x08, 0xad, 0x55, 0x68, 0x47, 0xa8, 0x9d,
0xd5, 0x24, 0x68, 0xc2, 0x22, 0x7c, 0x70,
0x63, 0x16 ), 1 );
bigint_shl_ok ( BIGINT ( 0x4a, 0x2b, 0x6b, 0x7c, 0xbf, 0x8a, 0x43,
0x71, 0x96, 0xd6, 0x2f, 0x14, 0xa0, 0x2a,
0xf8, 0x15 ),
BIGINT ( 0x94, 0x56, 0xd6, 0xf9, 0x7f, 0x14, 0x86,
0xe3, 0x2d, 0xac, 0x5e, 0x29, 0x40, 0x55,
0xf0, 0x2a ), 0 );
bigint_shl_ok ( BIGINT ( 0x07, 0x62, 0x78, 0x70, 0x2e, 0xd4, 0x41,
0xaa, 0x9b, 0x50, 0xb1, 0x9a, 0x71, 0xf5,
0x1c, 0x2f, 0xe7, 0x0d, 0xf1, 0x46, 0x57,
@@ -948,15 +969,15 @@ static void bigint_test_exec ( void ) {
0x10, 0xee, 0x09, 0xea, 0x09, 0x9a, 0xd6,
0x49, 0x7c, 0x1e, 0xdb, 0xc7, 0x65, 0xa6,
0x0e, 0xd1, 0xd2, 0x00, 0xb3, 0x41, 0xc9,
0x3c, 0xbc ) );
0x3c, 0xbc ), 0 );
bigint_shr_ok ( BIGINT ( 0x8f ),
BIGINT ( 0x47 ) );
BIGINT ( 0x47 ), 1 );
bigint_shr_ok ( BIGINT ( 0xaa, 0x1d ),
BIGINT ( 0x55, 0x0e ) );
BIGINT ( 0x55, 0x0e ), 1 );
bigint_shr_ok ( BIGINT ( 0xf0, 0xbd, 0x68 ),
BIGINT ( 0x78, 0x5e, 0xb4 ) );
BIGINT ( 0x78, 0x5e, 0xb4 ), 0 );
bigint_shr_ok ( BIGINT ( 0x33, 0xa0, 0x3d, 0x95 ),
BIGINT ( 0x19, 0xd0, 0x1e, 0xca ) );
BIGINT ( 0x19, 0xd0, 0x1e, 0xca ), 1 );
bigint_shr_ok ( BIGINT ( 0xa1, 0xf4, 0xb9, 0x64, 0x91, 0x99, 0xa1,
0xf4, 0xae, 0xeb, 0x71, 0x97, 0x1b, 0x71,
0x09, 0x38, 0x3f, 0x8f, 0xc5, 0x3a, 0xb9,
@@ -964,7 +985,19 @@ static void bigint_test_exec ( void ) {
BIGINT ( 0x50, 0xfa, 0x5c, 0xb2, 0x48, 0xcc, 0xd0,
0xfa, 0x57, 0x75, 0xb8, 0xcb, 0x8d, 0xb8,
0x84, 0x9c, 0x1f, 0xc7, 0xe2, 0x9d, 0x5c,
0xba, 0xca ) );
0xba, 0xca ), 0 );
bigint_shr_ok ( BIGINT ( 0xa4, 0x19, 0xbb, 0x93, 0x0b, 0x3f, 0x47,
0x5b, 0xb4, 0xb5, 0x7d, 0x75, 0x42, 0x22,
0xcc, 0xdf ),
BIGINT ( 0x52, 0x0c, 0xdd, 0xc9, 0x85, 0x9f, 0xa3,
0xad, 0xda, 0x5a, 0xbe, 0xba, 0xa1, 0x11,
0x66, 0x6f ), 1 );
bigint_shr_ok ( BIGINT ( 0x27, 0x7e, 0x8f, 0x60, 0x40, 0x93, 0x43,
0xd6, 0x89, 0x3e, 0x40, 0x61, 0x9a, 0x04,
0x4c, 0x02 ),
BIGINT ( 0x13, 0xbf, 0x47, 0xb0, 0x20, 0x49, 0xa1,
0xeb, 0x44, 0x9f, 0x20, 0x30, 0xcd, 0x02,
0x26, 0x01 ), 0 );
bigint_shr_ok ( BIGINT ( 0xc0, 0xb3, 0x78, 0x46, 0x69, 0x6e, 0x35,
0x94, 0xed, 0x28, 0xdc, 0xfd, 0xf6, 0xdb,
0x2d, 0x24, 0xcb, 0xa4, 0x6f, 0x0e, 0x58,
@@ -1002,7 +1035,7 @@ static void bigint_test_exec ( void ) {
0x10, 0x64, 0x55, 0x07, 0xec, 0xa8, 0xc7,
0x46, 0xa8, 0x94, 0xb0, 0xf7, 0xa4, 0x57,
0x1f, 0x72, 0x88, 0x5f, 0xed, 0x4d, 0xc9,
0x59, 0xbb ) );
0x59, 0xbb ), 1 );
bigint_is_zero_ok ( BIGINT ( 0x9b ),
0 );
bigint_is_zero_ok ( BIGINT ( 0x5a, 0x9d ),