From 5d811c9d88f1be9caa384d85d5c56c1d90021864 Mon Sep 17 00:00:00 2001 From: René Korthaus Date: Tue, 15 Nov 2016 11:12:28 +0100 Subject: Check all padding bytes in ESP_Padding::unpad() --- src/lib/modes/mode_pad/mode_pad.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/lib/modes') diff --git a/src/lib/modes/mode_pad/mode_pad.cpp b/src/lib/modes/mode_pad/mode_pad.cpp index d4ab914cc..eb4ae42be 100644 --- a/src/lib/modes/mode_pad/mode_pad.cpp +++ b/src/lib/modes/mode_pad/mode_pad.cpp @@ -145,22 +145,27 @@ void ESP_Padding::add_padding(secure_vector& buffer, */ size_t ESP_Padding::unpad(const byte block[], size_t size) const { - byte last_byte = block[size-1]; + const byte last_byte = block[size-1]; if(last_byte > size) { throw Decoding_Error(name()); } + // try to do this in const time by looping over the entire block + const size_t pad_pos = size - last_byte; size_t i = size - 1; - while(i > size - last_byte) + while(i) { if(block[i-1] != block[i]-1) { - throw Decoding_Error(name()); + if(i > pad_pos) + { + throw Decoding_Error(name()); + } } --i; } - return i; + return pad_pos; } -- cgit v1.2.3