Make get_zeros_padding() constant-time

This commit is contained in:
Manuel Pégourié-Gonnard 2013-10-27 18:26:39 +01:00
parent 6c32990114
commit e68bf171eb

View File

@ -533,14 +533,19 @@ static void add_zeros_padding( unsigned char *output,
static int get_zeros_padding( unsigned char *input, size_t input_len,
size_t *data_len )
{
unsigned char *p = input + input_len - 1;
size_t i;
unsigned char done = 0, prev_done;
if( NULL == input || NULL == data_len )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
while( *p == 0x00 && p > input )
--p;
*data_len = *p == 0x00 ? 0 : p - input + 1;
*data_len = 0;
for( i = input_len; i > 0; i-- )
{
prev_done = done;
done |= ( input[i-1] != 0 );
*data_len |= i * ( done != prev_done );
}
return 0;
}