Botan  2.15.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 69 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.

References BOTAN_DEBUG_ASSERT, Botan::CT::Mask< T >::is_gte(), Botan::CT::poison(), Botan::CT::Mask< T >::select(), and Botan::CT::unpoison().

44  {
45  /*
46  Padding format is
47  01
48  0202
49  030303
50  ...
51  */
52  BOTAN_DEBUG_ASSERT(last_byte_pos < BS);
53 
54  const uint8_t padding_len = static_cast<uint8_t>(BS - last_byte_pos);
55 
56  buffer.resize(buffer.size() + padding_len);
57 
58  CT::poison(&last_byte_pos, 1);
59  CT::poison(buffer.data(), buffer.size());
60 
61  BOTAN_DEBUG_ASSERT(buffer.size() % BS == 0);
62  BOTAN_DEBUG_ASSERT(buffer.size() >= BS);
63 
64  const size_t start_of_last_block = buffer.size() - BS;
65  const size_t end_of_last_block = buffer.size();
66  const size_t start_of_padding = buffer.size() - padding_len;
67 
68  for(size_t i = start_of_last_block; i != end_of_last_block; ++i)
69  {
70  auto needs_padding = CT::Mask<uint8_t>(CT::Mask<size_t>::is_gte(i, start_of_padding));
71  buffer[i] = needs_padding.select(padding_len, buffer[i]);
72  }
73 
74  CT::unpoison(buffer.data(), buffer.size());
75  CT::unpoison(last_byte_pos);
76  }
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
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:123
void unpoison(const T *p, size_t n)
Definition: ct_utils.h:59

◆ name()

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

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 80 of file mode_pad.h.

80 { 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 81 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().

82  {
83  if(!valid_blocksize(input_length))
84  return input_length;
85 
86  CT::poison(input, input_length);
87 
88  const uint8_t last_byte = input[input_length-1];
89 
90  /*
91  The input should == the block size so if the last byte exceeds
92  that then the padding is certainly invalid
93  */
94  auto bad_input = CT::Mask<size_t>::is_gt(last_byte, input_length);
95 
96  const size_t pad_pos = input_length - last_byte;
97 
98  for(size_t i = 0; i != input_length - 1; ++i)
99  {
100  // Does this byte equal the expected pad byte?
101  const auto pad_eq = CT::Mask<size_t>::is_equal(input[i], last_byte);
102 
103  // Ignore values that are not part of the padding
104  const auto in_range = CT::Mask<size_t>::is_gte(i, pad_pos);
105  bad_input |= in_range & (~pad_eq);
106  }
107 
108  CT::unpoison(input, input_length);
109 
110  return bad_input.select_and_unpoison(input_length, pad_pos);
111  }
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:78
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 78 of file mode_pad.h.

Referenced by unpad().

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

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