Botan 3.0.0-alpha0
Crypto and TLS for C&
Public Member Functions | Static 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
 

Static Public Member Functions

static std::unique_ptr< BlockCipherModePaddingMethodcreate (const std::string &algo_spec)
 

Detailed Description

PKCS#7 Padding

Definition at line 73 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 42 of file mode_pad.cpp.

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

References BOTAN_DEBUG_ASSERT, Botan::CT::poison(), and Botan::CT::unpoison().

◆ create()

std::unique_ptr< BlockCipherModePaddingMethod > Botan::BlockCipherModePaddingMethod::create ( const std::string &  algo_spec)
staticinherited

Get a block cipher padding mode by name (eg "NoPadding" or "PKCS7")

Parameters
algo_specblock cipher padding mode name

Get a block cipher padding method by name

Definition at line 19 of file mode_pad.cpp.

20 {
21 if(algo_spec == "NoPadding")
22 return std::make_unique<Null_Padding>();
23
24 if(algo_spec == "PKCS7")
25 return std::make_unique<PKCS7_Padding>();
26
27 if(algo_spec == "OneAndZeros")
28 return std::make_unique<OneAndZeros_Padding>();
29
30 if(algo_spec == "X9.23")
31 return std::make_unique<ANSI_X923_Padding>();
32
33 if(algo_spec == "ESP")
34 return std::make_unique<ESP_Padding>();
35
36 return nullptr;
37 }

Referenced by Botan::Cipher_Mode::create().

◆ name()

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

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 84 of file mode_pad.h.

84{ 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 82 of file mode_pad.cpp.

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

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().

◆ 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 82 of file mode_pad.h.

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

Referenced by unpad().


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