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

#include <mode_pad.h>

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

ANSI X9.23 Padding

Definition at line 84 of file mode_pad.h.

Member Function Documentation

◆ add_padding()

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

92  {
93  const uint8_t pad_value = static_cast<uint8_t>(block_size - last_byte_pos);
94 
95  for(size_t i = last_byte_pos; i < block_size-1; ++i)
96  {
97  buffer.push_back(0);
98  }
99  buffer.push_back(pad_value);
100  }

◆ name()

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

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 95 of file mode_pad.h.

95 { return "X9.23"; }

◆ unpad()

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

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

106  {
107  if(!valid_blocksize(input_length))
108  return input_length;
109 
110  CT::poison(input, input_length);
111 
112  const size_t last_byte = input[input_length-1];
113 
114  auto bad_input = CT::Mask<size_t>::is_gt(last_byte, input_length);
115 
116  const size_t pad_pos = input_length - last_byte;
117 
118  for(size_t i = 0; i != input_length - 1; ++i)
119  {
120  // Ignore values that are not part of the padding
121  const auto in_range = CT::Mask<size_t>::is_gte(i, pad_pos);
122  const auto pad_is_nonzero = CT::Mask<size_t>::expand(input[i]);
123  bad_input |= pad_is_nonzero & in_range;
124  }
125 
126  CT::unpoison(input, input_length);
127 
128  return bad_input.select_and_unpoison(input_length, pad_pos);
129  }
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:93
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
static Mask< T > expand(T v)
Definition: ct_utils.h:123
void unpoison(const T *p, size_t n)
Definition: ct_utils.h:59
static Mask< T > is_gt(T x, T y)
Definition: ct_utils.h:165

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

Referenced by unpad().

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

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