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

#include <mode_pad.h>

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

One And Zeros Padding (ISO/IEC 9797-1, padding method 2)

Definition at line 101 of file mode_pad.h.

Member Function Documentation

◆ add_padding()

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

125  {
126  buffer.push_back(0x80);
127 
128  for(size_t i = last_byte_pos + 1; i % block_size; ++i)
129  buffer.push_back(0x00);
130  }

◆ name()

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

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 112 of file mode_pad.h.

112 { return "OneAndZeros"; }

◆ unpad()

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

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

136  {
137  if(input_length <= 2)
138  return input_length;
139 
140  CT::poison(input, input_length);
141 
142  uint8_t bad_input = 0;
143  uint8_t seen_one = 0;
144  size_t pad_pos = input_length - 1;
145  size_t i = input_length;
146 
147  while(i)
148  {
149  seen_one |= CT::is_equal<uint8_t>(input[i-1], 0x80);
150  pad_pos -= CT::select<uint8_t>(~seen_one, 1, 0);
151  bad_input |= ~CT::is_zero<uint8_t>(input[i-1]) & ~seen_one;
152  i--;
153  }
154  bad_input |= ~seen_one;
155 
156  CT::unpoison(input, input_length);
157  return CT::conditional_return(bad_input, input_length, pad_pos);
158  }
void poison(const T *p, size_t n)
Definition: ct_utils.h:47
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::OneAndZeros_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 110 of file mode_pad.h.

110 { return (bs > 2); }

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