Botan 3.4.0
Crypto and TLS for C&
mode_pad.h
Go to the documentation of this file.
1/*
2* CBC Padding Methods
3* (C) 1999-2008,2013 Jack Lloyd
4* (C) 2016 René Korthaus, Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_MODE_PADDING_H_
10#define BOTAN_MODE_PADDING_H_
11
12#include <botan/secmem.h>
13#include <string>
14
15namespace Botan {
16
17/**
18* Block Cipher Mode Padding Method
19* This class is pretty limited, it cannot deal well with
20* randomized padding methods, or any padding method that
21* wants to add more than one block. For instance, it should
22* be possible to define cipher text stealing mode as simply
23* a padding mode for CBC, which happens to consume the last
24* two block (and requires use of the block cipher).
25*/
27 public:
28 /**
29 * Get a block cipher padding mode by name (eg "NoPadding" or "PKCS7")
30 * @param algo_spec block cipher padding mode name
31 */
32 static std::unique_ptr<BlockCipherModePaddingMethod> create(std::string_view algo_spec);
33
34 /**
35 * Add padding bytes to buffer.
36 * @param buffer data to pad
37 * @param final_block_bytes size of the final block in bytes
38 * @param block_size size of each block in bytes
39 */
40 virtual void add_padding(secure_vector<uint8_t>& buffer, size_t final_block_bytes, size_t block_size) const = 0;
41
42 /**
43 * Remove padding bytes from block
44 * @param block the last block
45 * @param len the size of the block in bytes
46 * @return number of data bytes, or if the padding is invalid returns len
47 */
48 virtual size_t unpad(const uint8_t block[], size_t len) const = 0;
49
50 /**
51 * @param block_size of the cipher
52 * @return valid block size for this padding mode
53 */
54 virtual bool valid_blocksize(size_t block_size) const = 0;
55
56 /**
57 * @return name of the mode
58 */
59 virtual std::string name() const = 0;
60
61 /**
62 * virtual destructor
63 */
64 virtual ~BlockCipherModePaddingMethod() = default;
65};
66
67/**
68* PKCS#7 Padding
69*/
71 public:
72 void add_padding(secure_vector<uint8_t>& buffer, size_t final_block_bytes, size_t block_size) const override;
73
74 size_t unpad(const uint8_t[], size_t) const override;
75
76 bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
77
78 std::string name() const override { return "PKCS7"; }
79};
80
81/**
82* ANSI X9.23 Padding
83*/
85 public:
86 void add_padding(secure_vector<uint8_t>& buffer, size_t final_block_bytes, size_t block_size) const override;
87
88 size_t unpad(const uint8_t[], size_t) const override;
89
90 bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
91
92 std::string name() const override { return "X9.23"; }
93};
94
95/**
96* One And Zeros Padding (ISO/IEC 9797-1, padding method 2)
97*/
99 public:
100 void add_padding(secure_vector<uint8_t>& buffer, size_t final_block_bytes, size_t block_size) const override;
101
102 size_t unpad(const uint8_t[], size_t) const override;
103
104 bool valid_blocksize(size_t bs) const override { return (bs > 2); }
105
106 std::string name() const override { return "OneAndZeros"; }
107};
108
109/**
110* ESP Padding (RFC 4303)
111*/
113 public:
114 void add_padding(secure_vector<uint8_t>& buffer, size_t final_block_bytes, size_t block_size) const override;
115
116 size_t unpad(const uint8_t[], size_t) const override;
117
118 bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
119
120 std::string name() const override { return "ESP"; }
121};
122
123/**
124* Null Padding
125*/
127 public:
128 void add_padding(secure_vector<uint8_t>&, size_t, size_t) const override { /* no padding */
129 }
130
131 size_t unpad(const uint8_t[], size_t size) const override { return size; }
132
133 bool valid_blocksize(size_t) const override { return true; }
134
135 std::string name() const override { return "NoPadding"; }
136};
137
138} // namespace Botan
139
140#endif
std::string name() const override
Definition mode_pad.h:92
bool valid_blocksize(size_t bs) const override
Definition mode_pad.h:90
virtual std::string name() const =0
virtual bool valid_blocksize(size_t block_size) const =0
virtual size_t unpad(const uint8_t block[], size_t len) const =0
virtual ~BlockCipherModePaddingMethod()=default
virtual void add_padding(secure_vector< uint8_t > &buffer, size_t final_block_bytes, size_t block_size) const =0
std::string name() const override
Definition mode_pad.h:120
bool valid_blocksize(size_t bs) const override
Definition mode_pad.h:118
size_t unpad(const uint8_t[], size_t size) const override
Definition mode_pad.h:131
std::string name() const override
Definition mode_pad.h:135
bool valid_blocksize(size_t) const override
Definition mode_pad.h:133
void add_padding(secure_vector< uint8_t > &, size_t, size_t) const override
Definition mode_pad.h:128
std::string name() const override
Definition mode_pad.h:106
bool valid_blocksize(size_t bs) const override
Definition mode_pad.h:104
bool valid_blocksize(size_t bs) const override
Definition mode_pad.h:76
std::string name() const override
Definition mode_pad.h:78
int(* final)(unsigned char *, CTX *)
#define BOTAN_TEST_API
Definition compiler.h:51
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61