Botan  2.9.0
Crypto and TLS for C++11
Public Member Functions | List of all members
Botan::ESP_Padding Class Referencefinal

#include <mode_pad.h>

Inheritance diagram for Botan::ESP_Padding:
Botan::BlockCipherModePaddingMethod

Public Member Functions

void add_padding (secure_vector< uint8_t > &buffer, size_t final_block_bytes, size_t block_size) const override
 
std::string name () const override
 
size_t unpad (const uint8_t[], size_t) const override
 
bool valid_blocksize (size_t bs) const override
 

Detailed Description

ESP Padding (RFC 4304)

Definition at line 118 of file mode_pad.h.

Member Function Documentation

◆ add_padding()

void Botan::ESP_Padding::add_padding ( secure_vector< uint8_t > &  buffer,
size_t  final_block_bytes,
size_t  block_size 
) const
overridevirtual

Add padding bytes to buffer.

Parameters
bufferdata to pad
final_block_bytessize of the final block in bytes
block_sizesize of each block in bytes

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 180 of file mode_pad.cpp.

183  {
184  uint8_t pad_value = 0x01;
185 
186  for(size_t i = last_byte_pos; i < block_size; ++i)
187  {
188  buffer.push_back(pad_value++);
189  }
190  }

◆ name()

std::string Botan::ESP_Padding::name ( ) const
inlineoverridevirtual
Returns
name of the mode

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 129 of file mode_pad.h.

129 { return "ESP"; }

◆ unpad()

size_t Botan::ESP_Padding::unpad ( const uint8_t  block[],
size_t  len 
) const
overridevirtual

Remove padding bytes from block

Parameters
blockthe last block
lenthe size of the block in bytes
Returns
number of data bytes, or if the padding is invalid returns len

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 195 of file mode_pad.cpp.

References Botan::CT::Mask< T >::is_equal(), Botan::CT::Mask< T >::is_gt(), Botan::CT::Mask< T >::is_zero(), Botan::CT::poison(), Botan::CT::unpoison(), and valid_blocksize().

196  {
197  if(!valid_blocksize(input_length))
198  return input_length;
199 
200  CT::poison(input, input_length);
201 
202  const uint8_t input_length_8 = static_cast<uint8_t>(input_length);
203  const uint8_t last_byte = input[input_length-1];
204 
205  auto bad_input = CT::Mask<uint8_t>::is_zero(last_byte) |
206  CT::Mask<uint8_t>::is_gt(last_byte, input_length_8);
207 
208  const uint8_t pad_pos = input_length_8 - last_byte;
209  size_t i = input_length_8 - 1;
210  while(i)
211  {
212  const auto in_range = CT::Mask<size_t>::is_gt(i, pad_pos);
213  const auto incrementing = CT::Mask<uint8_t>::is_equal(input[i-1], input[i]-1);
214 
215  bad_input |= CT::Mask<uint8_t>(in_range) & ~incrementing;
216  --i;
217  }
218 
219  CT::unpoison(input, input_length);
220  return bad_input.select_and_unpoison(input_length_8, pad_pos);
221  }
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:127
void poison(const T *p, size_t n)
Definition: ct_utils.h:48
void unpoison(const T *p, size_t n)
Definition: ct_utils.h:59
static Mask< T > is_zero(T x)
Definition: ct_utils.h:141
static Mask< T > is_equal(T x, T y)
Definition: ct_utils.h:149
static Mask< T > is_gt(T x, T y)
Definition: ct_utils.h:165

◆ valid_blocksize()

bool Botan::ESP_Padding::valid_blocksize ( size_t  block_size) const
inlineoverridevirtual
Parameters
block_sizeof the cipher
Returns
valid block size for this padding mode

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 127 of file mode_pad.h.

Referenced by unpad().

127 { return (bs > 2 && bs < 256); }

The documentation for this class was generated from the following files: