Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
Botan::OneAndZeros_Padding Class Referencefinal

`#include <mode_pad.h>`

Inheritance diagram for Botan::OneAndZeros_Padding:

## 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 103 of file mode_pad.h.

## Member Function Documentation

void Botan::OneAndZeros_Padding::add_padding | ( | secure_vector< uint8_t > & | buffer,
|  | size_t | final_block_bytes,
|  | size_t | block_size
| ) |  | const
| override virtual

Add padding bytes to buffer.

Parameters

  buffer | data to pad
  final_block_bytes | size of the final block in bytes
  block_size | size of each block in bytes

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 134 of file mode_pad.cpp.

137 {
138 buffer.push_back(0x80);
139
140 for(size_t i = last_byte_pos + 1; i % block_size; ++i)
141 buffer.push_back(0x00);
142 }

std::string Botan::OneAndZeros_Padding::name | ( |  | ) | const
| inline override virtual

Returns
  name of the mode

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 114 of file mode_pad.h.

114 { return "OneAndZeros"; }

size_t Botan::OneAndZeros_Padding::unpad | ( | const uint8_t | block[],
|  | size_t | len
| ) |  | const
| override virtual

Remove padding bytes from block

Parameters

  block | the last block
  len | the 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 147 of file mode_pad.cpp.

References Botan::CT::Mask< T >::cleared(), Botan::CT::Mask< T >::expand(), Botan::CT::Mask< T >::is_equal(), Botan::CT::Mask< T >::is_zero(), Botan::CT::poison(), Botan::CT::unpoison(), and valid_blocksize().

148 {
149 if(!valid_blocksize(input_length))
150 return input_length;
151
152 CT::poison(input, input_length);
153
154 auto bad_input = CT::Mask<uint8_t>::cleared();
155 auto seen_0x80 = CT::Mask<uint8_t>::cleared();
156
157 size_t pad_pos = input_length - 1;
158 size_t i = input_length;
159
160 while(i)
161 {
162 const auto is_0x80 = CT::Mask<uint8_t>::is_equal(input[i-1], 0x80);
163 const auto is_zero = CT::Mask<uint8_t>::is_zero(input[i-1]);
164
165 seen_0x80 |= is_0x80;
166 pad_pos -= seen_0x80.if_not_set_return(1);
167 bad_input |= ~seen_0x80 & ~is_zero;
168 i--;
169 }
170 bad_input |= ~seen_0x80;
171
172 CT::unpoison(input, input_length);
173
174 return CT::Mask<size_t>::expand(bad_input).select_and_unpoison(input_length, pad_pos);
175 }
Botan::CT::Mask::cleared
static Mask< T > cleared()
Definition: ct_utils.h:115
Botan::CT::poison
void poison(const T *p, size_t n)
Definition: ct_utils.h:48
Botan::CT::Mask::expand
static Mask< T > expand(T v)
Definition: ct_utils.h:123
Botan::OneAndZeros_Padding::valid_blocksize
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:112
Botan::CT::unpoison
void unpoison(const T *p, size_t n)
Definition: ct_utils.h:59
Botan::CT::Mask::is_zero
static Mask< T > is_zero(T x)
Definition: ct_utils.h:141
Botan::CT::Mask::is_equal
static Mask< T > is_equal(T x, T y)
Definition: ct_utils.h:149

bool Botan::OneAndZeros_Padding::valid_blocksize | ( | size_t | block_size | ) | const
| inline override virtual

Parameters

  block_size | of the cipher

Returns
  valid block size for this padding mode

Implements Botan::BlockCipherModePaddingMethod.

Definition at line 112 of file mode_pad.h.

Referenced by unpad().

112 { return (bs > 2); }

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

- src/lib/modes/mode_pad/mode_pad.h
- src/lib/modes/mode_pad/mode_pad.cpp