Botan  2.8.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 80 of file mode_pad.cpp.

83  {
84  const uint8_t pad_value = static_cast<uint8_t>(block_size - last_byte_pos);
85 
86  for(size_t i = last_byte_pos; i < block_size-1; ++i)
87  {
88  buffer.push_back(0);
89  }
90  buffer.push_back(pad_value);
91  }

◆ 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 96 of file mode_pad.cpp.

References Botan::CT::conditional_return(), Botan::CT::expand_mask(), Botan::CT::poison(), and Botan::CT::unpoison().

97  {
98  if(input_length <= 2)
99  return input_length;
100 
101  CT::poison(input, input_length);
102  const size_t last_byte = input[input_length-1];
103 
104  uint8_t bad_input = 0;
105  bad_input |= CT::expand_mask<uint8_t>(last_byte > input_length);
106 
107  const size_t pad_pos = input_length - last_byte;
108 
109  for(size_t i = 0; i != input_length - 1; ++i)
110  {
111  const uint8_t in_range = CT::expand_mask<uint8_t>(i >= pad_pos);
112  bad_input |= CT::expand_mask(input[i]) & in_range;
113  }
114 
115  CT::unpoison(input, input_length);
116  return CT::conditional_return(bad_input, input_length, pad_pos);
117  }
void poison(const T *p, size_t n)
Definition: ct_utils.h:47
T expand_mask(T x)
Definition: ct_utils.h:103
void unpoison(const T *p, size_t n)
Definition: ct_utils.h:58
T conditional_return(C condvar, T left, T right)
Definition: ct_utils.h:153

◆ 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.

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

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