9#include <botan/internal/mode_pad.h>
11#include <botan/exceptn.h>
12#include <botan/internal/ct_utils.h>
20 if(algo_spec ==
"NoPadding") {
21 return std::make_unique<Null_Padding>();
24 if(algo_spec ==
"PKCS7") {
25 return std::make_unique<PKCS7_Padding>();
28 if(algo_spec ==
"OneAndZeros") {
29 return std::make_unique<OneAndZeros_Padding>();
32 if(algo_spec ==
"X9.23") {
33 return std::make_unique<ANSI_X923_Padding>();
36 if(algo_spec ==
"ESP") {
37 return std::make_unique<ESP_Padding>();
56 const uint8_t padding_len =
static_cast<uint8_t
>(BS - last_byte_pos);
58 buffer.resize(buffer.size() + padding_len);
66 const size_t start_of_last_block = buffer.size() - BS;
67 const size_t end_of_last_block = buffer.size();
68 const size_t start_of_padding = buffer.size() - padding_len;
70 for(
size_t i = start_of_last_block; i != end_of_last_block; ++i) {
72 buffer[i] = needs_padding.select(padding_len, buffer[i]);
89 const uint8_t last_byte = input[input_length - 1];
97 const size_t pad_pos = input_length - last_byte;
99 for(
size_t i = 0; i != input_length - 1; ++i) {
105 bad_input |= in_range & (~pad_eq);
110 return bad_input.select_and_unpoison(input_length, pad_pos);
126 const uint8_t padding_len =
static_cast<uint8_t
>(BS - last_byte_pos);
128 buffer.resize(buffer.size() + padding_len);
136 const size_t start_of_last_block = buffer.size() - BS;
137 const size_t end_of_zero_padding = buffer.size() - 1;
138 const size_t start_of_padding = buffer.size() - padding_len;
140 for(
size_t i = start_of_last_block; i != end_of_zero_padding; ++i) {
142 buffer[i] = needs_padding.select(0, buffer[i]);
145 buffer[buffer.size() - 1] = padding_len;
160 const size_t last_byte = input[input_length - 1];
164 const size_t pad_pos = input_length - last_byte;
166 for(
size_t i = 0; i != input_length - 1; ++i) {
170 bad_input |= pad_is_nonzero & in_range;
175 return bad_input.select_and_unpoison(input_length, pad_pos);
192 const uint8_t padding_len =
static_cast<uint8_t
>(BS - last_byte_pos);
194 buffer.resize(buffer.size() + padding_len);
202 const size_t start_of_last_block = buffer.size() - BS;
203 const size_t end_of_last_block = buffer.size();
204 const size_t start_of_padding = buffer.size() - padding_len;
206 for(
size_t i = start_of_last_block; i != end_of_last_block; ++i) {
209 buffer[i] = needs_00.select(0x00, needs_80.select(0x80, buffer[i]));
229 size_t pad_pos = input_length - 1;
230 size_t i = input_length;
236 seen_0x80 |= is_0x80;
237 pad_pos -= seen_0x80.if_not_set_return(1);
238 bad_input |= ~seen_0x80 & ~is_zero;
241 bad_input |= ~seen_0x80;
261 const uint8_t padding_len =
static_cast<uint8_t
>(BS - last_byte_pos);
263 buffer.resize(buffer.size() + padding_len);
271 const size_t start_of_last_block = buffer.size() - BS;
272 const size_t end_of_last_block = buffer.size();
273 const size_t start_of_padding = buffer.size() - padding_len;
275 uint8_t pad_ctr = 0x01;
277 for(
size_t i = start_of_last_block; i != end_of_last_block; ++i) {
279 buffer[i] = needs_padding.select(pad_ctr, buffer[i]);
280 pad_ctr = needs_padding.select(pad_ctr + 1, pad_ctr);
297 const uint8_t input_length_8 =
static_cast<uint8_t
>(input_length);
298 const uint8_t last_byte = input[input_length - 1];
302 const uint8_t pad_pos = input_length_8 - last_byte;
303 size_t i = input_length_8 - 1;
313 return bad_input.select_and_unpoison(input_length_8, pad_pos);
#define BOTAN_DEBUG_ASSERT(expr)
void add_padding(secure_vector< uint8_t > &buffer, size_t final_block_bytes, size_t block_size) const override
size_t unpad(const uint8_t[], size_t) const override
bool valid_blocksize(size_t bs) const override
static std::unique_ptr< BlockCipherModePaddingMethod > create(std::string_view algo_spec)
static constexpr Mask< T > is_gte(T x, T y)
static constexpr Mask< T > expand(T v)
static constexpr Mask< T > is_equal(T x, T y)
static constexpr Mask< T > is_gt(T x, T y)
static constexpr Mask< T > is_zero(T x)
static constexpr Mask< T > cleared()
bool valid_blocksize(size_t bs) const override
size_t unpad(const uint8_t[], size_t) const override
void add_padding(secure_vector< uint8_t > &buffer, size_t final_block_bytes, size_t block_size) const override
size_t unpad(const uint8_t[], size_t) const override
void add_padding(secure_vector< uint8_t > &buffer, size_t final_block_bytes, size_t block_size) const override
bool valid_blocksize(size_t bs) const override
void add_padding(secure_vector< uint8_t > &buffer, size_t final_block_bytes, size_t block_size) const override
bool valid_blocksize(size_t bs) const override
size_t unpad(const uint8_t[], size_t) const override
constexpr void unpoison(const T *p, size_t n)
constexpr void poison(const T *p, size_t n)
std::vector< T, secure_allocator< T > > secure_vector