Botan 2.19.5 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
15 BOTAN_FUTURE_INTERNAL_HEADER(mode_pad.h)
16
17namespace Botan {
18
19/**
20* Block Cipher Mode Padding Method
21* This class is pretty limited, it cannot deal well with
22* randomized padding methods, or any padding method that
23* wants to add more than one block. For instance, it should
24* be possible to define cipher text stealing mode as simply
25* a padding mode for CBC, which happens to consume the last
26* two block (and requires use of the block cipher).
27*/
28 class BOTAN_PUBLIC_API(2,0) BlockCipherModePaddingMethod
29 {
30 public:
31 /**
32 * Add padding bytes to buffer.
33 * @param buffer data to pad
34 * @param final_block_bytes size of the final block in bytes
35 * @param block_size size of each block in bytes
36 */
37 virtual void add_padding(secure_vector<uint8_t>& buffer,
38 size_t final_block_bytes,
39 size_t block_size) const = 0;
40
41 /**
42 * Remove padding bytes from block
43 * @param block the last block
44 * @param len the size of the block in bytes
45 * @return number of data bytes, or if the padding is invalid returns len
46 */
47 virtual size_t unpad(const uint8_t block[], size_t len) const = 0;
48
49 /**
50 * @param block_size of the cipher
51 * @return valid block size for this padding mode
52 */
53 virtual bool valid_blocksize(size_t block_size) const = 0;
54
55 /**
56 * @return name of the mode
57 */
58 virtual std::string name() const = 0;
59
60 /**
61 * virtual destructor
62 */
63 virtual ~BlockCipherModePaddingMethod() {}
64 };
65
66/**
67* PKCS#7 Padding
68*/
69 class BOTAN_PUBLIC_API(2,0) PKCS7_Padding final : public BlockCipherModePaddingMethod
70 {
71 public:
72 void add_padding(secure_vector<uint8_t>& buffer,
73 size_t final_block_bytes,
74 size_t block_size) const override;
75
76 size_t unpad(const uint8_t[], size_t) const override;
77
78 bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
79
80 std::string name() const override { return "PKCS7"; }
81 };
82
83/**
84* ANSI X9.23 Padding
85*/
86 class BOTAN_PUBLIC_API(2,0) ANSI_X923_Padding final : public BlockCipherModePaddingMethod
87 {
88 public:
89 void add_padding(secure_vector<uint8_t>& buffer,
90 size_t final_block_bytes,
91 size_t block_size) const override;
92
93 size_t unpad(const uint8_t[], size_t) const override;
94
95 bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
96
97 std::string name() const override { return "X9.23"; }
98 };
99
100/**
101* One And Zeros Padding (ISO/IEC 9797-1, padding method 2)
102*/
103 class BOTAN_PUBLIC_API(2,0) OneAndZeros_Padding final : public BlockCipherModePaddingMethod
104 {
105 public:
106 void add_padding(secure_vector<uint8_t>& buffer,
107 size_t final_block_bytes,
108 size_t block_size) const override;
109
110 size_t unpad(const uint8_t[], size_t) const override;
111
112 bool valid_blocksize(size_t bs) const override { return (bs > 2); }
113
114 std::string name() const override { return "OneAndZeros"; }
115 };
116
117/**
118* ESP Padding (RFC 4304)
119*/
120 class BOTAN_PUBLIC_API(2,0) ESP_Padding final : public BlockCipherModePaddingMethod
121 {
122 public:
123 void add_padding(secure_vector<uint8_t>& buffer,
124 size_t final_block_bytes,
125 size_t block_size) const override;
126
127 size_t unpad(const uint8_t[], size_t) const override;
128
129 bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
130
131 std::string name() const override { return "ESP"; }
132 };
133
134/**
135* Null Padding
136*/
137 class BOTAN_PUBLIC_API(2,0) Null_Padding final : public BlockCipherModePaddingMethod
138 {
139 public:
140 void add_padding(secure_vector<uint8_t>&, size_t, size_t) const override
141 {
142 /* no padding */
143 }
144
145 size_t unpad(const uint8_t[], size_t size) const override { return size; }
146
147 bool valid_blocksize(size_t) const override { return true; }
148
149 std::string name() const override { return "NoPadding"; }
150 };
151
152/**
153* Get a block cipher padding mode by name (eg "NoPadding" or "PKCS7")
154* @param algo_spec block cipher padding mode name
155*/
156 BOTAN_PUBLIC_API(2,0) BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec);
157
158}
159
160#endif
Botan::ANSI_X923_Padding
Definition: mode_pad.h:87
Botan::ANSI_X923_Padding::name
std::string name() const override
Definition: mode_pad.h:97
Botan::ANSI_X923_Padding::valid_blocksize
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:95
Botan::BlockCipherModePaddingMethod
Definition: mode_pad.h:29
Botan::BlockCipherModePaddingMethod::name
virtual std::string name() const =0
Botan::BlockCipherModePaddingMethod::valid_blocksize
virtual bool valid_blocksize(size_t block_size) const =0
Botan::BlockCipherModePaddingMethod::unpad
virtual size_t unpad(const uint8_t block[], size_t len) const =0
Botan::BlockCipherModePaddingMethod::~BlockCipherModePaddingMethod
virtual ~BlockCipherModePaddingMethod()
Definition: mode_pad.h:63
Botan::BlockCipherModePaddingMethod::add_padding
virtual void add_padding(secure_vector< uint8_t > &buffer, size_t final_block_bytes, size_t block_size) const =0
Botan::ESP_Padding
Definition: mode_pad.h:121
Botan::ESP_Padding::name
std::string name() const override
Definition: mode_pad.h:131
Botan::ESP_Padding::valid_blocksize
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:129
Botan::Null_Padding
Definition: mode_pad.h:138
Botan::Null_Padding::unpad
size_t unpad(const uint8_t[], size_t size) const override
Definition: mode_pad.h:145
Botan::Null_Padding::name
std::string name() const override
Definition: mode_pad.h:149
Botan::Null_Padding::valid_blocksize
bool valid_blocksize(size_t) const override
Definition: mode_pad.h:147
Botan::Null_Padding::add_padding
void add_padding(secure_vector< uint8_t > &, size_t, size_t) const override
Definition: mode_pad.h:140
Botan::OneAndZeros_Padding
Definition: mode_pad.h:104
Botan::OneAndZeros_Padding::name
std::string name() const override
Definition: mode_pad.h:114
Botan::OneAndZeros_Padding::valid_blocksize
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:112
Botan::PKCS7_Padding
Definition: mode_pad.h:70
Botan::PKCS7_Padding::valid_blocksize
bool valid_blocksize(size_t bs) const override
Definition: mode_pad.h:78
Botan::PKCS7_Padding::name
std::string name() const override
Definition: mode_pad.h:80
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:29
BOTAN_PUBLIC_API
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
BOTAN_FUTURE_INTERNAL_HEADER
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:136
Botan
Definition: alg_id.cpp:13
Botan::get_bc_pad
BlockCipherModePaddingMethod * get_bc_pad(const std::string &algo_spec)
Definition: mode_pad.cpp:18
Botan::secure_vector
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
std
Definition: bigint.h:1155