Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
Botan::PKCS7_Padding Class Referencefinal

`#include <mode_pad.h>`

Inheritance diagram for Botan::PKCS7_Padding:

## 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

void Botan::PKCS7_Padding::add_padding | ( | secure_vector< uint8_t > & | buffer,
|  | size_t | final_block_bytes,
|  | size_t | block_size
| ) |  | const
| override virtual

Add padding bytes to buffer.

Parameters

  buffer | data to pad
  final_block_bytes | size of the final block in bytes
  block_size | size 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 }

std::string Botan::PKCS7_Padding::name | ( |  | ) | const
| inline override virtual

Returns
  name of the mode

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 80 of file mode_pad.h.

80 { return "PKCS7"; }

size_t Botan::PKCS7_Padding::unpad | ( | const uint8_t | block[],
|  | size_t | len
| ) |  | const
| override virtual

Remove padding bytes from block

Parameters

  block | the last block
  len | the 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 }
Botan::CT::Mask::is_gte
static Mask< T > is_gte(T x, T y)
Definition: ct_utils.h:181
Botan::CT::poison
void poison(const T *p, size_t n)
Definition: ct_utils.h:48
Botan::PKCS7_Padding::valid_blocksize
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:78
Botan::CT::unpoison
void unpoison(const T *p, size_t n)
Definition: ct_utils.h:59
Botan::CT::Mask::is_equal
static Mask< T > is_equal(T x, T y)
Definition: ct_utils.h:149
Botan::CT::Mask::is_gt
static Mask< T > is_gt(T x, T y)
Definition: ct_utils.h:165

bool Botan::PKCS7_Padding::valid_blocksize | ( | size_t | block_size | ) | const
| inline override virtual

Parameters

  block_size | of 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:

- src/lib/modes/mode_pad/mode_pad.h
- src/lib/modes/mode_pad/mode_pad.cpp