Botan 3.13.0
Crypto and TLS for C&
Botan::ANSI_X923_Padding Class Referencefinal

#include <mode_pad.h>

Inheritance diagram for Botan::ANSI_X923_Padding:
Botan::BlockCipherModePaddingMethod

Public Member Functions

virtual void add_padding (std::span< uint8_t > buffer, size_t final_block_bytes, size_t block_size) const
void apply_padding (std::span< uint8_t > last_block, size_t final_block_bytes) const override
std::string name () const override
virtual size_t output_length (size_t input_length, size_t block_size) const
size_t remove_padding (std::span< const uint8_t > last_block) const override
size_t unpad (std::span< const uint8_t > last_block) const
bool valid_blocksize (size_t bs) const override

Static Public Member Functions

static std::unique_ptr< BlockCipherModePaddingMethodcreate (std::string_view algo_spec)

Detailed Description

ANSI X9.23 Padding

Definition at line 118 of file mode_pad.h.

Member Function Documentation

◆ add_padding()

void Botan::BlockCipherModePaddingMethod::add_padding ( std::span< uint8_t > buffer,
size_t final_block_bytes,
size_t block_size ) const
virtualinherited

Add padding bytes to buffer.

Parameters
bufferdata to pad, span must be large enough to hold the padding behind the final (partial) block
final_block_bytessize of the final block in bytes
block_sizesize of each block in bytes

Reimplemented in Botan::Null_Padding.

Definition at line 50 of file mode_pad.cpp.

50 {
52 BOTAN_ASSERT_NOMSG(last_byte_pos < BS);
53 BOTAN_ASSERT_NOMSG(buffer.size() % BS == 0);
54 BOTAN_ASSERT_NOMSG(buffer.size() >= BS);
55
56 auto poison = CT::scoped_poison(last_byte_pos, buffer);
57 apply_padding(buffer.last(BS), last_byte_pos);
58}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
virtual bool valid_blocksize(size_t block_size) const =0
virtual void apply_padding(std::span< uint8_t > last_block, size_t padding_start_pos) const =0
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:222
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:56

References apply_padding(), BOTAN_ASSERT_NOMSG, Botan::CT::scoped_poison(), and valid_blocksize().

◆ apply_padding()

void Botan::ANSI_X923_Padding::apply_padding ( std::span< uint8_t > last_block,
size_t padding_start_pos ) const
overridevirtual

Applies the concrete padding to the last_block assuming the padding bytes should start at padding_start_pos within the last block.

Concrete implementations of this function must ensure not to leak padding_start_pos via side channels. Both the bytes of last_block and padding_start_pos are passed in with CT::poison applied.

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 119 of file mode_pad.cpp.

119 {
120 /*
121 Padding format is
122 01
123 0002
124 000003
125 ...
126 */
127 const uint8_t BS = static_cast<uint8_t>(last_block.size());
128 const uint8_t start_pos = static_cast<uint8_t>(padding_start_pos);
129 const uint8_t padding_len = BS - start_pos;
130 for(uint8_t i = 0; i != BS - 1; ++i) {
131 auto needs_padding = CT::Mask<uint8_t>::is_gte(i, start_pos);
132 last_block[i] = needs_padding.select(0, last_block[i]);
133 }
134
135 last_block.back() = padding_len;
136}
static constexpr Mask< T > is_gte(T x, T y)
Definition ct_utils.h:468

References Botan::CT::Mask< T >::is_gte().

◆ create()

std::unique_ptr< BlockCipherModePaddingMethod > Botan::BlockCipherModePaddingMethod::create ( std::string_view 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 26 of file mode_pad.cpp.

26 {
27 if(algo_spec == "NoPadding") {
28 return std::make_unique<Null_Padding>();
29 }
30
31 if(algo_spec == "PKCS7") {
32 return std::make_unique<PKCS7_Padding>();
33 }
34
35 if(algo_spec == "OneAndZeros") {
36 return std::make_unique<OneAndZeros_Padding>();
37 }
38
39 if(algo_spec == "X9.23") {
40 return std::make_unique<ANSI_X923_Padding>();
41 }
42
43 if(algo_spec == "ESP") {
44 return std::make_unique<ESP_Padding>();
45 }
46
47 return nullptr;
48}

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

◆ name()

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

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 126 of file mode_pad.h.

126{ return "X9.23"; }

◆ output_length()

size_t Botan::BlockCipherModePaddingMethod::output_length ( size_t input_length,
size_t block_size ) const
virtualinherited
Parameters
input_lengthnumber of bytes to be padded
block_sizesize of each block in bytes
Returns
the total number of output bytes (including the padding)

Reimplemented in Botan::Null_Padding.

Definition at line 17 of file mode_pad.cpp.

17 {
18 // Round input_length down to a multiple of block_size then add a full block
19 const size_t full_blocks = input_length - (input_length % block_size);
20 return add_or_throw(full_blocks, block_size, "Input too large to pad");
21}
constexpr T add_or_throw(T a, T b, std::string_view msg)
Definition int_utils.h:66

References Botan::add_or_throw().

Referenced by Botan::CBC_Encryption::output_length(), and valid_blocksize().

◆ remove_padding()

size_t Botan::ANSI_X923_Padding::remove_padding ( std::span< const uint8_t > last_block) const
overridevirtual

Removes the padding from last_block and returns the number of data bytes. If the padding is invalid, this returns the byte length of last_block.

Concrete implementations of this function must ensure not to leak the size or validity of the padding via side channels. The bytes of last_block are passed in with CT::poison applied to them.

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 141 of file mode_pad.cpp.

141 {
142 const size_t BS = input.size();
143 const size_t last_byte = input.back();
144
145 auto bad_input = CT::Mask<size_t>::is_gt(last_byte, BS);
146
147 const size_t pad_pos = BS - last_byte;
148
149 for(size_t i = 0; i != BS - 1; ++i) {
150 // Ignore values that are not part of the padding
151 const auto in_range = CT::Mask<size_t>::is_gte(i, pad_pos);
152 const auto pad_is_nonzero = CT::Mask<size_t>::expand(input[i]);
153 bad_input |= pad_is_nonzero & in_range;
154 }
155
156 return bad_input.select(BS, pad_pos);
157}
static constexpr Mask< T > expand(T v)
Definition ct_utils.h:392
static constexpr Mask< T > is_gt(T x, T y)
Definition ct_utils.h:458

References Botan::CT::Mask< T >::expand(), Botan::CT::Mask< T >::is_gt(), and Botan::CT::Mask< T >::is_gte().

◆ unpad()

size_t Botan::BlockCipherModePaddingMethod::unpad ( std::span< const uint8_t > last_block) const
inherited

Remove padding bytes from block

Parameters
last_blockthe last block containing the padding
Returns
number of data bytes, or if the padding is invalid returns the byte length of last_block (i.e. the block size)

Definition at line 60 of file mode_pad.cpp.

60 {
61 if(!valid_blocksize(last_block.size())) {
62 return last_block.size();
63 }
64
65 auto poison = CT::scoped_poison(last_block);
66 return CT::driveby_unpoison(remove_padding(last_block));
67}
virtual size_t remove_padding(std::span< const uint8_t > last_block) const =0
decltype(auto) driveby_unpoison(T &&v)
Definition ct_utils.h:243

References Botan::CT::driveby_unpoison(), remove_padding(), Botan::CT::scoped_poison(), and valid_blocksize().

◆ valid_blocksize()

bool Botan::ANSI_X923_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 124 of file mode_pad.h.

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

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