diff --git a/library/cipher.c b/library/cipher.c index cbbdcd510..2941f40f3 100644 --- a/library/cipher.c +++ b/library/cipher.c @@ -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; }