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

#include <mode_pad.h>

Inheritance diagram for Botan::PKCS7_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

PKCS#7 Padding

Definition at line 67 of file mode_pad.h.

Member Function Documentation

◆ add_padding()

void Botan::PKCS7_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 41 of file mode_pad.cpp.

44  {
45  const uint8_t pad_value = static_cast<uint8_t>(block_size - last_byte_pos);
46 
47  for(size_t i = 0; i != pad_value; ++i)
48  buffer.push_back(pad_value);
49  }

◆ name()

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

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 78 of file mode_pad.h.

78 { return "PKCS7"; }

◆ unpad()

size_t Botan::PKCS7_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 54 of file mode_pad.cpp.

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

55  {
56  if(!valid_blocksize(input_length))
57  return input_length;
58 
59  CT::poison(input, input_length);
60 
61  const uint8_t last_byte = input[input_length-1];
62 
63  /*
64  The input should == the block size so if the last byte exceeds
65  that then the padding is certainly invalid
66  */
67  auto bad_input = CT::Mask<size_t>::is_gt(last_byte, input_length);
68 
69  const size_t pad_pos = input_length - last_byte;
70 
71  for(size_t i = 0; i != input_length - 1; ++i)
72  {
73  // Does this byte equal the expected pad byte?
74  const auto pad_eq = CT::Mask<size_t>::is_equal(input[i], last_byte);
75 
76  // Ignore values that are not part of the padding
77  const auto in_range = CT::Mask<size_t>::is_gte(i, pad_pos);
78  bad_input |= in_range & (~pad_eq);
79  }
80 
81  CT::unpoison(input, input_length);
82 
83  return bad_input.select_and_unpoison(input_length, pad_pos);
84  }
static Mask< T > is_gte(T x, T y)
Definition: ct_utils.h:181
void poison(const T *p, size_t n)
Definition: ct_utils.h:48
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:76
void unpoison(const T *p, size_t n)
Definition: ct_utils.h:59
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::PKCS7_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 76 of file mode_pad.h.

Referenced by unpad().

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

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