Make get_pkcs_padding() constant-time

This commit is contained in:
Manuel Pégourié-Gonnard 2013-10-27 17:21:14 +01:00
parent 6888167e73
commit f8ab069d6a
2 changed files with 75 additions and 12 deletions

View File

@ -406,7 +406,7 @@ static void add_pkcs_padding( unsigned char *output, size_t output_len,
size_t data_len )
{
size_t padding_len = output_len - data_len;
unsigned char i = 0;
unsigned char i;
for( i = 0; i < padding_len; i++ )
output[data_len + i] = (unsigned char) padding_len;
@ -415,23 +415,26 @@ static void add_pkcs_padding( unsigned char *output, size_t output_len,
static int get_pkcs_padding( unsigned char *input, size_t input_len,
size_t *data_len )
{
size_t i, padding_len = 0;
size_t i, pad_idx;
unsigned char padding_len, bad = 0;
if( NULL == input || NULL == data_len )
return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
padding_len = input[input_len - 1];
if( padding_len > input_len || padding_len == 0 )
return POLARSSL_ERR_CIPHER_INVALID_PADDING;
for( i = input_len - padding_len; i < input_len; i++ )
if( input[i] != padding_len )
return POLARSSL_ERR_CIPHER_INVALID_PADDING;
*data_len = input_len - padding_len;
return 0;
/* Avoid logical || since it results in a branch */
bad |= padding_len > input_len;
bad |= padding_len == 0;
/* The number of bytes checked must be independent of padding_len,
* so pick input_len, which is usually 8 or 16 (one block) */
pad_idx = input_len - padding_len;
for( i = 0; i < input_len; i++ )
bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
return POLARSSL_ERR_CIPHER_INVALID_PADDING * (bad != 0);
}
#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */

View File

@ -82,10 +82,70 @@ Check PKCS padding #6 (too few padding bytes)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD0002":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes)
Check PKCS padding #7 (non-uniform padding bytes #1)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00030203":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #2)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00030103":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #3)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00030703":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #4)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00030b03":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #5)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00031303":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #6)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00032303":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #7)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00034203":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #8)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00038303":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #9)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00020303":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #10)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00010303":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #11)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00070303":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #12)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD000b0303":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #13)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00130303":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #14)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00230303":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #15)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00420303":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #7 (non-uniform padding bytes #16)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"DABBAD00830303":POLARSSL_ERR_CIPHER_INVALID_PADDING:0
Check PKCS padding #8 (overlong)
depends_on:POLARSSL_CIPHER_PADDING_PKCS7
check_padding:POLARSSL_PADDING_PKCS7:"040404":POLARSSL_ERR_CIPHER_INVALID_PADDING:0